@@ -5,8 +5,8 @@ namespace ts {
5
5
startRecordingFilesWithChangedResolutions ( ) : void ;
6
6
finishRecordingFilesWithChangedResolutions ( ) : Path [ ] | undefined ;
7
7
8
- resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined , redirectedReference ?: ResolvedProjectReference ) : ( ResolvedModuleFull | undefined ) [ ] ;
9
- getResolvedModuleWithFailedLookupLocationsFromCache ( moduleName : string , containingFile : string ) : CachedResolvedModuleWithFailedLookupLocations | undefined ;
8
+ resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined , redirectedReference ?: ResolvedProjectReference , containingSourceFile ?: SourceFile ) : ( ResolvedModuleFull | undefined ) [ ] ;
9
+ getResolvedModuleWithFailedLookupLocationsFromCache ( moduleName : string , containingFile : string , resolutionMode ?: ModuleKind . CommonJS | ModuleKind . ESNext ) : CachedResolvedModuleWithFailedLookupLocations | undefined ;
10
10
resolveTypeReferenceDirectives ( typeDirectiveNames : string [ ] , containingFile : string , redirectedReference ?: ResolvedProjectReference ) : ( ResolvedTypeReferenceDirective | undefined ) [ ] ;
11
11
12
12
invalidateResolutionsOfFailedLookupLocations ( ) : boolean ;
@@ -166,8 +166,8 @@ namespace ts {
166
166
// The resolvedModuleNames and resolvedTypeReferenceDirectives are the cache of resolutions per file.
167
167
// The key in the map is source file's path.
168
168
// The values are Map of resolutions with key being name lookedup.
169
- const resolvedModuleNames = new Map < Path , ESMap < string , CachedResolvedModuleWithFailedLookupLocations > > ( ) ;
170
- const perDirectoryResolvedModuleNames : CacheWithRedirects < ESMap < string , CachedResolvedModuleWithFailedLookupLocations > > = createCacheWithRedirects ( ) ;
169
+ const resolvedModuleNames = new Map < Path , ModeAwareCache < CachedResolvedModuleWithFailedLookupLocations > > ( ) ;
170
+ const perDirectoryResolvedModuleNames : CacheWithRedirects < ModeAwareCache < CachedResolvedModuleWithFailedLookupLocations > > = createCacheWithRedirects ( ) ;
171
171
const nonRelativeModuleNameCache : CacheWithRedirects < PerModuleNameCache > = createCacheWithRedirects ( ) ;
172
172
const moduleResolutionCache = createModuleResolutionCache (
173
173
getCurrentDirectory ( ) ,
@@ -177,8 +177,8 @@ namespace ts {
177
177
nonRelativeModuleNameCache ,
178
178
) ;
179
179
180
- const resolvedTypeReferenceDirectives = new Map < Path , ESMap < string , CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > ( ) ;
181
- const perDirectoryResolvedTypeReferenceDirectives : CacheWithRedirects < ESMap < string , CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > = createCacheWithRedirects ( ) ;
180
+ const resolvedTypeReferenceDirectives = new Map < Path , ModeAwareCache < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > ( ) ;
181
+ const perDirectoryResolvedTypeReferenceDirectives : CacheWithRedirects < ModeAwareCache < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > = createCacheWithRedirects ( ) ;
182
182
const typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache (
183
183
getCurrentDirectory ( ) ,
184
184
resolutionHost . getCanonicalFileName ,
@@ -354,27 +354,28 @@ namespace ts {
354
354
names : readonly string [ ] ;
355
355
containingFile : string ;
356
356
redirectedReference : ResolvedProjectReference | undefined ;
357
- cache : ESMap < Path , ESMap < string , T > > ;
358
- perDirectoryCacheWithRedirects : CacheWithRedirects < ESMap < string , T > > ;
359
- loader : ( name : string , containingFile : string , options : CompilerOptions , host : ModuleResolutionHost , redirectedReference ?: ResolvedProjectReference ) => T ;
357
+ cache : ESMap < Path , ModeAwareCache < T > > ;
358
+ perDirectoryCacheWithRedirects : CacheWithRedirects < ModeAwareCache < T > > ;
359
+ loader : ( name : string , containingFile : string , options : CompilerOptions , host : ModuleResolutionHost , redirectedReference ?: ResolvedProjectReference , containingSourceFile ?: SourceFile ) => T ;
360
360
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ;
361
361
shouldRetryResolution : ( t : T ) => boolean ;
362
362
reusedNames ?: readonly string [ ] ;
363
363
logChanges ?: boolean ;
364
+ containingSourceFile ?: SourceFile ;
364
365
}
365
366
function resolveNamesWithLocalCache < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > ( {
366
367
names, containingFile, redirectedReference,
367
368
cache, perDirectoryCacheWithRedirects,
368
369
loader, getResolutionWithResolvedFileName,
369
- shouldRetryResolution, reusedNames, logChanges
370
+ shouldRetryResolution, reusedNames, logChanges, containingSourceFile
370
371
} : ResolveNamesWithLocalCacheInput < T , R > ) : ( R | undefined ) [ ] {
371
372
const path = resolutionHost . toPath ( containingFile ) ;
372
- const resolutionsInFile = cache . get ( path ) || cache . set ( path , new Map ( ) ) . get ( path ) ! ;
373
+ const resolutionsInFile = cache . get ( path ) || cache . set ( path , createModeAwareCache ( ) ) . get ( path ) ! ;
373
374
const dirPath = getDirectoryPath ( path ) ;
374
375
const perDirectoryCache = perDirectoryCacheWithRedirects . getOrCreateMapOfCacheRedirects ( redirectedReference ) ;
375
376
let perDirectoryResolution = perDirectoryCache . get ( dirPath ) ;
376
377
if ( ! perDirectoryResolution ) {
377
- perDirectoryResolution = new Map ( ) ;
378
+ perDirectoryResolution = createModeAwareCache ( ) ;
378
379
perDirectoryCache . set ( dirPath , perDirectoryResolution ) ;
379
380
}
380
381
const resolvedModules : ( R | undefined ) [ ] = [ ] ;
@@ -388,16 +389,19 @@ namespace ts {
388
389
! redirectedReference || redirectedReference . sourceFile . path !== oldRedirect . sourceFile . path :
389
390
! ! redirectedReference ;
390
391
391
- const seenNamesInFile = new Map < string , true > ( ) ;
392
+ const seenNamesInFile = createModeAwareCache < true > ( ) ;
393
+ let i = 0 ;
392
394
for ( const name of names ) {
393
- let resolution = resolutionsInFile . get ( name ) ;
395
+ const mode = containingSourceFile ? getModeForResolutionAtIndex ( containingSourceFile , i ) : undefined ;
396
+ i ++ ;
397
+ let resolution = resolutionsInFile . get ( name , mode ) ;
394
398
// Resolution is valid if it is present and not invalidated
395
- if ( ! seenNamesInFile . has ( name ) &&
399
+ if ( ! seenNamesInFile . has ( name , mode ) &&
396
400
unmatchedRedirects || ! resolution || resolution . isInvalidated ||
397
401
// If the name is unresolved import that was invalidated, recalculate
398
402
( hasInvalidatedNonRelativeUnresolvedImport && ! isExternalModuleNameRelative ( name ) && shouldRetryResolution ( resolution ) ) ) {
399
403
const existingResolution = resolution ;
400
- const resolutionInDirectory = perDirectoryResolution . get ( name ) ;
404
+ const resolutionInDirectory = perDirectoryResolution . get ( name , mode ) ;
401
405
if ( resolutionInDirectory ) {
402
406
resolution = resolutionInDirectory ;
403
407
const host = resolutionHost . getCompilerHost ?.( ) || resolutionHost ;
@@ -425,10 +429,10 @@ namespace ts {
425
429
}
426
430
}
427
431
else {
428
- resolution = loader ( name , containingFile , compilerOptions , resolutionHost . getCompilerHost ?.( ) || resolutionHost , redirectedReference ) ;
429
- perDirectoryResolution . set ( name , resolution ) ;
432
+ resolution = loader ( name , containingFile , compilerOptions , resolutionHost . getCompilerHost ?.( ) || resolutionHost , redirectedReference , containingSourceFile ) ;
433
+ perDirectoryResolution . set ( name , mode , resolution ) ;
430
434
}
431
- resolutionsInFile . set ( name , resolution ) ;
435
+ resolutionsInFile . set ( name , mode , resolution ) ;
432
436
watchFailedLookupLocationsOfExternalModuleResolutions ( name , resolution , path , getResolutionWithResolvedFileName ) ;
433
437
if ( existingResolution ) {
434
438
stopWatchFailedLookupLocationOfResolution ( existingResolution , path , getResolutionWithResolvedFileName ) ;
@@ -442,7 +446,7 @@ namespace ts {
442
446
}
443
447
else {
444
448
const host = resolutionHost . getCompilerHost ?.( ) || resolutionHost ;
445
- if ( isTraceEnabled ( compilerOptions , host ) && ! seenNamesInFile . has ( name ) ) {
449
+ if ( isTraceEnabled ( compilerOptions , host ) && ! seenNamesInFile . has ( name , mode ) ) {
446
450
const resolved = getResolutionWithResolvedFileName ( resolution ) ;
447
451
trace (
448
452
host ,
@@ -465,15 +469,15 @@ namespace ts {
465
469
}
466
470
}
467
471
Debug . assert ( resolution !== undefined && ! resolution . isInvalidated ) ;
468
- seenNamesInFile . set ( name , true ) ;
472
+ seenNamesInFile . set ( name , mode , true ) ;
469
473
resolvedModules . push ( getResolutionWithResolvedFileName ( resolution ) ) ;
470
474
}
471
475
472
476
// Stop watching and remove the unused name
473
- resolutionsInFile . forEach ( ( resolution , name ) => {
474
- if ( ! seenNamesInFile . has ( name ) && ! contains ( reusedNames , name ) ) {
477
+ resolutionsInFile . forEach ( ( resolution , name , mode ) => {
478
+ if ( ! seenNamesInFile . has ( name , mode ) && ! contains ( reusedNames , name ) ) {
475
479
stopWatchFailedLookupLocationOfResolution ( resolution , path , getResolutionWithResolvedFileName ) ;
476
- resolutionsInFile . delete ( name ) ;
480
+ resolutionsInFile . delete ( name , mode ) ;
477
481
}
478
482
} ) ;
479
483
@@ -511,7 +515,7 @@ namespace ts {
511
515
} ) ;
512
516
}
513
517
514
- function resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined , redirectedReference ?: ResolvedProjectReference ) : ( ResolvedModuleFull | undefined ) [ ] {
518
+ function resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined , redirectedReference ?: ResolvedProjectReference , containingSourceFile ?: SourceFile ) : ( ResolvedModuleFull | undefined ) [ ] {
515
519
return resolveNamesWithLocalCache < CachedResolvedModuleWithFailedLookupLocations , ResolvedModuleFull > ( {
516
520
names : moduleNames ,
517
521
containingFile,
@@ -523,12 +527,14 @@ namespace ts {
523
527
shouldRetryResolution : resolution => ! resolution . resolvedModule || ! resolutionExtensionIsTSOrJson ( resolution . resolvedModule . extension ) ,
524
528
reusedNames,
525
529
logChanges : logChangesWhenResolvingModule ,
530
+ containingSourceFile,
526
531
} ) ;
527
532
}
528
533
529
- function getResolvedModuleWithFailedLookupLocationsFromCache ( moduleName : string , containingFile : string ) : CachedResolvedModuleWithFailedLookupLocations | undefined {
534
+ function getResolvedModuleWithFailedLookupLocationsFromCache ( moduleName : string , containingFile : string , resolutionMode ?: ModuleKind . CommonJS | ModuleKind . ESNext ) : CachedResolvedModuleWithFailedLookupLocations | undefined {
530
535
const cache = resolvedModuleNames . get ( resolutionHost . toPath ( containingFile ) ) ;
531
- return cache && cache . get ( moduleName ) ;
536
+ if ( ! cache ) return undefined ;
537
+ return cache . get ( moduleName , resolutionMode ) ;
532
538
}
533
539
534
540
function isNodeModulesAtTypesDirectory ( dirPath : Path ) {
@@ -751,7 +757,7 @@ namespace ts {
751
757
}
752
758
753
759
function removeResolutionsOfFileFromCache < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
754
- cache : ESMap < string , ESMap < string , T > > ,
760
+ cache : ESMap < string , ModeAwareCache < T > > ,
755
761
filePath : Path ,
756
762
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
757
763
) {
0 commit comments