Skip to content

Commit c3b5d77

Browse files
committed
fixup! perf(@angular/build): optimize incremental watch rebuilds in angular compilation
1 parent 9269238 commit c3b5d77

1 file changed

Lines changed: 64 additions & 61 deletions

File tree

‎packages/angular/build/src/tools/angular/compilation/aot-compilation.ts‎

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -280,79 +280,82 @@ export class AotCompilation extends TypeScriptCompilation {
280280
const syntactic = modes & DiagnosticModes.Syntactic;
281281
const semantic = modes & DiagnosticModes.Semantic;
282282

283-
// Collect program level diagnostics
284-
if (modes & DiagnosticModes.Option) {
285-
yield* typeScriptProgram.getConfigFileParsingDiagnostics();
286-
yield* angularCompiler.getOptionDiagnostics();
287-
yield* typeScriptProgram.getOptionsDiagnostics();
288-
}
289-
if (syntactic) {
290-
yield* typeScriptProgram.getGlobalDiagnostics();
291-
}
292-
293-
// Collect source file specific diagnostics
294-
for (const sourceFile of typeScriptProgram.getSourceFiles()) {
295-
if (angularCompiler.ignoreForDiagnostics.has(sourceFile)) {
296-
continue;
283+
try {
284+
// Collect program level diagnostics
285+
if (modes & DiagnosticModes.Option) {
286+
yield* typeScriptProgram.getConfigFileParsingDiagnostics();
287+
yield* angularCompiler.getOptionDiagnostics();
288+
yield* typeScriptProgram.getOptionsDiagnostics();
297289
}
298-
299290
if (syntactic) {
300-
// TypeScript will use cached diagnostics for files that have not been
301-
// changed or affected for this build when using incremental building.
302-
yield* profileSync(
303-
'NG_DIAGNOSTICS_SYNTACTIC',
304-
() => typeScriptProgram.getSyntacticDiagnostics(sourceFile),
305-
true,
306-
);
291+
yield* typeScriptProgram.getGlobalDiagnostics();
307292
}
308293

309-
if (!semantic) {
310-
continue;
311-
}
294+
// Collect source file specific diagnostics
295+
for (const sourceFile of typeScriptProgram.getSourceFiles()) {
296+
if (angularCompiler.ignoreForDiagnostics.has(sourceFile)) {
297+
continue;
298+
}
312299

313-
yield* profileSync(
314-
'NG_DIAGNOSTICS_SEMANTIC',
315-
() => typeScriptProgram.getSemanticDiagnostics(sourceFile),
316-
true,
317-
);
300+
if (syntactic) {
301+
// TypeScript will use cached diagnostics for files that have not been
302+
// changed or affected for this build when using incremental building.
303+
yield* profileSync(
304+
'NG_DIAGNOSTICS_SYNTACTIC',
305+
() => typeScriptProgram.getSyntacticDiagnostics(sourceFile),
306+
true,
307+
);
308+
}
318309

319-
// Declaration files cannot have template diagnostics
320-
if (sourceFile.isDeclarationFile) {
321-
continue;
322-
}
310+
if (!semantic) {
311+
continue;
312+
}
323313

324-
// Only request Angular template diagnostics for affected files to avoid
325-
// overhead of template diagnostics for unchanged files.
326-
if (affectedFiles.has(sourceFile)) {
327-
const angularDiagnostics = profileSync(
328-
'NG_DIAGNOSTICS_TEMPLATE',
329-
() => angularCompiler.getDiagnosticsForFile(sourceFile, templateDiagnosticsOptimization),
314+
yield* profileSync(
315+
'NG_DIAGNOSTICS_SEMANTIC',
316+
() => typeScriptProgram.getSemanticDiagnostics(sourceFile),
330317
true,
331318
);
332-
diagnosticCache.set(sourceFile, angularDiagnostics);
333-
yield* angularDiagnostics;
334-
} else {
335-
const angularDiagnostics = diagnosticCache.get(sourceFile);
336-
if (angularDiagnostics) {
319+
320+
// Declaration files cannot have template diagnostics
321+
if (sourceFile.isDeclarationFile) {
322+
continue;
323+
}
324+
325+
// Only request Angular template diagnostics for affected files to avoid
326+
// overhead of template diagnostics for unchanged files.
327+
if (affectedFiles.has(sourceFile)) {
328+
const angularDiagnostics = profileSync(
329+
'NG_DIAGNOSTICS_TEMPLATE',
330+
() =>
331+
angularCompiler.getDiagnosticsForFile(sourceFile, templateDiagnosticsOptimization),
332+
true,
333+
);
334+
diagnosticCache.set(sourceFile, angularDiagnostics);
337335
yield* angularDiagnostics;
336+
} else {
337+
const angularDiagnostics = diagnosticCache.get(sourceFile);
338+
if (angularDiagnostics) {
339+
yield* angularDiagnostics;
340+
}
338341
}
339342
}
340-
}
341-
342-
// Angular's template typechecker lazily creates `.ngtypecheck.ts` shims in a new `ts.Program`
343-
// during `getDiagnosticsForFile`. Sync the builder program with `angularCompiler.getCurrentProgram()`
344-
// so the next incremental build does not treat all `.ngtypecheck.ts` files as newly added files.
345-
commitBuilderProgramState(typeScriptProgram);
346-
const currentTsProgram = angularCompiler.getCurrentProgram();
347-
if (currentTsProgram !== typeScriptProgram.getProgram()) {
348-
ensureSourceFileVersions(currentTsProgram);
349-
const updatedBuilder = ts.createEmitAndSemanticDiagnosticsBuilderProgram(
350-
currentTsProgram,
351-
this.#state.compilerHost,
352-
typeScriptProgram,
353-
);
354-
commitBuilderProgramState(updatedBuilder);
355-
this.#state.typeScriptProgram = updatedBuilder;
343+
} finally {
344+
// Angular's template typechecker lazily creates `.ngtypecheck.ts` shims in a new `ts.Program`
345+
// during `getDiagnosticsForFile`. Sync the builder program with `angularCompiler.getCurrentProgram()`
346+
// so the next incremental build does not treat all `.ngtypecheck.ts` files as newly added files.
347+
commitBuilderProgramState(typeScriptProgram);
348+
const currentTsProgram = angularCompiler.getCurrentProgram();
349+
if (currentTsProgram !== typeScriptProgram.getProgram()) {
350+
ensureSourceFileVersions(currentTsProgram);
351+
const updatedBuilder = ts.createEmitAndSemanticDiagnosticsBuilderProgram(
352+
currentTsProgram,
353+
this.#state.compilerHost,
354+
typeScriptProgram,
355+
);
356+
commitBuilderProgramState(updatedBuilder);
357+
this.#state.typeScriptProgram = updatedBuilder;
358+
}
356359
}
357360
}
358361

0 commit comments

Comments
 (0)