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