Skip to content

Commit 0d54f2d

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): only watch used files with application builder
When using the application builder in watch mode (including `ng serve`), the file watching will now only watch files used or relevant to the used files. Previously, all files within the project root were watched. This previous behavior could result in unneeded rebuilds when unrelated files were changed. An environment variable named `NG_BUILD_WATCH_ROOT` is also now available to enable the previous behavior in cases where it is still preferred as well as for testing and debugging purposes. (cherry picked from commit 06b8fe6)
1 parent 486becd commit 0d54f2d

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

packages/angular_devkit/build_angular/src/builders/application/build-action.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { BuildOutputFile } from '../../tools/esbuild/bundler-context';
1414
import { ExecutionResult, RebuildState } from '../../tools/esbuild/bundler-execution-result';
1515
import { shutdownSassWorkerPool } from '../../tools/esbuild/stylesheets/sass-language';
1616
import { withNoProgress, withSpinner, writeResultFiles } from '../../tools/esbuild/utils';
17+
import { shouldWatchRoot } from '../../utils/environment-options';
1718
import { assertIsError } from '../../utils/error';
1819
import { NormalizedCachedOptions } from '../../utils/normalize-cache';
1920

@@ -112,8 +113,10 @@ export async function* runEsBuildBuildAction(
112113
// Setup abort support
113114
options.signal?.addEventListener('abort', () => void watcher?.close());
114115

115-
// Temporarily watch the entire project
116-
watcher.add(projectRoot);
116+
// Watch the entire project root if 'NG_BUILD_WATCH_ROOT' environment variable is set
117+
if (shouldWatchRoot) {
118+
watcher.add(projectRoot);
119+
}
117120

118121
// Watch workspace for package manager changes
119122
const packageWatchFiles = [

packages/angular_devkit/build_angular/src/tools/esbuild/bundler-execution-result.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ export class ExecutionResult {
8181
if (this.codeBundleCache?.referencedFiles) {
8282
files.push(...this.codeBundleCache.referencedFiles);
8383
}
84+
if (this.codeBundleCache?.loadResultCache) {
85+
files.push(...this.codeBundleCache.loadResultCache.watchFiles);
86+
}
8487

8588
return files;
8689
}

packages/angular_devkit/build_angular/src/utils/environment-options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,6 @@ export const useLegacySass: boolean = (() => {
9999

100100
const debugPerfVariable = process.env['NG_BUILD_DEBUG_PERF'];
101101
export const debugPerformance = isPresent(debugPerfVariable) && isEnabled(debugPerfVariable);
102+
103+
const watchRootVariable = process.env['NG_BUILD_WATCH_ROOT'];
104+
export const shouldWatchRoot = isPresent(watchRootVariable) && isEnabled(watchRootVariable);

0 commit comments

Comments
 (0)