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