Improved watch mode

This commit is contained in:
Ryan Cavanaugh 2018-06-06 16:47:15 -07:00
parent b107849a3a
commit 21a65f5dc0

View file

@ -341,8 +341,6 @@ namespace ts {
const cache = createFileMap<ParsedCommandLine>(); const cache = createFileMap<ParsedCommandLine>();
const configParseHost = parseConfigHostFromCompilerHost(host); const configParseHost = parseConfigHostFromCompilerHost(host);
// TODO: Cache invalidation under --watch
function parseConfigFile(configFilePath: ResolvedConfigFileName) { function parseConfigFile(configFilePath: ResolvedConfigFileName) {
const sourceFile = host.getSourceFile(configFilePath, ScriptTarget.JSON) as JsonSourceFile; const sourceFile = host.getSourceFile(configFilePath, ScriptTarget.JSON) as JsonSourceFile;
if (sourceFile === undefined) { if (sourceFile === undefined) {
@ -529,6 +527,8 @@ namespace ts {
const configFileCache = createConfigFileCache(host); const configFileCache = createConfigFileCache(host);
let context = createBuildContext(defaultOptions, reportDiagnostic); let context = createBuildContext(defaultOptions, reportDiagnostic);
const existingWatchersForWildcards = createMap<WildcardDirectoryWatcher>();
return { return {
buildAllProjects, buildAllProjects,
getUpToDateStatus, getUpToDateStatus,
@ -551,18 +551,43 @@ namespace ts {
if (!system.watchFile || !system.watchDirectory || !system.setTimeout) throw new Error("System host must support watchFile / watchDirectory / setTimeout if using --watch"); if (!system.watchFile || !system.watchDirectory || !system.setTimeout) throw new Error("System host must support watchFile / watchDirectory / setTimeout if using --watch");
const graph = getGlobalDependencyGraph()!; const graph = getGlobalDependencyGraph()!;
if (!graph.buildQueue) {
// Everything is broken - we don't even know what to watch. Give up.
return;
}
for (const resolved of graph.buildQueue) { for (const resolved of graph.buildQueue) {
const cfg = configFileCache.parseConfigFile(resolved); const cfg = configFileCache.parseConfigFile(resolved);
if (cfg) { if (cfg) {
// Watch this file
system.watchFile!(resolved, () => {
configFileCache.removeKey(resolved);
invalidateProjectAndScheduleBuilds(resolved);
});
// Update watchers for wildcard directories
if (cfg.configFileSpecs) {
updateWatchingWildcardDirectories(existingWatchersForWildcards, createMapFromTemplate(cfg.configFileSpecs.wildcardDirectories), (dir, flags) => {
return system.watchDirectory!(dir, () => {
invalidateProjectAndScheduleBuilds(resolved);
}, !!(flags & WatchDirectoryFlags.Recursive));
});
}
// Watch input files
for (const input of cfg.fileNames) { for (const input of cfg.fileNames) {
system.watchFile(input, () => { system.watchFile(input, () => {
invalidateProject(resolved); invalidateProjectAndScheduleBuilds(resolved);
system.setTimeout!(buildInvalidatedProjects, 100);
system.setTimeout!(buildDependentInvalidatedProjects, 3000);
}); });
} }
} }
} }
function invalidateProjectAndScheduleBuilds(resolved: ResolvedConfigFileName) {
invalidateProject(resolved);
system!.setTimeout!(buildInvalidatedProjects, 100);
system!.setTimeout!(buildDependentInvalidatedProjects, 3000);
}
} }
function resetBuildContext(opts = defaultOptions) { function resetBuildContext(opts = defaultOptions) {