Skip to content

Commit 9b5acdd

Browse files
committed
fixup! perf(@angular/build): optimize incremental watch rebuilds in AOT compilation
1 parent 0a0c5e7 commit 9b5acdd

3 files changed

Lines changed: 353 additions & 138 deletions

File tree

‎packages/angular/build/src/builders/application/tests/behavior/rebuild-errors_spec.ts‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,125 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
232232
);
233233
});
234234

235+
it('detects template errors when an input becomes required or a selector is renamed on watch rebuild', async () => {
236+
harness.useTarget('build', {
237+
...BASE_OPTIONS,
238+
watch: true,
239+
});
240+
241+
await harness.modifyFile('tsconfig.json', (content) => {
242+
const tsconfig = JSON.parse(content);
243+
tsconfig.compilerOptions = {
244+
...tsconfig.compilerOptions,
245+
isolatedModules: true,
246+
};
247+
248+
return JSON.stringify(tsconfig);
249+
});
250+
251+
const goodChildComponentContents = `
252+
import { Component, input } from '@angular/core';
253+
@Component({
254+
selector: 'app-child',
255+
standalone: true,
256+
template: '<p>{{ label() }}</p>',
257+
})
258+
export class ChildComponent {
259+
label = input('');
260+
}
261+
`;
262+
263+
const requiredInputErrorText = `Required input 'label' from component ChildComponent must be specified.`;
264+
const unknownElementErrorText = `'app-child' is not a known element`;
265+
266+
await harness.writeFiles({
267+
'src/app/child.component.ts': goodChildComponentContents,
268+
'src/app/app.module.ts': `
269+
import { NgModule } from '@angular/core';
270+
import { BrowserModule } from '@angular/platform-browser';
271+
import { AppComponent } from './app.component';
272+
@NgModule({
273+
imports: [
274+
BrowserModule,
275+
AppComponent,
276+
],
277+
providers: [],
278+
bootstrap: [AppComponent]
279+
})
280+
export class AppModule { }
281+
`,
282+
'src/app/app.component.ts': `
283+
import { Component } from '@angular/core';
284+
import { ChildComponent } from './child.component';
285+
@Component({
286+
selector: 'app-root',
287+
standalone: true,
288+
imports: [ChildComponent],
289+
template: '<app-child />',
290+
})
291+
export class AppComponent {}
292+
`,
293+
});
294+
295+
await harness.executeWithCases(
296+
[
297+
async ({ result }) => {
298+
expect(result?.success).toBeTrue();
299+
300+
// Make input required using signal input.required() (which has the same TS .d.ts type InputSignal<string>)
301+
await harness.writeFile(
302+
'src/app/child.component.ts',
303+
`
304+
import { Component, input } from '@angular/core';
305+
@Component({
306+
selector: 'app-child',
307+
standalone: true,
308+
template: '<p>{{ label() }}</p>',
309+
})
310+
export class ChildComponent {
311+
label = input.required<string>();
312+
}
313+
`,
314+
);
315+
},
316+
async ({ result, logs }) => {
317+
expect(result?.success).toBeFalse();
318+
expectLog(logs, requiredInputErrorText);
319+
320+
// Revert required input and rename selector instead
321+
await harness.writeFile(
322+
'src/app/child.component.ts',
323+
`
324+
import { Component, input } from '@angular/core';
325+
@Component({
326+
selector: 'app-child-renamed',
327+
standalone: true,
328+
template: '<p>{{ label() }}</p>',
329+
})
330+
export class ChildComponent {
331+
label = input('');
332+
}
333+
`,
334+
);
335+
},
336+
async ({ result, logs }) => {
337+
expect(result?.success).toBeFalse();
338+
expectNoLog(logs, requiredInputErrorText);
339+
expectLog(logs, unknownElementErrorText);
340+
341+
// Revert back to valid child component
342+
await harness.writeFile('src/app/child.component.ts', goodChildComponentContents);
343+
},
344+
({ result, logs }) => {
345+
expect(result?.success).toBeTrue();
346+
expectNoLog(logs, requiredInputErrorText);
347+
expectNoLog(logs, unknownElementErrorText);
348+
},
349+
],
350+
{ outputLogsOnFailure: false },
351+
);
352+
});
353+
235354
it('detects cumulative block syntax errors', async () => {
236355
harness.useTarget('build', {
237356
...BASE_OPTIONS,

‎packages/angular/build/src/tools/angular/angular-host.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export interface AngularHostOptions {
3535
* field. The `version` field is required when used with a TypeScript BuilderProgram.
3636
* @param program The TypeScript Program instance to patch.
3737
*/
38-
export function ensureSourceFileVersions(program: ts.Program): void {
38+
export function ensureSourceFileVersions(
39+
program: ts.Program,
40+
previousProgram?: ts.BuilderProgram,
41+
): void {
3942
const baseGetSourceFiles = program.getSourceFiles;
4043
// TODO: Update Angular compiler to add versions to all internal files and remove this
4144
program.getSourceFiles = function (...parameters) {
@@ -45,7 +48,11 @@ export function ensureSourceFileVersions(program: ts.Program): void {
4548

4649
for (const file of files) {
4750
if (file.version === undefined) {
48-
file.version = calculateHash(file.text);
51+
const previousFile: (ts.SourceFile & { version?: string }) | undefined =
52+
previousProgram && !file.fileName.endsWith('.ngtypecheck.ts')
53+
? previousProgram.getSourceFile(file.fileName)
54+
: undefined;
55+
file.version = previousFile?.version ?? calculateHash(file.text);
4956
}
5057
}
5158

0 commit comments

Comments
 (0)