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,
@@ -741,7 +748,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
741
748
containingSourceFile : SourceFile ;
742
749
redirectedReference : ResolvedProjectReference | undefined ;
743
750
options : CompilerOptions ;
744
- reusedNames ?: readonly Entry [ ] ;
751
+ reusedNames : readonly Entry [ ] | undefined ;
752
+ ambientModuleNames ?: readonly Entry [ ] | undefined ,
745
753
perFileCache : Map < Path , ModeAwareCache < T > > ;
746
754
loader : ResolutionLoader < Entry , T , SourceFile > ;
747
755
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ;
@@ -751,7 +759,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
751
759
}
752
760
function resolveNamesWithLocalCache < Entry , SourceFile , T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > ( {
753
761
entries, containingFile, containingSourceFile, redirectedReference, options,
754
- perFileCache, reusedNames,
762
+ perFileCache, reusedNames, ambientModuleNames ,
755
763
loader, getResolutionWithResolvedFileName, deferWatchingNonRelativeResolution,
756
764
shouldRetryResolution, logChanges,
757
765
} : ResolveNamesWithLocalCacheInput < Entry , SourceFile , T , R > ) : readonly T [ ] {
@@ -824,20 +832,16 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
824
832
seenNamesInFile . set ( name , mode , true ) ;
825
833
resolvedModules . push ( resolution ) ;
826
834
}
827
- reusedNames ?. forEach ( entry => seenNamesInFile . set (
828
- loader . nameAndMode . getName ( entry ) ,
829
- loader . nameAndMode . getMode ( entry , containingSourceFile ) ,
830
- true ,
831
- ) ) ;
832
- if ( resolutionsInFile . size ( ) !== seenNamesInFile . size ( ) ) {
833
- // Stop watching and remove the unused name
834
- resolutionsInFile . forEach ( ( resolution , name , mode ) => {
835
- if ( ! seenNamesInFile . has ( name , mode ) ) {
836
- stopWatchFailedLookupLocationOfResolution ( resolution , path , getResolutionWithResolvedFileName ) ;
837
- resolutionsInFile . delete ( name , mode ) ;
838
- }
839
- } ) ;
840
- }
835
+ reuseResolutionsWorker (
836
+ reusedNames ,
837
+ containingSourceFile ,
838
+ path ,
839
+ resolutionsInFile ,
840
+ seenNamesInFile ,
841
+ loader . nameAndMode ,
842
+ getResolutionWithResolvedFileName ,
843
+ ambientModuleNames ,
844
+ ) ;
841
845
return resolvedModules ;
842
846
843
847
function resolutionIsEqualTo ( oldResolution : T | undefined , newResolution : T | undefined ) : boolean {
@@ -895,6 +899,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
895
899
options : CompilerOptions ,
896
900
containingSourceFile : SourceFile ,
897
901
reusedNames : readonly StringLiteralLike [ ] | undefined ,
902
+ ambientModuleNames : readonly StringLiteralLike [ ] | undefined ,
898
903
) : readonly ResolvedModuleWithFailedLookupLocations [ ] {
899
904
return resolveNamesWithLocalCache ( {
900
905
entries : moduleLiterals ,
@@ -903,6 +908,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
903
908
redirectedReference,
904
909
options,
905
910
reusedNames,
911
+ ambientModuleNames,
906
912
perFileCache : resolvedModuleNames ,
907
913
loader : createModuleResolutionLoaderUsingGlobalCache (
908
914
containingFile ,
@@ -956,6 +962,99 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
956
962
return resolution ;
957
963
}
958
964
965
+ function reuseResolutionsWorker < Entry , SourceFile , T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
966
+ reusedNames : readonly Entry [ ] | undefined ,
967
+ containingSourceFile : SourceFile ,
968
+ path : Path ,
969
+ resolutionsInFile : ModeAwareCache < T > ,
970
+ seenNamesInFile : ModeAwareCache < true > ,
971
+ nameAndModeGetter : ResolutionNameAndModeGetter < Entry , SourceFile > ,
972
+ getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
973
+ ambientModuleNames : readonly Entry [ ] | undefined ,
974
+ ) {
975
+ reusedNames ?. forEach ( entry => seenNamesInFile . set (
976
+ nameAndModeGetter . getName ( entry ) ,
977
+ nameAndModeGetter . getMode ( entry , containingSourceFile ) ,
978
+ true ,
979
+ ) ) ;
980
+ // For ambient module names, if its not invalidated keep it
981
+ ambientModuleNames ?. forEach ( entry => {
982
+ const name = nameAndModeGetter . getName ( entry ) ;
983
+ const mode = nameAndModeGetter . getMode ( entry , containingSourceFile ) ;
984
+ if ( ! seenNamesInFile . has ( name , mode ) ) {
985
+ const resolution = resolutionsInFile . get ( name , mode ) ;
986
+ // Keep this resolution from old time for ambient module names
987
+ if ( resolution && ! resolution . isInvalidated ) {
988
+ seenNamesInFile . set ( name , mode , true ) ;
989
+ }
990
+ }
991
+ } ) ;
992
+ if ( resolutionsInFile . size ( ) !== seenNamesInFile . size ( ) ) {
993
+ // Stop watching and remove the unused name
994
+ resolutionsInFile . forEach ( ( resolution , name , mode ) => {
995
+ if ( ! seenNamesInFile . has ( name , mode ) ) {
996
+ stopWatchFailedLookupLocationOfResolution ( resolution , path , getResolutionWithResolvedFileName ) ;
997
+ resolutionsInFile . delete ( name , mode ) ;
998
+ }
999
+ } ) ;
1000
+ }
1001
+ }
1002
+
1003
+ function reuseResolutions < Entry , SourceFile , T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
1004
+ reusedNames : readonly Entry [ ] | undefined ,
1005
+ containingSourceFile : SourceFile ,
1006
+ path : Path ,
1007
+ perFileCache : Map < Path , ModeAwareCache < T > > ,
1008
+ nameAndModeGetter : ResolutionNameAndModeGetter < Entry , SourceFile > ,
1009
+ getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
1010
+ ambientModuleNames ?: readonly Entry [ ] | undefined ,
1011
+ ) {
1012
+ const resolutionsInFile = perFileCache . get ( path ) ;
1013
+ if ( ! resolutionsInFile ) return ;
1014
+ const seenNamesInFile = createModeAwareCache < true > ( ) ;
1015
+ reuseResolutionsWorker (
1016
+ reusedNames ,
1017
+ containingSourceFile ,
1018
+ path , resolutionsInFile ,
1019
+ seenNamesInFile ,
1020
+ nameAndModeGetter ,
1021
+ getResolutionWithResolvedFileName ,
1022
+ ambientModuleNames ,
1023
+ ) ;
1024
+ }
1025
+
1026
+ function reusedModuleResolutions (
1027
+ reusedNames : readonly StringLiteralLike [ ] | undefined ,
1028
+ containingSourceFile : SourceFile ,
1029
+ ambientModuleNames : readonly StringLiteralLike [ ] | undefined ,
1030
+ ) {
1031
+ reuseResolutions (
1032
+ reusedNames ,
1033
+ containingSourceFile ,
1034
+ containingSourceFile . path ,
1035
+ resolvedModuleNames ,
1036
+ moduleResolutionNameAndModeGetter ,
1037
+ getResolvedModule ,
1038
+ ambientModuleNames ,
1039
+ ) ;
1040
+ }
1041
+
1042
+ function reusedTypeReferenceDirectiveResolutions < T extends FileReference | string > (
1043
+ reusedNames : readonly T [ ] | undefined ,
1044
+ containingSourceFile : SourceFile | undefined ,
1045
+ ) {
1046
+ reuseResolutions (
1047
+ reusedNames ,
1048
+ containingSourceFile ,
1049
+ containingSourceFile ?
1050
+ containingSourceFile . path :
1051
+ resolutionHost . toPath ( getAutomaticTypeDirectiveContainingFile ( resolutionHost . getCompilationSettings ( ) , getCurrentDirectory ( ) ) ) ,
1052
+ resolvedTypeReferenceDirectives ,
1053
+ typeReferenceResolutionNameAndModeGetter ,
1054
+ getResolvedTypeReferenceDirective ,
1055
+ ) ;
1056
+ }
1057
+
959
1058
function resolveSingleModuleNameWithoutWatching ( moduleName : string , containingFile : string ) {
960
1059
const path = resolutionHost . toPath ( containingFile ) ;
961
1060
const resolutionsInFile = resolvedModuleNames . get ( path ) ;
0 commit comments