@@ -10,6 +10,7 @@ import {
10
10
CustomResolveModuleName ,
11
11
CustomResolveTypeReferenceDirective ,
12
12
FilePathKey ,
13
+ ModuleResolutionCache ,
13
14
ModuleResolutionHostMayBeCacheable ,
14
15
ResolvedModule ,
15
16
ServiceHostWhichMayBeCacheable ,
@@ -247,39 +248,19 @@ function makeResolvers<T extends typescript.ModuleResolutionHost>(
247
248
scriptRegex : RegExp ,
248
249
instance : TSInstance
249
250
) {
250
- const resolveTypeReferenceDirective = makeResolveTypeReferenceDirective (
251
- compiler ,
252
- compilerOptions ,
253
- moduleResolutionHost ,
254
- customResolveTypeReferenceDirective
255
- ) ;
256
-
257
- const resolveTypeReferenceDirectives = (
258
- typeDirectiveNames : string [ ] ,
259
- containingFile : string ,
260
- _redirectedReference ?: typescript . ResolvedProjectReference
261
- ) : ( typescript . ResolvedTypeReferenceDirective | undefined ) [ ] =>
262
- typeDirectiveNames . map (
263
- directive =>
264
- resolveTypeReferenceDirective (
265
- directive ,
266
- containingFile ,
267
- _redirectedReference
268
- ) . resolvedTypeReferenceDirective
269
- ) ;
270
-
271
251
const resolveModuleName = makeResolveModuleName (
272
252
compiler ,
273
253
compilerOptions ,
274
254
moduleResolutionHost ,
275
- customResolveModuleName
255
+ customResolveModuleName ,
256
+ instance
276
257
) ;
277
258
278
259
const resolveModuleNames = (
279
260
moduleNames : string [ ] ,
280
261
containingFile : string ,
281
262
_reusedNames ?: string [ ] | undefined ,
282
- _redirectedReference ?: typescript . ResolvedProjectReference | undefined
263
+ redirectedReference ?: typescript . ResolvedProjectReference | undefined
283
264
) : ( typescript . ResolvedModule | undefined ) [ ] => {
284
265
const resolvedModules = moduleNames . map ( moduleName =>
285
266
resolveModule (
@@ -288,7 +269,8 @@ function makeResolvers<T extends typescript.ModuleResolutionHost>(
288
269
appendTsTsxSuffixesIfRequired ,
289
270
scriptRegex ,
290
271
moduleName ,
291
- containingFile
272
+ containingFile ,
273
+ redirectedReference
292
274
)
293
275
) ;
294
276
@@ -297,6 +279,28 @@ function makeResolvers<T extends typescript.ModuleResolutionHost>(
297
279
return resolvedModules ;
298
280
} ;
299
281
282
+ const resolveTypeReferenceDirective = makeResolveTypeReferenceDirective (
283
+ compiler ,
284
+ compilerOptions ,
285
+ moduleResolutionHost ,
286
+ customResolveTypeReferenceDirective ,
287
+ instance
288
+ ) ;
289
+
290
+ const resolveTypeReferenceDirectives = (
291
+ typeDirectiveNames : string [ ] ,
292
+ containingFile : string ,
293
+ redirectedReference ?: typescript . ResolvedProjectReference
294
+ ) : ( typescript . ResolvedTypeReferenceDirective | undefined ) [ ] =>
295
+ typeDirectiveNames . map (
296
+ directive =>
297
+ resolveTypeReferenceDirective (
298
+ directive ,
299
+ containingFile ,
300
+ redirectedReference
301
+ ) . resolvedTypeReferenceDirective
302
+ ) ;
303
+
300
304
return {
301
305
resolveTypeReferenceDirectives,
302
306
resolveModuleNames,
@@ -492,7 +496,7 @@ export function makeWatchHost(
492
496
fileName =>
493
497
files . has ( filePathKeyMapper ( fileName ) ) ||
494
498
compiler . sys . fileExists ( fileName ) ,
495
- /*enabledCaching*/ false
499
+ instance . loaderOptions . experimentalFileCaching
496
500
) ;
497
501
498
502
const watchHost : WatchHost = {
@@ -600,6 +604,60 @@ export function makeWatchHost(
600
604
601
605
const missingFileModifiedTime = new Date ( 0 ) ;
602
606
607
+ function identity < T > ( x : T ) {
608
+ return x ;
609
+ }
610
+ function toLowerCase ( x : string ) {
611
+ return x . toLowerCase ( ) ;
612
+ }
613
+ const fileNameLowerCaseRegExp = / [ ^ \u0130 \u0131 \u00DF a - z 0 - 9 \\ / : \- _ \. ] + / g;
614
+ function toFileNameLowerCase ( x : string ) {
615
+ return fileNameLowerCaseRegExp . test ( x )
616
+ ? x . replace ( fileNameLowerCaseRegExp , toLowerCase )
617
+ : x ;
618
+ }
619
+ function createGetCanonicalFileName ( instance : TSInstance ) {
620
+ return useCaseSensitiveFileNames ( instance . compiler , instance . loaderOptions )
621
+ ? identity
622
+ : toFileNameLowerCase ;
623
+ }
624
+
625
+ function createModuleResolutionCache (
626
+ instance : TSInstance ,
627
+ moduleResolutionHost : typescript . ModuleResolutionHost
628
+ ) : ModuleResolutionCache {
629
+ const cache = instance . compiler . createModuleResolutionCache (
630
+ moduleResolutionHost . getCurrentDirectory ! ( ) ,
631
+ createGetCanonicalFileName ( instance ) ,
632
+ instance . compilerOptions
633
+ ) as ModuleResolutionCache ;
634
+ // Add new API optional methods
635
+ if ( ! cache . clear ) {
636
+ cache . clear = ( ) => {
637
+ cache . directoryToModuleNameMap . clear ( ) ;
638
+ cache . moduleNameToDirectoryMap . clear ( ) ;
639
+ } ;
640
+ }
641
+ if ( ! cache . update ) {
642
+ cache . update = options => {
643
+ if ( ! options . configFile ) return ;
644
+ const ref : typescript . ResolvedProjectReference = {
645
+ sourceFile : options . configFile ! as typescript . TsConfigSourceFile ,
646
+ commandLine : { options } as typescript . ParsedCommandLine ,
647
+ } ;
648
+ cache . directoryToModuleNameMap . setOwnMap (
649
+ cache . directoryToModuleNameMap . getOrCreateMapOfCacheRedirects ( ref )
650
+ ) ;
651
+ cache . moduleNameToDirectoryMap . setOwnMap (
652
+ cache . moduleNameToDirectoryMap . getOrCreateMapOfCacheRedirects ( ref )
653
+ ) ;
654
+ cache . directoryToModuleNameMap . setOwnOptions ( options ) ;
655
+ cache . moduleNameToDirectoryMap . setOwnOptions ( options ) ;
656
+ } ;
657
+ }
658
+ return cache ;
659
+ }
660
+
603
661
/**
604
662
* Create the TypeScript Watch host
605
663
*/
@@ -617,12 +675,7 @@ export function makeSolutionBuilderHost(
617
675
// loader.context seems to work fine on Linux / Mac regardless causes problems for @types resolution on Windows for TypeScript < 2.3
618
676
const formatDiagnosticHost : typescript . FormatDiagnosticsHost = {
619
677
getCurrentDirectory : compiler . sys . getCurrentDirectory ,
620
- getCanonicalFileName : useCaseSensitiveFileNames (
621
- compiler ,
622
- instance . loaderOptions
623
- )
624
- ? s => s
625
- : s => s . toLowerCase ( ) ,
678
+ getCanonicalFileName : createGetCanonicalFileName ( instance ) ,
626
679
getNewLine : ( ) => compiler . sys . newLine ,
627
680
} ;
628
681
@@ -705,6 +758,28 @@ export function makeSolutionBuilderHost(
705
758
const solutionBuilderHost : SolutionBuilderWithWatchHost = {
706
759
...sysHost ,
707
760
...moduleResolutionHost ,
761
+ createProgram : (
762
+ rootNames ,
763
+ options ,
764
+ host ,
765
+ oldProgram ,
766
+ configFileParsingDiagnostics ,
767
+ projectReferences
768
+ ) => {
769
+ instance . moduleResolutionCache ?. update ( options || { } ) ;
770
+ instance . typeReferenceResolutionCache ?. update ( options || { } ) ;
771
+ const result = sysHost . createProgram (
772
+ rootNames ,
773
+ options ,
774
+ host ,
775
+ oldProgram ,
776
+ configFileParsingDiagnostics ,
777
+ projectReferences
778
+ ) ;
779
+ instance . typeReferenceResolutionCache ?. update ( instance . compilerOptions ) ;
780
+ instance . moduleResolutionCache ?. update ( instance . compilerOptions ) ;
781
+ return result ;
782
+ } ,
708
783
resolveModuleNames,
709
784
resolveTypeReferenceDirectives,
710
785
diagnostics,
@@ -1091,16 +1166,31 @@ function makeResolveTypeReferenceDirective(
1091
1166
moduleResolutionHost : typescript . ModuleResolutionHost ,
1092
1167
customResolveTypeReferenceDirective :
1093
1168
| CustomResolveTypeReferenceDirective
1094
- | undefined
1169
+ | undefined ,
1170
+ instance : TSInstance
1095
1171
) : ResolveTypeReferenceDirective {
1096
1172
if ( customResolveTypeReferenceDirective === undefined ) {
1173
+ // Until the api is published
1174
+ if (
1175
+ ( compiler as any ) . createTypeReferenceDirectiveResolutionCache &&
1176
+ ! instance . typeReferenceResolutionCache
1177
+ ) {
1178
+ instance . typeReferenceResolutionCache = ( compiler as any ) . createTypeReferenceDirectiveResolutionCache (
1179
+ moduleResolutionHost . getCurrentDirectory ! ( ) ,
1180
+ createGetCanonicalFileName ( instance ) ,
1181
+ instance . compilerOptions ,
1182
+ instance . moduleResolutionCache ?. getPackageJsonInfoCache ?.( )
1183
+ ) ;
1184
+ }
1097
1185
return ( directive , containingFile , redirectedReference ) =>
1098
- compiler . resolveTypeReferenceDirective (
1186
+ // Until the api is published
1187
+ ( compiler . resolveTypeReferenceDirective as any ) (
1099
1188
directive ,
1100
1189
containingFile ,
1101
1190
compilerOptions ,
1102
1191
moduleResolutionHost ,
1103
- redirectedReference
1192
+ redirectedReference ,
1193
+ instance . typeReferenceResolutionCache
1104
1194
) ;
1105
1195
}
1106
1196
@@ -1130,7 +1220,8 @@ function resolveModule(
1130
1220
appendTsTsxSuffixesIfRequired : ( filePath : string ) => string ,
1131
1221
scriptRegex : RegExp ,
1132
1222
moduleName : string ,
1133
- containingFile : string
1223
+ containingFile : string ,
1224
+ redirectedReference : typescript . ResolvedProjectReference | undefined
1134
1225
) {
1135
1226
let resolutionResult : ResolvedModule ;
1136
1227
@@ -1150,16 +1241,19 @@ function resolveModule(
1150
1241
}
1151
1242
} catch ( e ) { }
1152
1243
1153
- const tsResolution = resolveModuleName ( moduleName , containingFile ) ;
1244
+ const tsResolution = resolveModuleName (
1245
+ moduleName ,
1246
+ containingFile ,
1247
+ redirectedReference
1248
+ ) ;
1154
1249
if ( tsResolution . resolvedModule !== undefined ) {
1155
1250
const resolvedFileName = path . normalize (
1156
1251
tsResolution . resolvedModule . resolvedFileName
1157
1252
) ;
1158
1253
const tsResolutionResult : ResolvedModule = {
1254
+ ...tsResolution . resolvedModule ,
1159
1255
originalFileName : resolvedFileName ,
1160
1256
resolvedFileName,
1161
- isExternalLibraryImport :
1162
- tsResolution . resolvedModule . isExternalLibraryImport ,
1163
1257
} ;
1164
1258
1165
1259
return resolutionResult ! === undefined ||
@@ -1174,26 +1268,36 @@ function resolveModule(
1174
1268
1175
1269
type ResolveModuleName = (
1176
1270
moduleName : string ,
1177
- containingFile : string
1271
+ containingFile : string ,
1272
+ redirectedReference : typescript . ResolvedProjectReference | undefined
1178
1273
) => typescript . ResolvedModuleWithFailedLookupLocations ;
1179
1274
1180
1275
function makeResolveModuleName (
1181
1276
compiler : typeof typescript ,
1182
1277
compilerOptions : typescript . CompilerOptions ,
1183
1278
moduleResolutionHost : typescript . ModuleResolutionHost ,
1184
- customResolveModuleName : CustomResolveModuleName | undefined
1279
+ customResolveModuleName : CustomResolveModuleName | undefined ,
1280
+ instance : TSInstance
1185
1281
) : ResolveModuleName {
1186
1282
if ( customResolveModuleName === undefined ) {
1187
- return ( moduleName : string , containingFile : string ) =>
1283
+ if ( ! instance . moduleResolutionCache ) {
1284
+ instance . moduleResolutionCache = createModuleResolutionCache (
1285
+ instance ,
1286
+ moduleResolutionHost
1287
+ ) ;
1288
+ }
1289
+ return ( moduleName , containingFile , redirectedReference ) =>
1188
1290
compiler . resolveModuleName (
1189
1291
moduleName ,
1190
1292
containingFile ,
1191
1293
compilerOptions ,
1192
- moduleResolutionHost
1294
+ moduleResolutionHost ,
1295
+ instance . moduleResolutionCache ,
1296
+ redirectedReference
1193
1297
) ;
1194
1298
}
1195
1299
1196
- return ( moduleName : string , containingFile : string ) =>
1300
+ return ( moduleName , containingFile ) =>
1197
1301
customResolveModuleName (
1198
1302
moduleName ,
1199
1303
containingFile ,
0 commit comments