From 21a65f5dc03d619b58d98a9e2cb0387720f73d96 Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Wed, 6 Jun 2018 16:47:15 -0700 Subject: [PATCH] Improved watch mode --- src/compiler/tsbuild.ts | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/compiler/tsbuild.ts b/src/compiler/tsbuild.ts index db998d651c..c041540cdf 100644 --- a/src/compiler/tsbuild.ts +++ b/src/compiler/tsbuild.ts @@ -341,8 +341,6 @@ namespace ts { const cache = createFileMap(); const configParseHost = parseConfigHostFromCompilerHost(host); - // TODO: Cache invalidation under --watch - function parseConfigFile(configFilePath: ResolvedConfigFileName) { const sourceFile = host.getSourceFile(configFilePath, ScriptTarget.JSON) as JsonSourceFile; if (sourceFile === undefined) { @@ -529,6 +527,8 @@ namespace ts { const configFileCache = createConfigFileCache(host); let context = createBuildContext(defaultOptions, reportDiagnostic); + const existingWatchersForWildcards = createMap(); + return { buildAllProjects, 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"); 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) { const cfg = configFileCache.parseConfigFile(resolved); 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) { system.watchFile(input, () => { - invalidateProject(resolved); - system.setTimeout!(buildInvalidatedProjects, 100); - system.setTimeout!(buildDependentInvalidatedProjects, 3000); + invalidateProjectAndScheduleBuilds(resolved); }); } } } + + function invalidateProjectAndScheduleBuilds(resolved: ResolvedConfigFileName) { + invalidateProject(resolved); + system!.setTimeout!(buildInvalidatedProjects, 100); + system!.setTimeout!(buildDependentInvalidatedProjects, 3000); + } } function resetBuildContext(opts = defaultOptions) {