Skip to content

Commit 5940eb8

Browse files
committed
Enable caches while watch mode compilation
1 parent 94ee0dc commit 5940eb8

File tree

1 file changed

+56
-17
lines changed

1 file changed

+56
-17
lines changed

src/compiler/tsbuild.ts

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,17 @@ namespace ts {
402402

403403
compilerHost.resolveModuleNames = maybeBind(host, host.resolveModuleNames);
404404
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;
406416

407417
const buildInfoChecked = createFileMap<true>(toPath);
408418

@@ -867,6 +877,7 @@ namespace ts {
867877
diagnostics.removeKey(resolved);
868878

869879
addProjToQueue(resolved, reloadLevel);
880+
enableCache();
870881
}
871882

872883
/**
@@ -927,6 +938,7 @@ namespace ts {
927938
}
928939
}
929940
else {
941+
disableCache();
930942
reportErrorSummary();
931943
}
932944
}
@@ -1382,17 +1394,18 @@ namespace ts {
13821394
return configFileNames.map(resolveProjectName);
13831395
}
13841396

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;
13911404

13921405
const { originalReadFile, originalFileExists, originalDirectoryExists,
13931406
originalCreateDirectory, originalWriteFile, getSourceFileWithCache,
13941407
readFileWithCache: newReadFileWithCache
1395-
} = changeCompilerHostLikeToUseCache(host, toPath, (...args) => savedGetSourceFile.call(compilerHost, ...args));
1408+
} = changeCompilerHostLikeToUseCache(host, toPath, (...args) => originalGetSourceFile.call(compilerHost, ...args));
13961409
readFileWithCache = newReadFileWithCache;
13971410
compilerHost.getSourceFile = getSourceFileWithCache!;
13981411

@@ -1403,6 +1416,40 @@ namespace ts {
14031416
loadWithLocalCache<ResolvedModuleFull>(Debug.assertEachDefined(moduleNames), containingFile, redirectedReference, loader);
14041417
}
14051418

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+
14061453
const graph = getGlobalDependencyGraph();
14071454
reportBuildQueue(graph);
14081455
let anyFailed = false;
@@ -1456,15 +1503,7 @@ namespace ts {
14561503
anyFailed = anyFailed || !!(buildResult & BuildResultFlags.AnyErrors);
14571504
}
14581505
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();
14681507
return anyFailed ? ExitStatus.DiagnosticsPresent_OutputsSkipped : ExitStatus.Success;
14691508
}
14701509

0 commit comments

Comments
 (0)