Skip to content

fix(@angular-devkit/build-angular): watch all bundler provided inputs with esbuild builder #25416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ export class AotCompilation extends AngularCompilation {
const referencedFiles = typeScriptProgram
.getSourceFiles()
.filter((sourceFile) => !angularCompiler.ignoreForEmit.has(sourceFile))
.map((sourceFile) => sourceFile.fileName);
.flatMap((sourceFile) => [
sourceFile.fileName,
...angularCompiler.getResourceDependencies(sourceFile),
]);

return { affectedFiles, compilerOptions, referencedFiles };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
build,
context,
} from 'esbuild';
import { basename, extname, relative } from 'node:path';
import { basename, extname, join, relative } from 'node:path';

export type BundleContextResult =
| { errors: Message[]; warnings: Message[] }
Expand Down Expand Up @@ -48,6 +48,8 @@ export class BundlerContext {
#esbuildContext?: BuildContext<{ metafile: true; write: false }>;
#esbuildOptions: BuildOptions & { metafile: true; write: false };

readonly watchFiles = new Set<string>();

constructor(
private workspaceRoot: string,
private incremental: boolean,
Expand Down Expand Up @@ -138,6 +140,17 @@ export class BundlerContext {
}
}

// Update files that should be watched.
// While this should technically not be linked to incremental mode, incremental is only
// currently enabled with watch mode where watch files are needed.
if (this.incremental) {
this.watchFiles.clear();
// Add input files except virtual angular files which do not exist on disk
Object.keys(result.metafile.inputs)
.filter((input) => !input.startsWith('angular:'))
.forEach((input) => this.watchFiles.add(join(this.workspaceRoot, input)));
}

// Return if the build encountered any errors
if (result.errors.length) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ export class ExecutionResult {
}

get watchFiles() {
return this.codeBundleCache?.referencedFiles ?? [];
const files = this.rebuildContexts.flatMap((context) => [...context.watchFiles]);
if (this.codeBundleCache?.referencedFiles) {
files.push(...this.codeBundleCache.referencedFiles);
}

return files;
}

createRebuildState(fileChanges: ChangedFiles): RebuildState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export function createWatcher(options?: {
ignored?: string[];
}): BuildWatcher {
const watcher = new FSWatcher({
...options,
usePolling: options?.polling,
interval: options?.interval,
ignored: options?.ignored,
disableGlobbing: true,
ignoreInitial: true,
});
Expand Down