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