Skip to content

Commit c462d9c

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.
1 parent c657639 commit c462d9c

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(
@@ -191,9 +194,11 @@ export class AotCompilation extends AngularCompilation {
191194
angularCompiler.incrementalCompilation.recordSuccessfulEmit(sourceFile);
192195
emittedFiles.set(sourceFile, { filename: sourceFile.fileName, contents });
193196
};
194-
const transformers = mergeTransformers(angularCompiler.prepareEmit().transformers, {
195-
before: [replaceBootstrap(() => typeScriptProgram.getProgram().getTypeChecker())],
196-
});
197+
const transformers = mergeTransformers(
198+
angularCompiler.prepareEmit().transformers,
199+
// The default behavior is to replace JIT bootstraping and remove AOT metadata calls
200+
createAotTransformers(typeScriptProgram, {}),
201+
);
197202

198203
// TypeScript will loop until there are no more affected files in the program
199204
while (

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)