Skip to content

Commit fdce97f

Browse files
committed
test(@angular/build): add e2e test for animations chunk optimization
Add an E2E test case to verify that the chunk optimizer does not fold lazy-loaded animations modules back into the primary main chunk. The test installs the matching version of @angular/animations, injects provideAnimationsAsync, and builds the test project with chunk optimization enabled. It then validates that the AnimationEngine is preserved within one of the independent lazy chunks and is absent from the initial main.js output.
1 parent a4f11c1 commit fdce97f

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

packages/angular/build/src/builders/application/chunk-optimizer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ export async function optimizeChunks(
312312
});
313313

314314
const result = await bundle.generate({
315+
compact: true,
315316
sourcemap,
316317
chunkFileNames: (chunkInfo) =>
317318
`${chunkInfo.name.replace(/-[a-zA-Z0-9]{8}$/, '')}-[hash].js`,

tests/e2e.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ WEBPACK_IGNORE_TESTS = [
5959
"tests/build/chunk-optimizer-lazy.js",
6060
"tests/build/chunk-optimizer-heuristic.js",
6161
"tests/build/chunk-optimizer-env.js",
62+
"tests/build/chunk-optimizer-animations.js",
6263
]
6364

6465
def _to_glob(patterns):
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import assert from 'node:assert/strict';
2+
import { readdir } from 'node:fs/promises';
3+
import { readFile, writeFile } from '../../utils/fs';
4+
import { installPackage } from '../../utils/packages';
5+
import { execWithEnv } from '../../utils/process';
6+
7+
export default async function () {
8+
// Read @angular/core version from test project's package.json
9+
const projectJson = JSON.parse(await readFile('package.json'));
10+
const ngCoreVersion =
11+
projectJson.dependencies?.['@angular/core'] ??
12+
projectJson.devDependencies?.['@angular/core'] ??
13+
'latest';
14+
15+
// Install @angular/animations package with matching version
16+
await installPackage(`@angular/animations@${ngCoreVersion}`);
17+
18+
// Configure app.config.ts with provideAnimationsAsync
19+
const originalConfig = await readFile('src/app/app.config.ts');
20+
await writeFile(
21+
'src/app/app.config.ts',
22+
`
23+
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
24+
${originalConfig.replace(/providers:\s*\[/, 'providers: [provideAnimationsAsync(),')}
25+
`,
26+
);
27+
28+
const updatedConfig = await readFile('src/app/app.config.ts');
29+
assert.ok(
30+
updatedConfig.includes('provideAnimationsAsync()'),
31+
'Expected src/app/app.config.ts to include provideAnimationsAsync().',
32+
);
33+
34+
// Build with chunk optimization
35+
await execWithEnv('ng', ['build', '--output-hashing=none'], {
36+
...process.env,
37+
NG_BUILD_OPTIMIZE_CHUNKS: '1',
38+
});
39+
const optimizedFiles = await readdir('dist/test-project/browser');
40+
const optimizedJsFiles = optimizedFiles.filter((f) => f.endsWith('.js'));
41+
42+
// Read the optimized main.js file
43+
const mainCode = await readFile('dist/test-project/browser/main.js');
44+
45+
// Check that optimized chunks still contain more than 1 javascript file
46+
assert.ok(
47+
optimizedJsFiles.length > 1,
48+
`Expected more than one chunk, but found ${optimizedJsFiles.length}.`,
49+
);
50+
51+
// Check that one of the lazy loaded chunks contains the animations package code
52+
let foundAnimationsChunk = false;
53+
for (const file of optimizedJsFiles) {
54+
if (file === 'main.js') {
55+
continue;
56+
}
57+
const code = await readFile(`dist/test-project/browser/${file}`);
58+
if (code.includes('AnimationEngine')) {
59+
foundAnimationsChunk = true;
60+
break;
61+
}
62+
}
63+
64+
assert.ok(
65+
foundAnimationsChunk,
66+
'Expected to find AnimationEngine in one of the optimized lazy chunks.',
67+
);
68+
69+
// The animations engine should not be bundled in main.js
70+
assert.ok(
71+
!mainCode.includes('AnimationEngine'),
72+
'Expected main.js not to contain AnimationEngine from @angular/animations/browser.',
73+
);
74+
}

0 commit comments

Comments
 (0)