Merge pull request #31128 from Microsoft/buildWatchCaching
Use caches similar to buildAll when building incrementally with in watch mode of tsbuild
This commit is contained in:
commit
66bdc83005
1 changed files with 58 additions and 20 deletions
|
@ -403,10 +403,20 @@ namespace ts {
|
||||||
|
|
||||||
compilerHost.resolveModuleNames = maybeBind(host, host.resolveModuleNames);
|
compilerHost.resolveModuleNames = maybeBind(host, host.resolveModuleNames);
|
||||||
compilerHost.resolveTypeReferenceDirectives = maybeBind(host, host.resolveTypeReferenceDirectives);
|
compilerHost.resolveTypeReferenceDirectives = maybeBind(host, host.resolveTypeReferenceDirectives);
|
||||||
let moduleResolutionCache = !compilerHost.resolveModuleNames ? createModuleResolutionCache(currentDirectory, getCanonicalFileName) : undefined;
|
const moduleResolutionCache = !compilerHost.resolveModuleNames ? createModuleResolutionCache(currentDirectory, getCanonicalFileName) : undefined;
|
||||||
|
let cacheState: {
|
||||||
|
originalReadFile: CompilerHost["readFile"];
|
||||||
|
originalFileExists: CompilerHost["fileExists"];
|
||||||
|
originalDirectoryExists: CompilerHost["directoryExists"];
|
||||||
|
originalCreateDirectory: CompilerHost["createDirectory"];
|
||||||
|
originalWriteFile: CompilerHost["writeFile"] | undefined;
|
||||||
|
originalReadFileWithCache: CompilerHost["readFile"];
|
||||||
|
originalGetSourceFile: CompilerHost["getSourceFile"];
|
||||||
|
originalResolveModuleNames: CompilerHost["resolveModuleNames"];
|
||||||
|
} | undefined;
|
||||||
|
|
||||||
const buildInfoChecked = createFileMap<true>(toPath);
|
const buildInfoChecked = createFileMap<true>(toPath);
|
||||||
let extendedConfigCache: Map<ExtendedConfigCacheEntry> | undefined;
|
const extendedConfigCache = createMap<ExtendedConfigCacheEntry>();
|
||||||
|
|
||||||
// Watch state
|
// Watch state
|
||||||
const builderPrograms = createFileMap<T>(toPath);
|
const builderPrograms = createFileMap<T>(toPath);
|
||||||
|
@ -869,6 +879,7 @@ namespace ts {
|
||||||
diagnostics.removeKey(resolved);
|
diagnostics.removeKey(resolved);
|
||||||
|
|
||||||
addProjToQueue(resolved, reloadLevel);
|
addProjToQueue(resolved, reloadLevel);
|
||||||
|
enableCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -929,6 +940,7 @@ namespace ts {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
disableCache();
|
||||||
reportErrorSummary();
|
reportErrorSummary();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1384,20 +1396,20 @@ namespace ts {
|
||||||
return configFileNames.map(resolveProjectName);
|
return configFileNames.map(resolveProjectName);
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildAllProjects(): ExitStatus {
|
function enableCache() {
|
||||||
if (options.watch) { reportWatchStatus(Diagnostics.Starting_compilation_in_watch_mode); }
|
if (cacheState) {
|
||||||
// TODO:: In watch mode as well to use caches for incremental build once we can invalidate caches correctly and have right api
|
disableCache();
|
||||||
// Override readFile for json files and output .d.ts to cache the text
|
}
|
||||||
const savedReadFileWithCache = readFileWithCache;
|
|
||||||
const savedGetSourceFile = compilerHost.getSourceFile;
|
const originalReadFileWithCache = readFileWithCache;
|
||||||
|
const originalGetSourceFile = compilerHost.getSourceFile;
|
||||||
|
|
||||||
const { originalReadFile, originalFileExists, originalDirectoryExists,
|
const { originalReadFile, originalFileExists, originalDirectoryExists,
|
||||||
originalCreateDirectory, originalWriteFile, getSourceFileWithCache,
|
originalCreateDirectory, originalWriteFile, getSourceFileWithCache,
|
||||||
readFileWithCache: newReadFileWithCache
|
readFileWithCache: newReadFileWithCache
|
||||||
} = changeCompilerHostLikeToUseCache(host, toPath, (...args) => savedGetSourceFile.call(compilerHost, ...args));
|
} = changeCompilerHostLikeToUseCache(host, toPath, (...args) => originalGetSourceFile.call(compilerHost, ...args));
|
||||||
readFileWithCache = newReadFileWithCache;
|
readFileWithCache = newReadFileWithCache;
|
||||||
compilerHost.getSourceFile = getSourceFileWithCache!;
|
compilerHost.getSourceFile = getSourceFileWithCache!;
|
||||||
extendedConfigCache = createMap();
|
|
||||||
|
|
||||||
const originalResolveModuleNames = compilerHost.resolveModuleNames;
|
const originalResolveModuleNames = compilerHost.resolveModuleNames;
|
||||||
if (!compilerHost.resolveModuleNames) {
|
if (!compilerHost.resolveModuleNames) {
|
||||||
|
@ -1406,6 +1418,41 @@ namespace ts {
|
||||||
loadWithLocalCache<ResolvedModuleFull>(Debug.assertEachDefined(moduleNames), containingFile, redirectedReference, loader);
|
loadWithLocalCache<ResolvedModuleFull>(Debug.assertEachDefined(moduleNames), containingFile, redirectedReference, loader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cacheState = {
|
||||||
|
originalReadFile,
|
||||||
|
originalFileExists,
|
||||||
|
originalDirectoryExists,
|
||||||
|
originalCreateDirectory,
|
||||||
|
originalWriteFile,
|
||||||
|
originalReadFileWithCache,
|
||||||
|
originalGetSourceFile,
|
||||||
|
originalResolveModuleNames
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function disableCache() {
|
||||||
|
if (!cacheState) return;
|
||||||
|
|
||||||
|
host.readFile = cacheState.originalReadFile;
|
||||||
|
host.fileExists = cacheState.originalFileExists;
|
||||||
|
host.directoryExists = cacheState.originalDirectoryExists;
|
||||||
|
host.createDirectory = cacheState.originalCreateDirectory;
|
||||||
|
host.writeFile = cacheState.originalWriteFile;
|
||||||
|
compilerHost.getSourceFile = cacheState.originalGetSourceFile;
|
||||||
|
readFileWithCache = cacheState.originalReadFileWithCache;
|
||||||
|
compilerHost.resolveModuleNames = cacheState.originalResolveModuleNames;
|
||||||
|
extendedConfigCache.clear();
|
||||||
|
if (moduleResolutionCache) {
|
||||||
|
moduleResolutionCache.directoryToModuleNameMap.clear();
|
||||||
|
moduleResolutionCache.moduleNameToDirectoryMap.clear();
|
||||||
|
}
|
||||||
|
cacheState = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildAllProjects(): ExitStatus {
|
||||||
|
if (options.watch) { reportWatchStatus(Diagnostics.Starting_compilation_in_watch_mode); }
|
||||||
|
enableCache();
|
||||||
|
|
||||||
const graph = getGlobalDependencyGraph();
|
const graph = getGlobalDependencyGraph();
|
||||||
reportBuildQueue(graph);
|
reportBuildQueue(graph);
|
||||||
let anyFailed = false;
|
let anyFailed = false;
|
||||||
|
@ -1459,16 +1506,7 @@ namespace ts {
|
||||||
anyFailed = anyFailed || !!(buildResult & BuildResultFlags.AnyErrors);
|
anyFailed = anyFailed || !!(buildResult & BuildResultFlags.AnyErrors);
|
||||||
}
|
}
|
||||||
reportErrorSummary();
|
reportErrorSummary();
|
||||||
host.readFile = originalReadFile;
|
disableCache();
|
||||||
host.fileExists = originalFileExists;
|
|
||||||
host.directoryExists = originalDirectoryExists;
|
|
||||||
host.createDirectory = originalCreateDirectory;
|
|
||||||
host.writeFile = originalWriteFile;
|
|
||||||
compilerHost.getSourceFile = savedGetSourceFile;
|
|
||||||
readFileWithCache = savedReadFileWithCache;
|
|
||||||
extendedConfigCache = undefined;
|
|
||||||
compilerHost.resolveModuleNames = originalResolveModuleNames;
|
|
||||||
moduleResolutionCache = undefined;
|
|
||||||
return anyFailed ? ExitStatus.DiagnosticsPresent_OutputsSkipped : ExitStatus.Success;
|
return anyFailed ? ExitStatus.DiagnosticsPresent_OutputsSkipped : ExitStatus.Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue