@@ -360,4 +360,74 @@ export class LanguageServiceHost implements ts.LanguageServiceHost {
360
360
return this . config . projectFileDirectory ;
361
361
}
362
362
getDefaultLibFileName = ts . getDefaultLibFileName ;
363
+
364
+ resolveModuleNames ( moduleNames : string [ ] , containingFile : string ) : string [ ] {
365
+ return moduleNames . map ( x => this . resolveExternalModule ( x , containingFile ) ) ;
366
+ }
367
+
368
+ /**
369
+ * node_modules resolution logic
370
+ * Code from https://github.com/Microsoft/TypeScript/pull/3147/files
371
+ */
372
+ resolvedExternalModuleCache : ts . Map < string > = { } ;
373
+ resolveExternalModule ( moduleName : string , containingFile : string ) : string {
374
+ let normalizePath = ts . normalizePath ;
375
+ let combinePaths = ts . combinePaths ;
376
+ let removeFileExtension = ts . removeFileExtension ;
377
+ let getDirectoryPath = ts . getDirectoryPath ;
378
+ let forEach = ts . forEach ;
379
+ let supportedExtensions = ts . supportedExtensions ;
380
+
381
+ let cacheLookupName = moduleName + containingFile ;
382
+ if ( this . resolvedExternalModuleCache [ cacheLookupName ] ) {
383
+ return this . resolvedExternalModuleCache [ cacheLookupName ] ;
384
+ }
385
+ if ( this . resolvedExternalModuleCache [ cacheLookupName ] === '' ) {
386
+ return undefined ;
387
+ }
388
+ function getNameIfExists ( fileName : string ) : string {
389
+ if ( fs . existsSync ( fileName ) ) {
390
+ return fileName ;
391
+ }
392
+ }
393
+ while ( true ) {
394
+ // Look at files by all extensions
395
+ let found = ts . forEach ( ts . supportedExtensions ,
396
+ extension => getNameIfExists ( ts . normalizePath ( ts . combinePaths ( containingFile , moduleName ) ) + extension ) ) ;
397
+ // Also look at all files by node_modules
398
+ if ( ! found ) {
399
+ found = ts . forEach ( ts . supportedExtensions ,
400
+ extension => getNameIfExists ( ts . normalizePath ( ts . combinePaths ( ts . combinePaths ( containingFile , "node_modules" ) , moduleName ) ) + extension ) ) ;
401
+ }
402
+ // Also look at package.json's main in node_modules
403
+ if ( ! found ) {
404
+ // If we found a package.json then look at its main field
405
+ let pkgJson = getNameIfExists ( normalizePath ( combinePaths ( combinePaths ( combinePaths ( containingFile , "node_modules" ) , moduleName ) , "package.json" ) ) ) ;
406
+ if ( pkgJson ) {
407
+ let pkgFile = JSON . parse ( fs . readFileSync ( pkgJson , 'utf8' ) ) ;
408
+ if ( pkgFile . main ) {
409
+ var indexFileName = removeFileExtension ( combinePaths ( getDirectoryPath ( pkgJson ) , pkgFile . main ) ) ;
410
+ found = forEach ( supportedExtensions ,
411
+ extension => getNameIfExists ( indexFileName + extension ) )
412
+ }
413
+ }
414
+ }
415
+ // look at node_modules index
416
+ if ( ! found ) {
417
+ found = forEach ( supportedExtensions ,
418
+ extension => getNameIfExists ( normalizePath ( combinePaths ( combinePaths ( combinePaths ( containingFile , "node_modules" ) , moduleName ) , "index" ) ) + extension ) ) ;
419
+ }
420
+
421
+ // Finally cache and return or continue up the directory tree
422
+ if ( found ) {
423
+ return this . resolvedExternalModuleCache [ cacheLookupName ] = found ;
424
+ }
425
+ let parentPath = getDirectoryPath ( containingFile ) ;
426
+ if ( parentPath === containingFile ) {
427
+ this . resolvedExternalModuleCache [ cacheLookupName ] = '' ;
428
+ return undefined ;
429
+ }
430
+ containingFile = parentPath ;
431
+ }
432
+ }
363
433
}
0 commit comments