Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): watch all TypeScript referenced f…
Browse files Browse the repository at this point in the history
…iles in esbuild builder

When using the esbuild-based browser application builder in watch mode, all files referenced
by the TypeScript program are now watched in additional to files within the project root.
This allows for more extensive monorepo setups to take advantage of watch mode as TypeScript
files may exist in other library projects within the repository.
  • Loading branch information
clydin committed May 25, 2023
1 parent b674bd2 commit 5cacd34
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export abstract class AngularCompilation {
tsconfig: string,
hostOptions: AngularHostOptions,
compilerOptionsTransformer?: (compilerOptions: ng.CompilerOptions) => ng.CompilerOptions,
): Promise<{ affectedFiles: ReadonlySet<ts.SourceFile>; compilerOptions: ng.CompilerOptions }>;
): Promise<{
affectedFiles: ReadonlySet<ts.SourceFile>;
compilerOptions: ng.CompilerOptions;
referencedFiles: readonly string[];
}>;

abstract collectDiagnostics(): Iterable<ts.Diagnostic>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export class AotCompilation extends AngularCompilation {
tsconfig: string,
hostOptions: AngularHostOptions,
compilerOptionsTransformer?: (compilerOptions: ng.CompilerOptions) => ng.CompilerOptions,
): Promise<{ affectedFiles: ReadonlySet<ts.SourceFile>; compilerOptions: ng.CompilerOptions }> {
): Promise<{
affectedFiles: ReadonlySet<ts.SourceFile>;
compilerOptions: ng.CompilerOptions;
referencedFiles: readonly string[];
}> {
// Dynamically load the Angular compiler CLI package
const { NgtscProgram, OptimizeFor } = await AngularCompilation.loadCompilerCli();

Expand Down Expand Up @@ -96,7 +100,12 @@ export class AotCompilation extends AngularCompilation {
this.#state?.diagnosticCache,
);

return { affectedFiles, compilerOptions };
const referencedFiles = typeScriptProgram
.getSourceFiles()
.filter((sourceFile) => !angularCompiler.ignoreForEmit.has(sourceFile))
.map((sourceFile) => sourceFile.fileName);

return { affectedFiles, compilerOptions, referencedFiles };
}

*collectDiagnostics(): Iterable<ts.Diagnostic> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export class SourceFileCache extends Map<string, ts.SourceFile> {
readonly typeScriptFileCache = new Map<string, string | Uint8Array>();
readonly loadResultCache = new MemoryLoadResultCache();

referencedFiles?: readonly string[];

constructor(readonly persistentCachePath?: string) {
super();
}
Expand Down Expand Up @@ -185,6 +187,7 @@ export function createCompilerPlugin(
// In watch mode, previous build state will be reused.
const {
compilerOptions: { allowJs },
referencedFiles,
} = await compilation.initialize(tsconfigPath, hostOptions, (compilerOptions) => {
if (
compilerOptions.target === undefined ||
Expand Down Expand Up @@ -254,6 +257,11 @@ export function createCompilerPlugin(
}
});

// Store referenced files for updated file watching if enabled
if (pluginOptions.sourceFileCache) {
pluginOptions.sourceFileCache.referencedFiles = referencedFiles;
}

// Reset the setup warnings so that they are only shown during the first build.
setupWarnings = undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export class JitCompilation extends AngularCompilation {
tsconfig: string,
hostOptions: AngularHostOptions,
compilerOptionsTransformer?: (compilerOptions: ng.CompilerOptions) => ng.CompilerOptions,
): Promise<{ affectedFiles: ReadonlySet<ts.SourceFile>; compilerOptions: ng.CompilerOptions }> {
): Promise<{
affectedFiles: ReadonlySet<ts.SourceFile>;
compilerOptions: ng.CompilerOptions;
referencedFiles: readonly string[];
}> {
// Dynamically load the Angular compiler CLI package
const { constructorParametersDownlevelTransform } = await AngularCompilation.loadCompilerCli();

Expand Down Expand Up @@ -68,7 +72,11 @@ export class JitCompilation extends AngularCompilation {
createJitResourceTransformer(() => typeScriptProgram.getProgram().getTypeChecker()),
);

return { affectedFiles, compilerOptions };
const referencedFiles = typeScriptProgram
.getSourceFiles()
.map((sourceFile) => sourceFile.fileName);

return { affectedFiles, compilerOptions, referencedFiles };
}

*collectDiagnostics(): Iterable<ts.Diagnostic> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class ExecutionResult {
};
}

get watchFiles() {
return this.codeBundleCache?.referencedFiles ?? [];
}

createRebuildState(fileChanges: ChangedFiles): RebuildState {
this.codeBundleCache?.invalidate([...fileChanges.modified, ...fileChanges.removed]);

Expand Down Expand Up @@ -676,6 +680,10 @@ export async function* buildEsbuildBrowserInternal(
];
watcher.add(packageWatchFiles.map((file) => path.join(normalizedOptions.workspaceRoot, file)));

// Watch locations provided by the initial build result
let previousWatchFiles = new Set(result.watchFiles);
watcher.add(result.watchFiles);

// Wait for changes and rebuild as needed
try {
for await (const changes of watcher) {
Expand All @@ -687,6 +695,14 @@ export async function* buildEsbuildBrowserInternal(
execute(normalizedOptions, context, result.createRebuildState(changes)),
);

// Update watched locations provided by the new build result.
// Add any new locations
watcher.add(result.watchFiles.filter((watchFile) => !previousWatchFiles.has(watchFile)));
const newWatchFiles = new Set(result.watchFiles);
// Remove any old locations
watcher.remove([...previousWatchFiles].filter((watchFile) => !newWatchFiles.has(watchFile)));
previousWatchFiles = newWatchFiles;

if (shouldWriteResult) {
// Write output files
await writeResultFiles(result.outputFiles, result.assetFiles, normalizedOptions.outputPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class ChangedFiles {
}

export interface BuildWatcher extends AsyncIterableIterator<ChangedFiles> {
add(paths: string | string[]): void;
remove(paths: string | string[]): void;
add(paths: string | readonly string[]): void;
remove(paths: string | readonly string[]): void;
close(): Promise<void>;
}

Expand Down

0 comments on commit 5cacd34

Please sign in to comment.