11// modified from https://github.com/Microsoft/typescript-tslint-plugin
2+ import path from 'path'
23import merge from "merge-deep" ;
34import mockRequire from "mock-require" ;
45import ts_module , { ResolvedModuleFull , CompilerOptions } from "typescript/lib/tsserverlibrary" ;
6+ import { parseFromString , resolve , ImportMaps } from 'import-maps'
57
68import { Logger } from "./logger" ;
79import {
810 getGlobalDtsPath ,
911 getLocalDtsPath ,
1012 getDtsPathForVscode ,
13+ normalizeFilepath ,
14+ pathExistsSync ,
1115} from "./utils" ;
1216
1317import { universalModuleResolver } from "./module_resolver/universal_module_resolver" ;
18+ import { readFileSync } from 'fs' ;
19+ import { URL } from 'url' ;
1420
1521let logger : Logger ;
1622
1723type DenoPluginConfig = {
1824 enable : boolean ;
19- import_map ?: string ;
25+ importmap ?: string ;
2026 dtsPath ?: string ;
2127} ;
2228
@@ -79,6 +85,13 @@ module.exports = function init(
7985 // TypeScript plugins have a `cwd` of `/`, which causes issues with import resolution.
8086 process . chdir ( projectDirectory ) ;
8187
88+ let parsedImportMap : ImportMaps | null = null ;
89+
90+ if ( config . importmap != null ) {
91+ logger . info ( 'use import maps: ' + config . importmap ) ;
92+ parsedImportMap = parseImportMapFromFile ( projectDirectory , config . importmap ) ;
93+ }
94+
8295 const resolveTypeReferenceDirectives =
8396 tsLsHost . resolveTypeReferenceDirectives ;
8497
@@ -89,8 +102,6 @@ module.exports = function init(
89102 redirectedReference : ts_module . ResolvedProjectReference | undefined ,
90103 options : ts_module . CompilerOptions ,
91104 ) : ( ts_module . ResolvedTypeReferenceDirective | undefined ) [ ] => {
92- logger . info ( `typeDirectiveNames: ${ typeDirectiveNames } ` ) ;
93- logger . info ( `containingFile: ${ containingFile } ` ) ;
94105 const ret = resolveTypeReferenceDirectives . call (
95106 tsLsHost ,
96107 typeDirectiveNames ,
@@ -99,8 +110,6 @@ module.exports = function init(
99110 options ,
100111 ) ;
101112
102- logger . info ( JSON . stringify ( ret , null , " " ) ) ;
103-
104113 return ret ;
105114 } ;
106115 }
@@ -116,7 +125,17 @@ module.exports = function init(
116125 const resolvedModules : ( ResolvedModuleFull | undefined ) [ ] = [ ] ;
117126
118127 // try resolve typeReferenceDirectives
119- for ( const moduleName of moduleNames ) {
128+ for ( let moduleName of moduleNames ) {
129+ if ( parsedImportMap !== null ) {
130+ try {
131+ const moduleUrl = resolve ( moduleName , parsedImportMap , new URL ( projectDirectory , 'file:///' ) )
132+ moduleName = moduleUrl . protocol === 'file:' ? moduleUrl . pathname : moduleUrl . href ;
133+ } catch ( e ) {
134+ resolvedModules . push ( undefined ) ;
135+ continue ;
136+ }
137+ }
138+
120139 const resolvedModule = universalModuleResolver . resolve (
121140 moduleName ,
122141 containingFile ,
@@ -186,8 +205,6 @@ module.exports = function init(
186205 info . languageServiceHost ,
187206 ) ;
188207
189- logger . info ( `getScriptFileNames:${ JSON . stringify ( scriptFileNames ) } ` ) ;
190-
191208 const denoDtsPath = getDtsPathForVscode ( info ) ||
192209 getGlobalDtsPath ( ) ||
193210 getLocalDtsPath ( info . languageServiceHost ) ;
@@ -294,3 +311,25 @@ module.exports = function init(
294311 } ,
295312 } ;
296313} ;
314+
315+ function parseImportMapFromFile ( cwd : string , file : string ) : ImportMaps | null {
316+ if ( ! path . isAbsolute ( file ) ) {
317+ file = path . resolve ( cwd , file )
318+ }
319+
320+ const fullFilePath = normalizeFilepath ( file )
321+
322+ if ( ! pathExistsSync ( fullFilePath ) ) {
323+ return null ;
324+ }
325+
326+ const content = readFileSync ( fullFilePath , {
327+ encoding : "utf8" ,
328+ } ) ;
329+
330+ try {
331+ return parseFromString ( content , `file://${ cwd } /` ) ;
332+ } catch {
333+ return null ;
334+ }
335+ }
0 commit comments