@@ -403,10 +403,20 @@ namespace ts {
403403
404404 compilerHost . resolveModuleNames = maybeBind ( host , host . resolveModuleNames ) ;
405405 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 ;
407417
408418 const buildInfoChecked = createFileMap < true > ( toPath ) ;
409- let extendedConfigCache : Map < ExtendedConfigCacheEntry > | undefined ;
419+ const extendedConfigCache = createMap < ExtendedConfigCacheEntry > ( ) ;
410420
411421 // Watch state
412422 const builderPrograms = createFileMap < T > ( toPath ) ;
@@ -869,6 +879,7 @@ namespace ts {
869879 diagnostics . removeKey ( resolved ) ;
870880
871881 addProjToQueue ( resolved , reloadLevel ) ;
882+ enableCache ( ) ;
872883 }
873884
874885 /**
@@ -929,6 +940,7 @@ namespace ts {
929940 }
930941 }
931942 else {
943+ disableCache ( ) ;
932944 reportErrorSummary ( ) ;
933945 }
934946 }
@@ -1384,20 +1396,20 @@ namespace ts {
13841396 return configFileNames . map ( resolveProjectName ) ;
13851397 }
13861398
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 ;
13931406
13941407 const { originalReadFile, originalFileExists, originalDirectoryExists,
13951408 originalCreateDirectory, originalWriteFile, getSourceFileWithCache,
13961409 readFileWithCache : newReadFileWithCache
1397- } = changeCompilerHostLikeToUseCache ( host , toPath , ( ...args ) => savedGetSourceFile . call ( compilerHost , ...args ) ) ;
1410+ } = changeCompilerHostLikeToUseCache ( host , toPath , ( ...args ) => originalGetSourceFile . call ( compilerHost , ...args ) ) ;
13981411 readFileWithCache = newReadFileWithCache ;
13991412 compilerHost . getSourceFile = getSourceFileWithCache ! ;
1400- extendedConfigCache = createMap ( ) ;
14011413
14021414 const originalResolveModuleNames = compilerHost . resolveModuleNames ;
14031415 if ( ! compilerHost . resolveModuleNames ) {
@@ -1406,6 +1418,41 @@ namespace ts {
14061418 loadWithLocalCache < ResolvedModuleFull > ( Debug . assertEachDefined ( moduleNames ) , containingFile , redirectedReference , loader ) ;
14071419 }
14081420
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+
14091456 const graph = getGlobalDependencyGraph ( ) ;
14101457 reportBuildQueue ( graph ) ;
14111458 let anyFailed = false ;
@@ -1459,16 +1506,7 @@ namespace ts {
14591506 anyFailed = anyFailed || ! ! ( buildResult & BuildResultFlags . AnyErrors ) ;
14601507 }
14611508 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 ( ) ;
14721510 return anyFailed ? ExitStatus . DiagnosticsPresent_OutputsSkipped : ExitStatus . Success ;
14731511 }
14741512
0 commit comments