4
4
clearMap ,
5
5
closeFileWatcher ,
6
6
closeFileWatcherOf ,
7
+ CompilerHostSupportingResolutionCache ,
7
8
CompilerOptions ,
8
9
createModeAwareCache ,
9
10
createModuleResolutionCache ,
@@ -22,6 +23,7 @@ import {
22
23
FileWatcher ,
23
24
FileWatcherCallback ,
24
25
firstDefinedIterator ,
26
+ getAutomaticTypeDirectiveContainingFile ,
25
27
GetCanonicalFileName ,
26
28
getDirectoryPath ,
27
29
getEffectiveTypeRoots ,
@@ -63,6 +65,7 @@ import {
63
65
resolutionExtensionIsTSOrJson ,
64
66
ResolutionLoader ,
65
67
ResolutionMode ,
68
+ ResolutionNameAndModeGetter ,
66
69
ResolvedModuleWithFailedLookupLocations ,
67
70
ResolvedProjectReference ,
68
71
ResolvedTypeReferenceDirectiveWithFailedLookupLocations ,
@@ -75,6 +78,7 @@ import {
75
78
stringContains ,
76
79
StringLiteralLike ,
77
80
trace ,
81
+ typeReferenceResolutionNameAndModeGetter ,
78
82
updateResolutionField ,
79
83
WatchDirectoryFlags ,
80
84
} from "./_namespaces/ts" ;
@@ -89,7 +93,7 @@ export interface HasInvalidatedFromResolutionCache {
89
93
*
90
94
* @internal
91
95
*/
92
- export interface ResolutionCache {
96
+ export interface ResolutionCache extends Required < CompilerHostSupportingResolutionCache > {
93
97
rootDirForResolution : string ;
94
98
resolvedModuleNames : Map < Path , ModeAwareCache < CachedResolvedModuleWithFailedLookupLocations > > ;
95
99
resolvedTypeReferenceDirectives : Map < Path , ModeAwareCache < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > ;
@@ -117,6 +121,7 @@ export interface ResolutionCache {
117
121
options : CompilerOptions ,
118
122
containingSourceFile : SourceFile ,
119
123
reusedNames : readonly StringLiteralLike [ ] | undefined ,
124
+ ambientModuleNames : readonly StringLiteralLike [ ] | undefined ,
120
125
) : readonly ResolvedModuleWithFailedLookupLocations [ ] ;
121
126
resolveTypeReferenceDirectiveReferences < T extends FileReference | string > (
122
127
typeDirectiveReferences : readonly T [ ] ,
@@ -574,6 +579,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
574
579
resolveSingleModuleNameWithoutWatching,
575
580
removeResolutionsFromProjectReferenceRedirects,
576
581
removeResolutionsOfFile,
582
+ reusedModuleResolutions,
583
+ reusedTypeReferenceDirectiveResolutions,
577
584
hasChangedAutomaticTypeDirectiveNames : ( ) => hasChangedAutomaticTypeDirectiveNames ,
578
585
invalidateResolutionOfFile,
579
586
invalidateResolutionsOfFailedLookupLocations,
@@ -737,7 +744,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
737
744
containingSourceFile : SourceFile ;
738
745
redirectedReference : ResolvedProjectReference | undefined ;
739
746
options : CompilerOptions ;
740
- reusedNames ?: readonly Entry [ ] ;
747
+ reusedNames : readonly Entry [ ] | undefined ;
748
+ ambientModuleNames ?: readonly Entry [ ] | undefined ,
741
749
perFileCache : Map < Path , ModeAwareCache < T > > ;
742
750
loader : ResolutionLoader < Entry , T , SourceFile > ;
743
751
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ;
@@ -747,7 +755,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
747
755
}
748
756
function resolveNamesWithLocalCache < Entry , SourceFile , T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > ( {
749
757
entries, containingFile, containingSourceFile, redirectedReference, options,
750
- perFileCache, reusedNames,
758
+ perFileCache, reusedNames, ambientModuleNames ,
751
759
loader, getResolutionWithResolvedFileName, deferWatchingNonRelativeResolution,
752
760
shouldRetryResolution, logChanges,
753
761
} : ResolveNamesWithLocalCacheInput < Entry , SourceFile , T , R > ) : readonly T [ ] {
@@ -820,20 +828,16 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
820
828
seenNamesInFile . set ( name , mode , true ) ;
821
829
resolvedModules . push ( resolution ) ;
822
830
}
823
- reusedNames ?. forEach ( entry => seenNamesInFile . set (
824
- loader . nameAndMode . getName ( entry ) ,
825
- loader . nameAndMode . getMode ( entry , containingSourceFile ) ,
826
- true ,
827
- ) ) ;
828
- if ( resolutionsInFile . size ( ) !== seenNamesInFile . size ( ) ) {
829
- // Stop watching and remove the unused name
830
- resolutionsInFile . forEach ( ( resolution , name , mode ) => {
831
- if ( ! seenNamesInFile . has ( name , mode ) ) {
832
- stopWatchFailedLookupLocationOfResolution ( resolution , path , getResolutionWithResolvedFileName ) ;
833
- resolutionsInFile . delete ( name , mode ) ;
834
- }
835
- } ) ;
836
- }
831
+ reuseResolutionsWorker (
832
+ reusedNames ,
833
+ containingSourceFile ,
834
+ path ,
835
+ resolutionsInFile ,
836
+ seenNamesInFile ,
837
+ loader . nameAndMode ,
838
+ getResolutionWithResolvedFileName ,
839
+ ambientModuleNames ,
840
+ ) ;
837
841
return resolvedModules ;
838
842
839
843
function resolutionIsEqualTo ( oldResolution : T | undefined , newResolution : T | undefined ) : boolean {
@@ -891,6 +895,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
891
895
options : CompilerOptions ,
892
896
containingSourceFile : SourceFile ,
893
897
reusedNames : readonly StringLiteralLike [ ] | undefined ,
898
+ ambientModuleNames : readonly StringLiteralLike [ ] | undefined ,
894
899
) : readonly ResolvedModuleWithFailedLookupLocations [ ] {
895
900
return resolveNamesWithLocalCache ( {
896
901
entries : moduleLiterals ,
@@ -899,6 +904,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
899
904
redirectedReference,
900
905
options,
901
906
reusedNames,
907
+ ambientModuleNames,
902
908
perFileCache : resolvedModuleNames ,
903
909
loader : createModuleResolutionLoaderUsingGlobalCache (
904
910
containingFile ,
@@ -952,6 +958,99 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
952
958
return resolution ;
953
959
}
954
960
961
+ function reuseResolutionsWorker < Entry , SourceFile , T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
962
+ reusedNames : readonly Entry [ ] | undefined ,
963
+ containingSourceFile : SourceFile ,
964
+ path : Path ,
965
+ resolutionsInFile : ModeAwareCache < T > ,
966
+ seenNamesInFile : ModeAwareCache < true > ,
967
+ nameAndModeGetter : ResolutionNameAndModeGetter < Entry , SourceFile > ,
968
+ getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
969
+ ambientModuleNames : readonly Entry [ ] | undefined ,
970
+ ) {
971
+ reusedNames ?. forEach ( entry => seenNamesInFile . set (
972
+ nameAndModeGetter . getName ( entry ) ,
973
+ nameAndModeGetter . getMode ( entry , containingSourceFile ) ,
974
+ true ,
975
+ ) ) ;
976
+ // For ambient module names, if its not invalidated keep it
977
+ ambientModuleNames ?. forEach ( entry => {
978
+ const name = nameAndModeGetter . getName ( entry ) ;
979
+ const mode = nameAndModeGetter . getMode ( entry , containingSourceFile ) ;
980
+ if ( ! seenNamesInFile . has ( name , mode ) ) {
981
+ const resolution = resolutionsInFile . get ( name , mode ) ;
982
+ // Keep this resolution from old time for ambient module names
983
+ if ( resolution && ! resolution . isInvalidated ) {
984
+ seenNamesInFile . set ( name , mode , true ) ;
985
+ }
986
+ }
987
+ } ) ;
988
+ if ( resolutionsInFile . size ( ) !== seenNamesInFile . size ( ) ) {
989
+ // Stop watching and remove the unused name
990
+ resolutionsInFile . forEach ( ( resolution , name , mode ) => {
991
+ if ( ! seenNamesInFile . has ( name , mode ) ) {
992
+ stopWatchFailedLookupLocationOfResolution ( resolution , path , getResolutionWithResolvedFileName ) ;
993
+ resolutionsInFile . delete ( name , mode ) ;
994
+ }
995
+ } ) ;
996
+ }
997
+ }
998
+
999
+ function reuseResolutions < Entry , SourceFile , T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
1000
+ reusedNames : readonly Entry [ ] | undefined ,
1001
+ containingSourceFile : SourceFile ,
1002
+ path : Path ,
1003
+ perFileCache : Map < Path , ModeAwareCache < T > > ,
1004
+ nameAndModeGetter : ResolutionNameAndModeGetter < Entry , SourceFile > ,
1005
+ getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
1006
+ ambientModuleNames ?: readonly Entry [ ] | undefined ,
1007
+ ) {
1008
+ const resolutionsInFile = perFileCache . get ( path ) ;
1009
+ if ( ! resolutionsInFile ) return ;
1010
+ const seenNamesInFile = createModeAwareCache < true > ( ) ;
1011
+ reuseResolutionsWorker (
1012
+ reusedNames ,
1013
+ containingSourceFile ,
1014
+ path , resolutionsInFile ,
1015
+ seenNamesInFile ,
1016
+ nameAndModeGetter ,
1017
+ getResolutionWithResolvedFileName ,
1018
+ ambientModuleNames ,
1019
+ ) ;
1020
+ }
1021
+
1022
+ function reusedModuleResolutions (
1023
+ reusedNames : readonly StringLiteralLike [ ] | undefined ,
1024
+ containingSourceFile : SourceFile ,
1025
+ ambientModuleNames : readonly StringLiteralLike [ ] | undefined ,
1026
+ ) {
1027
+ reuseResolutions (
1028
+ reusedNames ,
1029
+ containingSourceFile ,
1030
+ containingSourceFile . path ,
1031
+ resolvedModuleNames ,
1032
+ moduleResolutionNameAndModeGetter ,
1033
+ getResolvedModule ,
1034
+ ambientModuleNames ,
1035
+ ) ;
1036
+ }
1037
+
1038
+ function reusedTypeReferenceDirectiveResolutions < T extends FileReference | string > (
1039
+ reusedNames : readonly T [ ] | undefined ,
1040
+ containingSourceFile : SourceFile | undefined ,
1041
+ ) {
1042
+ reuseResolutions (
1043
+ reusedNames ,
1044
+ containingSourceFile ,
1045
+ containingSourceFile ?
1046
+ containingSourceFile . path :
1047
+ resolutionHost . toPath ( getAutomaticTypeDirectiveContainingFile ( resolutionHost . getCompilationSettings ( ) , getCurrentDirectory ( ) ) ) ,
1048
+ resolvedTypeReferenceDirectives ,
1049
+ typeReferenceResolutionNameAndModeGetter ,
1050
+ getResolvedTypeReferenceDirective ,
1051
+ ) ;
1052
+ }
1053
+
955
1054
function resolveSingleModuleNameWithoutWatching ( moduleName : string , containingFile : string ) {
956
1055
const path = resolutionHost . toPath ( containingFile ) ;
957
1056
const resolutionsInFile = resolvedModuleNames . get ( path ) ;
0 commit comments