-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy pathchunk-optimizer-lazy.ts
More file actions
55 lines (51 loc) · 1.82 KB
/
chunk-optimizer-lazy.ts
File metadata and controls
55 lines (51 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import assert from 'node:assert/strict';
import { readdir } from 'node:fs/promises';
import { replaceInFile } from '../../utils/fs';
import { execWithEnv, ng } from '../../utils/process';
export default async function () {
// Add lazy routes.
await ng('generate', 'component', 'lazy-a');
await ng('generate', 'component', 'lazy-b');
await ng('generate', 'component', 'lazy-c');
await replaceInFile(
'src/app/app.routes.ts',
'routes: Routes = [];',
`routes: Routes = [
{
path: 'lazy-a',
loadComponent: () => import('./lazy-a/lazy-a').then(m => m.LazyA),
},
{
path: 'lazy-b',
loadComponent: () => import('./lazy-b/lazy-b').then(m => m.LazyB),
},
{
path: 'lazy-c',
loadComponent: () => import('./lazy-c/lazy-c').then(m => m.LazyC),
},
];`,
);
// Build without chunk optimization
await execWithEnv('ng', ['build', '--output-hashing=none'], {
...process.env,
NG_BUILD_OPTIMIZE_CHUNKS: 'false',
});
const unoptimizedFiles = await readdir('dist/test-project/browser');
const unoptimizedJsFiles = unoptimizedFiles.filter((f) => f.endsWith('.js'));
// Build with chunk optimization
await execWithEnv('ng', ['build', '--output-hashing=none'], {
...process.env,
NG_BUILD_OPTIMIZE_CHUNKS: '1',
});
const optimizedFiles = await readdir('dist/test-project/browser');
const optimizedJsFiles = optimizedFiles.filter((f) => f.endsWith('.js'));
// Check that the number of chunks is reduced but not all combined
assert.ok(
optimizedJsFiles.length < unoptimizedJsFiles.length,
`Expected chunk count to be less than ${unoptimizedJsFiles.length}, but was ${optimizedJsFiles.length}.`,
);
assert.ok(
optimizedJsFiles.length > 1,
`Expected more than one chunk, but found ${optimizedJsFiles.length}.`,
);
}