Skip to content

Commit cd02475

Browse files
committed
fix(@angular-devkit/build-angular): preemptively remove AOT metadata in esbuild builder
The Angular compiler generates two types of metadata calls when it generates AOT code. This metadata is not used in fully AOT compiled applications and can contain direct references to components, services, etc. that may affect the output chunk layout of the application. While this currently has not lead to any problems, it could in the future and the Webpack bundler already performs a transform that preemptively removes these calls. To remain consistent, the esbuild-based build system will now also perform this transform. This also updates the autoprefixer behavior tests to check the actual runtime style text instead of the style text within the metadata calls. (cherry picked from commit c462d9c)
1 parent 58bb392 commit cd02475

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

packages/angular_devkit/build_angular/src/builders/browser-esbuild/angular/aot-compilation.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {
1919

2020
// Temporary deep import for transformer support
2121
// TODO: Move these to a private exports location or move the implementation into this package.
22-
const { mergeTransformers, replaceBootstrap } = require('@ngtools/webpack/src/ivy/transformation');
22+
const {
23+
mergeTransformers,
24+
createAotTransformers,
25+
} = require('@ngtools/webpack/src/ivy/transformation');
2326

2427
class AngularCompilationState {
2528
constructor(
@@ -153,9 +156,11 @@ export class AotCompilation extends AngularCompilation {
153156
assert(this.#state, 'Angular compilation must be initialized prior to emitting files.');
154157
const { angularCompiler, typeScriptProgram } = this.#state;
155158

156-
const transformers = mergeTransformers(angularCompiler.prepareEmit().transformers, {
157-
before: [replaceBootstrap(() => typeScriptProgram.getProgram().getTypeChecker())],
158-
});
159+
const transformers = mergeTransformers(
160+
angularCompiler.prepareEmit().transformers,
161+
// The default behavior is to replace JIT bootstraping and remove AOT metadata calls
162+
createAotTransformers(typeScriptProgram, {}),
163+
);
159164

160165
return async (file: string) => {
161166
const sourceFile = typeScriptProgram.getSourceFile(file);

packages/angular_devkit/build_angular/src/builders/browser-esbuild/tests/behavior/stylesheet_autoprefixer_spec.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,10 @@ describeBuilder(buildEsbuildBrowser, BROWSER_BUILDER_INFO, (harness) => {
106106

107107
harness
108108
.expectFile('dist/main.js')
109-
.content.toMatch(
110-
/section\s*{\\n\s*-webkit-hyphens:\s*none;\\n\s*hyphens:\s*none;\\n\s*}/,
111-
);
109+
.content.toMatch(/{\\n\s*-webkit-hyphens:\s*none;\\n\s*hyphens:\s*none;\\n\s*}/);
112110
harness
113111
.expectFile('dist/main.js')
114-
.content.toMatch(/div\s*{\\n\s*-webkit-hyphens:\s*none;\\n\s*hyphens:\s*none;\\n\s*}/);
112+
.content.toMatch(/{\\n\s*-webkit-hyphens:\s*none;\\n\s*hyphens:\s*none;\\n\s*}/);
115113
});
116114

117115
it(`should not add prefixes if not required by browsers in external component styles [${ext}]`, async () => {
@@ -137,10 +135,8 @@ describeBuilder(buildEsbuildBrowser, BROWSER_BUILDER_INFO, (harness) => {
137135
const { result } = await harness.executeOnce();
138136
expect(result?.success).toBeTrue();
139137

140-
harness
141-
.expectFile('dist/main.js')
142-
.content.toMatch(/section\s*{\\n\s*hyphens:\s*none;\\n\s*}/);
143-
harness.expectFile('dist/main.js').content.toMatch(/div\s*{\\n\s*hyphens:\s*none;\\n\s*}/);
138+
harness.expectFile('dist/main.js').content.toMatch(/{\\n\s*hyphens:\s*none;\\n\s*}/);
139+
harness.expectFile('dist/main.js').content.toMatch(/{\\n\s*hyphens:\s*none;\\n\s*}/);
144140
});
145141
}
146142

@@ -169,7 +165,8 @@ describeBuilder(buildEsbuildBrowser, BROWSER_BUILDER_INFO, (harness) => {
169165

170166
harness
171167
.expectFile('dist/main.js')
172-
.content.toMatch(/div\s*{\\n\s*-webkit-hyphens:\s*none;\\n\s*hyphens:\s*none;\\n\s*}/);
168+
// div[_ngcontent-%COMP%] {\n -webkit-hyphens: none;\n hyphens: none;\n}\n
169+
.content.toMatch(/{\\n\s*-webkit-hyphens:\s*none;\\n\s*hyphens:\s*none;\\n\s*}/);
173170
});
174171

175172
it('should not add prefixes if not required by browsers in inline component styles', async () => {
@@ -193,7 +190,7 @@ describeBuilder(buildEsbuildBrowser, BROWSER_BUILDER_INFO, (harness) => {
193190
const { result } = await harness.executeOnce();
194191
expect(result?.success).toBeTrue();
195192

196-
harness.expectFile('dist/main.js').content.toMatch(/div\s*{\\n\s*hyphens:\s*none;\\n\s*}/);
193+
harness.expectFile('dist/main.js').content.toMatch(/{\\n\s*hyphens:\s*none;\\n\s*}/);
197194
});
198195
});
199196
});

0 commit comments

Comments
 (0)