@@ -403,10 +403,20 @@ namespace ts {
403
403
404
404
compilerHost . resolveModuleNames = maybeBind ( host , host . resolveModuleNames ) ;
405
405
compilerHost . resolveTypeReferenceDirectives = maybeBind ( host , host . resolveTypeReferenceDirectives ) ;
406
- let moduleResolutionCache = ! compilerHost . resolveModuleNames ? createModuleResolutionCache ( currentDirectory , getCanonicalFileName ) : undefined ;
406
+ const moduleResolutionCache = ! compilerHost . resolveModuleNames ? createModuleResolutionCache ( currentDirectory , getCanonicalFileName ) : undefined ;
407
+ let cacheState : {
408
+ originalReadFile : CompilerHost [ "readFile" ] ;
409
+ originalFileExists : CompilerHost [ "fileExists" ] ;
410
+ originalDirectoryExists : CompilerHost [ "directoryExists" ] ;
411
+ originalCreateDirectory : CompilerHost [ "createDirectory" ] ;
412
+ originalWriteFile : CompilerHost [ "writeFile" ] | undefined ;
413
+ originalReadFileWithCache : CompilerHost [ "readFile" ] ;
414
+ originalGetSourceFile : CompilerHost [ "getSourceFile" ] ;
415
+ originalResolveModuleNames : CompilerHost [ "resolveModuleNames" ] ;
416
+ } | undefined ;
407
417
408
418
const buildInfoChecked = createFileMap < true > ( toPath ) ;
409
- let extendedConfigCache : Map < ExtendedConfigCacheEntry > | undefined ;
419
+ const extendedConfigCache = createMap < ExtendedConfigCacheEntry > ( ) ;
410
420
411
421
// Watch state
412
422
const builderPrograms = createFileMap < T > ( toPath ) ;
@@ -869,6 +879,7 @@ namespace ts {
869
879
diagnostics . removeKey ( resolved ) ;
870
880
871
881
addProjToQueue ( resolved , reloadLevel ) ;
882
+ enableCache ( ) ;
872
883
}
873
884
874
885
/**
@@ -929,6 +940,7 @@ namespace ts {
929
940
}
930
941
}
931
942
else {
943
+ disableCache ( ) ;
932
944
reportErrorSummary ( ) ;
933
945
}
934
946
}
@@ -1384,20 +1396,20 @@ namespace ts {
1384
1396
return configFileNames . map ( resolveProjectName ) ;
1385
1397
}
1386
1398
1387
- function buildAllProjects ( ) : ExitStatus {
1388
- if ( options . watch ) { reportWatchStatus ( Diagnostics . Starting_compilation_in_watch_mode ) ; }
1389
- // TODO:: In watch mode as well to use caches for incremental build once we can invalidate caches correctly and have right api
1390
- // Override readFile for json files and output .d.ts to cache the text
1391
- const savedReadFileWithCache = readFileWithCache ;
1392
- const savedGetSourceFile = compilerHost . getSourceFile ;
1399
+ function enableCache ( ) {
1400
+ if ( cacheState ) {
1401
+ disableCache ( ) ;
1402
+ }
1403
+
1404
+ const originalReadFileWithCache = readFileWithCache ;
1405
+ const originalGetSourceFile = compilerHost . getSourceFile ;
1393
1406
1394
1407
const { originalReadFile, originalFileExists, originalDirectoryExists,
1395
1408
originalCreateDirectory, originalWriteFile, getSourceFileWithCache,
1396
1409
readFileWithCache : newReadFileWithCache
1397
- } = changeCompilerHostLikeToUseCache ( host , toPath , ( ...args ) => savedGetSourceFile . call ( compilerHost , ...args ) ) ;
1410
+ } = changeCompilerHostLikeToUseCache ( host , toPath , ( ...args ) => originalGetSourceFile . call ( compilerHost , ...args ) ) ;
1398
1411
readFileWithCache = newReadFileWithCache ;
1399
1412
compilerHost . getSourceFile = getSourceFileWithCache ! ;
1400
- extendedConfigCache = createMap ( ) ;
1401
1413
1402
1414
const originalResolveModuleNames = compilerHost . resolveModuleNames ;
1403
1415
if ( ! compilerHost . resolveModuleNames ) {
@@ -1406,6 +1418,41 @@ namespace ts {
1406
1418
loadWithLocalCache < ResolvedModuleFull > ( Debug . assertEachDefined ( moduleNames ) , containingFile , redirectedReference , loader ) ;
1407
1419
}
1408
1420
1421
+ cacheState = {
1422
+ originalReadFile,
1423
+ originalFileExists,
1424
+ originalDirectoryExists,
1425
+ originalCreateDirectory,
1426
+ originalWriteFile,
1427
+ originalReadFileWithCache,
1428
+ originalGetSourceFile,
1429
+ originalResolveModuleNames
1430
+ } ;
1431
+ }
1432
+
1433
+ function disableCache ( ) {
1434
+ if ( ! cacheState ) return ;
1435
+
1436
+ host . readFile = cacheState . originalReadFile ;
1437
+ host . fileExists = cacheState . originalFileExists ;
1438
+ host . directoryExists = cacheState . originalDirectoryExists ;
1439
+ host . createDirectory = cacheState . originalCreateDirectory ;
1440
+ host . writeFile = cacheState . originalWriteFile ;
1441
+ compilerHost . getSourceFile = cacheState . originalGetSourceFile ;
1442
+ readFileWithCache = cacheState . originalReadFileWithCache ;
1443
+ compilerHost . resolveModuleNames = cacheState . originalResolveModuleNames ;
1444
+ extendedConfigCache . clear ( ) ;
1445
+ if ( moduleResolutionCache ) {
1446
+ moduleResolutionCache . directoryToModuleNameMap . clear ( ) ;
1447
+ moduleResolutionCache . moduleNameToDirectoryMap . clear ( ) ;
1448
+ }
1449
+ cacheState = undefined ;
1450
+ }
1451
+
1452
+ function buildAllProjects ( ) : ExitStatus {
1453
+ if ( options . watch ) { reportWatchStatus ( Diagnostics . Starting_compilation_in_watch_mode ) ; }
1454
+ enableCache ( ) ;
1455
+
1409
1456
const graph = getGlobalDependencyGraph ( ) ;
1410
1457
reportBuildQueue ( graph ) ;
1411
1458
let anyFailed = false ;
@@ -1459,16 +1506,7 @@ namespace ts {
1459
1506
anyFailed = anyFailed || ! ! ( buildResult & BuildResultFlags . AnyErrors ) ;
1460
1507
}
1461
1508
reportErrorSummary ( ) ;
1462
- host . readFile = originalReadFile ;
1463
- host . fileExists = originalFileExists ;
1464
- host . directoryExists = originalDirectoryExists ;
1465
- host . createDirectory = originalCreateDirectory ;
1466
- host . writeFile = originalWriteFile ;
1467
- compilerHost . getSourceFile = savedGetSourceFile ;
1468
- readFileWithCache = savedReadFileWithCache ;
1469
- extendedConfigCache = undefined ;
1470
- compilerHost . resolveModuleNames = originalResolveModuleNames ;
1471
- moduleResolutionCache = undefined ;
1509
+ disableCache ( ) ;
1472
1510
return anyFailed ? ExitStatus . DiagnosticsPresent_OutputsSkipped : ExitStatus . Success ;
1473
1511
}
1474
1512
0 commit comments