Skip to content

Commit ee01c14

Browse files
committed
perf(@angular/build): avoid TS AST eviction and d.ts re-emit on external resource edits
Register external stylesheet dependencies on `stylesheetFile ?? containingFile` in the library builder so modifying an external stylesheet or imported SCSS partial does not add the component `.ts` file to `effectiveModifiedFiles` and evict it from `TypeScriptCompilation`'s AST cache. Instead, `getResourceDependencies` maps the modified stylesheet back to the component `.ts` file via `resourceModifiedFiles` while reusing the cached `ts.SourceFile` AST. Additionally, persist a `WeakSet<ts.SourceFile>` of emitted declaration files on `AngularCompilationState` across incremental watch builds so that unchanged `ts.SourceFile`s re-emitted solely due to external resource edits (`templateUrl` / `styleUrl`) pass `EmitOnly.Js` to `program.emit`, skipping redundant `.d.ts` generation and downstream declaration processing.
1 parent f696eaa commit ee01c14

3 files changed

Lines changed: 78 additions & 6 deletions

File tree

‎packages/angular/build/src/builders/library/pipeline/compilation.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ export async function compileLibrary(
8989
: await stylesheetBundler.bundleInline(data, containingFile);
9090

9191
if (result.referencedFiles) {
92-
buildState.addFileReferences(containingFile, result.referencedFiles);
93-
if (stylesheetFile) {
94-
buildState.addFileReferences(stylesheetFile, result.referencedFiles);
95-
}
92+
buildState.addFileReferences(stylesheetFile ?? containingFile, result.referencedFiles);
9693
}
9794

9895
if (result.warnings.length > 0) {

‎packages/angular/build/src/builders/library/tests/behavior/watch_spec.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,5 +905,79 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
905905
},
906906
]);
907907
});
908+
909+
it('should rebuild when an external Sass partial or template is modified and recover from template errors', async () => {
910+
await harness.writeFiles({
911+
'projects/lib/src/lib/_vars.scss': '$primary-color: rgb(10, 20, 30);',
912+
'projects/lib/src/lib/lib.component.scss':
913+
'@use "./vars"; :host { color: vars.$primary-color; }',
914+
'projects/lib/src/lib/lib.component.html': '<h1>{{ title }}</h1>',
915+
'projects/lib/src/lib/lib.component.ts': `
916+
import { Component } from '@angular/core';
917+
918+
@Component({
919+
selector: 'lib-external-resources',
920+
templateUrl: './lib.component.html',
921+
styleUrl: './lib.component.scss',
922+
})
923+
export class LibComponent {
924+
title = 'Hello';
925+
}
926+
`,
927+
});
928+
929+
harness.useTarget('build', {
930+
...BASE_OPTIONS,
931+
watch: true,
932+
});
933+
934+
await harness.executeWithCases([
935+
async ({ result }) => {
936+
expect(result?.success).toBeTrue();
937+
const content = harness.readFile('dist/lib/fesm2022/lib.mjs');
938+
expect(content).toContain('h1');
939+
expect(content).toMatch(/color:\s*(?:#0a141e|rgb\(10,\s*20,\s*30\))/);
940+
expect(harness.readFile('dist/lib/types/lib.d.ts')).toContain('LibComponent');
941+
942+
// Modify the imported Sass partial
943+
await harness.writeFile(
944+
'projects/lib/src/lib/_vars.scss',
945+
'$primary-color: rgb(200, 100, 50);',
946+
);
947+
},
948+
async ({ result }) => {
949+
expect(result?.success).toBeTrue();
950+
expect(harness.readFile('dist/lib/fesm2022/lib.mjs')).toMatch(
951+
/color:\s*(?:#c86432|rgb\(200,\s*100,\s*50\))/,
952+
);
953+
expect(harness.readFile('dist/lib/types/lib.d.ts')).toContain('LibComponent');
954+
955+
// Introduce a template type-checking error in the external HTML template
956+
await harness.writeFile(
957+
'projects/lib/src/lib/lib.component.html',
958+
'<section>{{ nonExistentProp }}</section>',
959+
);
960+
},
961+
async ({ result, logs }) => {
962+
expect(result?.success).toBeFalse();
963+
expect(logs).toContain(
964+
jasmine.objectContaining({
965+
message: jasmine.stringContaining('nonExistentProp'),
966+
}),
967+
);
968+
969+
// Fix the external HTML template error
970+
await harness.writeFile(
971+
'projects/lib/src/lib/lib.component.html',
972+
'<footer>{{ title }}</footer>',
973+
);
974+
},
975+
({ result }) => {
976+
expect(result?.success).toBeTrue();
977+
expect(harness.readFile('dist/lib/fesm2022/lib.mjs')).toContain('footer');
978+
expect(harness.readFile('dist/lib/types/lib.d.ts')).toContain('LibComponent');
979+
},
980+
]);
981+
});
908982
});
909983
});

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class AngularCompilationState {
8282
public readonly diagnosticCache = new WeakMap<ts.SourceFile, ts.Diagnostic[]>(),
8383
public readonly filesWithoutDtsSignature = new Set<string>(),
8484
public readonly useFallbackRootNames = false,
85+
public readonly emittedDtsSourceFiles = new WeakSet<ts.SourceFile>(),
8586
) {}
8687

8788
get angularCompiler() {
@@ -360,6 +361,7 @@ export class AotCompilation extends TypeScriptCompilation {
360361
this.#state?.diagnosticCache,
361362
filesWithoutDtsSignature,
362363
useFallbackRootNames,
364+
this.#state?.emittedDtsSourceFiles,
363365
);
364366

365367
return {
@@ -490,9 +492,9 @@ export class AotCompilation extends TypeScriptCompilation {
490492
override emitAffectedFiles(): Iterable<EmitFileResult> {
491493
assert(this.#state, 'Angular compilation must be initialized prior to emitting files.');
492494
const {
493-
affectedFiles,
494495
angularCompiler,
495496
compilerHost,
497+
emittedDtsSourceFiles,
496498
typeScriptProgram,
497499
webWorkerTransform,
498500
useTypeScriptTranspilation,
@@ -503,7 +505,6 @@ export class AotCompilation extends TypeScriptCompilation {
503505

504506
const emittedFiles = new Map<string, EmitFileResult>();
505507
const emittedSourceFiles = new Set<ts.SourceFile>();
506-
const emittedDtsSourceFiles = new Set<ts.SourceFile>();
507508
const writeFileCallback: ts.WriteFileCallback = (filename, contents, _a, _b, sourceFiles) => {
508509
if (!sourceFiles?.length && filename.endsWith(buildInfoFilename)) {
509510
// Save builder info contents to specified location

0 commit comments

Comments
 (0)