Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 57 additions & 48 deletions packages/angular/build/src/builders/application/build-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,62 +95,71 @@ export async function* runEsBuildBuildAction(
}
}

// Setup watcher if watch mode enabled
let watcher: import('../../tools/esbuild/watcher').BuildWatcher | undefined;
if (watch) {
if (progress) {
logger.info('Watch mode enabled. Watching for file changes...');
}
let watchLoopStarted = false;
try {
// Setup watcher if watch mode enabled
if (watch) {
if (progress) {
logger.info('Watch mode enabled. Watching for file changes...');
}

const ignored: string[] = [
// Ignore the output and cache paths to avoid infinite rebuild cycles
outputOptions.base,
cacheOptions.basePath,
`${toPosixPath(workspaceRoot)}/**/.*/**`,
];

// Setup a watcher
const { createWatcher } = await import('../../tools/esbuild/watcher');
watcher = createWatcher({
polling: typeof poll === 'number',
interval: poll,
followSymlinks: preserveSymlinks,
ignored,
});

// Setup abort support
options.signal?.addEventListener('abort', () => void watcher?.close());

// Watch the entire project root if 'NG_BUILD_WATCH_ROOT' environment variable is set
if (shouldWatchRoot) {
if (!preserveSymlinks) {
// Ignore all node modules directories to avoid excessive file watchers.
// Package changes are handled below by watching manifest and lock files.
// NOTE: this is not enable when preserveSymlinks is true as this would break `npm link` usages.
ignored.push('**/node_modules/**');

watcher.add(
packageWatchFiles
.map((file) => path.join(workspaceRoot, file))
.filter((file) => existsSync(file)),
);
}

const ignored: string[] = [
// Ignore the output and cache paths to avoid infinite rebuild cycles
outputOptions.base,
cacheOptions.basePath,
`${toPosixPath(workspaceRoot)}/**/.*/**`,
];

// Setup a watcher
const { createWatcher } = await import('../../tools/esbuild/watcher');
watcher = createWatcher({
polling: typeof poll === 'number',
interval: poll,
followSymlinks: preserveSymlinks,
ignored,
});

// Setup abort support
options.signal?.addEventListener('abort', () => void watcher?.close());

// Watch the entire project root if 'NG_BUILD_WATCH_ROOT' environment variable is set
if (shouldWatchRoot) {
if (!preserveSymlinks) {
// Ignore all node modules directories to avoid excessive file watchers.
// Package changes are handled below by watching manifest and lock files.
// NOTE: this is not enable when preserveSymlinks is true as this would break `npm link` usages.
ignored.push('**/node_modules/**');

watcher.add(
packageWatchFiles
.map((file) => path.join(workspaceRoot, file))
.filter((file) => existsSync(file)),
);
watcher.add(projectRoot);
}

watcher.add(projectRoot);
// Watch locations provided by the initial build result
watcher.add(result.watchFiles);
}

// Watch locations provided by the initial build result
watcher.add(result.watchFiles);
}
// Output the first build results after setting up the watcher to ensure that any code executed
// higher in the iterator call stack will trigger the watcher. This is particularly relevant for
// unit tests which execute the builder and modify the file system programmatically.
yield* emitOutputResults(result, outputOptions);

// Output the first build results after setting up the watcher to ensure that any code executed
// higher in the iterator call stack will trigger the watcher. This is particularly relevant for
// unit tests which execute the builder and modify the file system programmatically.
yield* emitOutputResults(result, outputOptions);
// Finish if watch mode is not enabled
if (!watcher) {
return;
}

// Finish if watch mode is not enabled
if (!watcher) {
return;
watchLoopStarted = true;
} finally {
if (!watchLoopStarted && result) {
await result.dispose();
}
}
Comment thread
clydin marked this conversation as resolved.

// Used to force a full result on next rebuild if there were initial errors.
Expand Down
7 changes: 2 additions & 5 deletions packages/angular/build/src/tools/esbuild/bundler-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,11 @@ export class BundlerContext {
if (this.#esbuildContext) {
// Rebuild using the existing incremental build context
result = await this.#esbuildContext.rebuild();
} else if (this.incremental) {
// Create an incremental build context and perform the first build.
} else {
// Create a build context and perform the build.
// Context creation does not perform a build.
this.#esbuildContext = await context(this.#esbuildOptions);
result = await this.#esbuildContext.rebuild();
} else {
// For non-incremental builds, perform a single build
result = await build(this.#esbuildOptions);
}
} catch (failure) {
// Build failures will throw an exception which contains errors/warnings
Expand Down
Loading