4
4
clearMap ,
5
5
closeFileWatcher ,
6
6
closeFileWatcherOf ,
7
+ CompilerHostSupportingResolutionCache ,
7
8
CompilerOptions ,
8
9
createModeAwareCache ,
9
10
createModuleResolutionCache ,
@@ -21,6 +22,7 @@ import {
21
22
FileWatcher ,
22
23
FileWatcherCallback ,
23
24
firstDefinedIterator ,
25
+ getAutomaticTypeDirectiveContainingFile ,
24
26
GetCanonicalFileName ,
25
27
getDirectoryPath ,
26
28
getEffectiveTypeRoots ,
@@ -63,6 +65,7 @@ import {
63
65
resolutionExtensionIsTSOrJson ,
64
66
ResolutionLoader ,
65
67
ResolutionMode ,
68
+ ResolutionNameAndModeGetter ,
66
69
ResolutionWithResolvedFileName ,
67
70
ResolvedModuleWithFailedLookupLocations ,
68
71
ResolvedProjectReference ,
@@ -75,6 +78,7 @@ import {
75
78
startsWith ,
76
79
StringLiteralLike ,
77
80
trace ,
81
+ typeReferenceResolutionNameAndModeGetter ,
78
82
updateResolutionField ,
79
83
WatchDirectoryFlags ,
80
84
} from "./_namespaces/ts" ;
@@ -97,7 +101,7 @@ export type CallbackOnNewResolution<T extends ResolutionWithFailedLookupLocation
97
101
*
98
102
* @internal
99
103
*/
100
- export interface ResolutionCache {
104
+ export interface ResolutionCache extends Required < CompilerHostSupportingResolutionCache > {
101
105
rootDirForResolution : string ;
102
106
resolvedModuleNames : Map < Path , ModeAwareCache < CachedResolvedModuleWithFailedLookupLocations > > ;
103
107
resolvedTypeReferenceDirectives : Map < Path , ModeAwareCache < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > ;
@@ -125,6 +129,7 @@ export interface ResolutionCache {
125
129
options : CompilerOptions ,
126
130
containingSourceFile : SourceFile ,
127
131
reusedNames : readonly StringLiteralLike [ ] | undefined ,
132
+ ambientModuleNames : readonly StringLiteralLike [ ] | undefined ,
128
133
onNewResolution ?: CallbackOnNewResolution < ResolvedModuleWithFailedLookupLocations > ,
129
134
) : readonly ResolvedModuleWithFailedLookupLocations [ ] ;
130
135
resolveTypeReferenceDirectiveReferences < T extends FileReference | string > (
@@ -630,6 +635,8 @@ export function createResolutionCache(
630
635
finishCachingPerDirectoryResolution,
631
636
resolveModuleNameLiterals,
632
637
resolveTypeReferenceDirectiveReferences,
638
+ onReusedModuleResolutions,
639
+ onReusedTypeReferenceDirectiveResolutions,
633
640
resolveLibrary,
634
641
resolveSingleModuleNameWithoutWatching,
635
642
removeResolutionsFromProjectReferenceRedirects,
@@ -801,6 +808,7 @@ export function createResolutionCache(
801
808
redirectedReference : ResolvedProjectReference | undefined ;
802
809
options : CompilerOptions ;
803
810
reusedNames ?: readonly Entry [ ] ;
811
+ ambientEntries ?: readonly Entry [ ] ;
804
812
perFileCache : Map < Path , ModeAwareCache < T > > ;
805
813
loader : ResolutionLoader < Entry , T , SourceFile > ;
806
814
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ;
@@ -815,6 +823,7 @@ export function createResolutionCache(
815
823
options,
816
824
perFileCache,
817
825
reusedNames,
826
+ ambientEntries,
818
827
loader,
819
828
getResolutionWithResolvedFileName,
820
829
deferWatchingNonRelativeResolution,
@@ -889,23 +898,102 @@ export function createResolutionCache(
889
898
seenNamesInFile . set ( name , mode , true ) ;
890
899
resolvedModules . push ( resolution ) ;
891
900
}
901
+ onReusedResolutions ( {
902
+ reusedNames,
903
+ containingSourceFile,
904
+ ambientEntries,
905
+ path,
906
+ resolutionsInFile,
907
+ seenNamesInFile,
908
+ nameAndModeGetter : loader . nameAndMode ,
909
+ getResolutionWithResolvedFileName,
910
+ } ) ;
911
+ return resolvedModules ;
912
+ }
913
+
914
+ interface OnReusedResolutionsInput < Entry , SourceFile , T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > {
915
+ reusedNames : readonly Entry [ ] | undefined ;
916
+ containingSourceFile : SourceFile ;
917
+ ambientEntries ?: readonly Entry [ ] ;
918
+ path : Path ;
919
+ resolutionsInFile : ModeAwareCache < T > | undefined ;
920
+ seenNamesInFile ?: ModeAwareCache < true > ;
921
+ nameAndModeGetter : ResolutionNameAndModeGetter < Entry , SourceFile > ;
922
+ getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ;
923
+ }
924
+ function onReusedResolutions < Entry , SourceFile , T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > ( {
925
+ reusedNames,
926
+ containingSourceFile,
927
+ path,
928
+ resolutionsInFile,
929
+ seenNamesInFile,
930
+ nameAndModeGetter,
931
+ getResolutionWithResolvedFileName,
932
+ ambientEntries,
933
+ } : OnReusedResolutionsInput < Entry , SourceFile , T , R > ) {
934
+ if ( ! resolutionsInFile ) return ;
935
+ if ( ! seenNamesInFile ) seenNamesInFile = createModeAwareCache ( ) ;
892
936
reusedNames ?. forEach ( entry =>
893
- seenNamesInFile . set (
894
- loader . nameAndMode . getName ( entry ) ,
895
- loader . nameAndMode . getMode ( entry , containingSourceFile ) ,
937
+ seenNamesInFile ! . set (
938
+ nameAndModeGetter . getName ( entry ) ,
939
+ nameAndModeGetter . getMode ( entry , containingSourceFile ) ,
896
940
true ,
897
941
)
898
942
) ;
943
+ // For ambient module names, if its not invalidated keep it
944
+ ambientEntries ?. forEach ( entry => {
945
+ const name = nameAndModeGetter . getName ( entry ) ;
946
+ const mode = nameAndModeGetter . getMode ( entry , containingSourceFile ) ;
947
+ if ( ! seenNamesInFile ! . has ( name , mode ) ) {
948
+ const resolution = resolutionsInFile . get ( name , mode ) ;
949
+ // Keep this resolution from old time for ambient module names
950
+ if ( resolution && ! resolution . isInvalidated ) {
951
+ seenNamesInFile ! . set ( name , mode , true ) ;
952
+ }
953
+ }
954
+ } ) ;
899
955
if ( resolutionsInFile . size ( ) !== seenNamesInFile . size ( ) ) {
900
956
// Stop watching and remove the unused name
901
957
resolutionsInFile . forEach ( ( resolution , name , mode ) => {
902
- if ( ! seenNamesInFile . has ( name , mode ) ) {
958
+ if ( ! seenNamesInFile ! . has ( name , mode ) ) {
903
959
stopWatchFailedLookupLocationOfResolution ( resolution , path , getResolutionWithResolvedFileName ) ;
904
960
resolutionsInFile . delete ( name , mode ) ;
905
961
}
906
962
} ) ;
907
963
}
908
- return resolvedModules ;
964
+ }
965
+
966
+ function onReusedModuleResolutions (
967
+ reusedNames : readonly StringLiteralLike [ ] | undefined ,
968
+ containingSourceFile : SourceFile ,
969
+ ambientModuleNames : readonly StringLiteralLike [ ] | undefined ,
970
+ ) {
971
+ onReusedResolutions ( {
972
+ reusedNames,
973
+ containingSourceFile,
974
+ ambientEntries : ambientModuleNames ,
975
+ path : containingSourceFile . path ,
976
+ resolutionsInFile : resolvedModuleNames . get ( containingSourceFile . path ) ,
977
+ nameAndModeGetter : moduleResolutionNameAndModeGetter ,
978
+ getResolutionWithResolvedFileName : getResolvedModuleFromResolution ,
979
+ } ) ;
980
+ }
981
+
982
+ function onReusedTypeReferenceDirectiveResolutions < T extends FileReference | string > (
983
+ reusedNames : readonly T [ ] | undefined ,
984
+ containingSourceFile : SourceFile | undefined ,
985
+ ) {
986
+ const path = containingSourceFile ?
987
+ containingSourceFile . path :
988
+ resolutionHost . toPath ( getAutomaticTypeDirectiveContainingFile ( resolutionHost . getCompilationSettings ( ) , getCurrentDirectory ( ) ) ) ;
989
+ onReusedResolutions ( {
990
+ reusedNames,
991
+ containingSourceFile,
992
+ path,
993
+ resolutionsInFile : resolvedTypeReferenceDirectives . get ( path ) ,
994
+ nameAndModeGetter : typeReferenceResolutionNameAndModeGetter ,
995
+ getResolutionWithResolvedFileName : getResolvedTypeReferenceDirectiveFromResolution ,
996
+ } ) ;
909
997
}
910
998
911
999
function resolveTypeReferenceDirectiveReferences < T extends FileReference | string > (
@@ -943,6 +1031,7 @@ export function createResolutionCache(
943
1031
options : CompilerOptions ,
944
1032
containingSourceFile : SourceFile ,
945
1033
reusedNames : readonly StringLiteralLike [ ] | undefined ,
1034
+ ambientModuleNames : readonly StringLiteralLike [ ] | undefined ,
946
1035
onNewResolution ?: CallbackOnNewResolution < ResolvedModuleWithFailedLookupLocations > ,
947
1036
) : readonly ResolvedModuleWithFailedLookupLocations [ ] {
948
1037
return resolveNamesWithLocalCache ( {
@@ -952,6 +1041,7 @@ export function createResolutionCache(
952
1041
redirectedReference,
953
1042
options,
954
1043
reusedNames,
1044
+ ambientEntries : ambientModuleNames ,
955
1045
perFileCache : resolvedModuleNames ,
956
1046
loader : createModuleResolutionLoaderUsingGlobalCache (
957
1047
containingFile ,
0 commit comments