|
| 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 | +import { installPackage, uninstallPackage } from '../../utils/packages'; |
| 6 | + |
| 7 | +export default async function () { |
| 8 | + // Case 1: Force on with true/1 with 1 lazy chunk |
| 9 | + await ng('generate', 'component', 'lazy-a'); |
| 10 | + await replaceInFile( |
| 11 | + 'src/app/app.routes.ts', |
| 12 | + 'routes: Routes = [];', |
| 13 | + `routes: Routes = [ |
| 14 | + { |
| 15 | + path: 'lazy-a', |
| 16 | + loadComponent: () => import('./lazy-a/lazy-a.component').then(m => m.LazyAComponent), |
| 17 | + }, |
| 18 | + ];`, |
| 19 | + ); |
| 20 | + |
| 21 | + // Build with forced optimization |
| 22 | + await execWithEnv('ng', ['build', '--output-hashing=none'], { |
| 23 | + ...process.env, |
| 24 | + NG_BUILD_OPTIMIZE_CHUNKS: 'true', |
| 25 | + }); |
| 26 | + const files1Opt = await readdir('dist/test-project/browser'); |
| 27 | + const jsFiles1Opt = files1Opt.filter((f) => f.endsWith('.js')); |
| 28 | + |
| 29 | + // Build with forced off |
| 30 | + await execWithEnv('ng', ['build', '--output-hashing=none'], { |
| 31 | + ...process.env, |
| 32 | + NG_BUILD_OPTIMIZE_CHUNKS: 'false', |
| 33 | + }); |
| 34 | + const files1Unopt = await readdir('dist/test-project/browser'); |
| 35 | + const jsFiles1Unopt = files1Unopt.filter((f) => f.endsWith('.js')); |
| 36 | + |
| 37 | + // We just verify it runs without error. |
| 38 | + // With 1 chunk it might not be able to optimize further, so counts might be equal. |
| 39 | + |
| 40 | + // Case 2: Force off with false/0 with 3 lazy chunks |
| 41 | + await ng('generate', 'component', 'lazy-b'); |
| 42 | + await ng('generate', 'component', 'lazy-c'); |
| 43 | + await replaceInFile( |
| 44 | + 'src/app/app.routes.ts', |
| 45 | + `path: 'lazy-a', |
| 46 | + loadComponent: () => import('./lazy-a/lazy-a.component').then(m => m.LazyAComponent), |
| 47 | + },`, |
| 48 | + `path: 'lazy-a', |
| 49 | + loadComponent: () => import('./lazy-a/lazy-a.component').then(m => m.LazyAComponent), |
| 50 | + }, |
| 51 | + { |
| 52 | + path: 'lazy-b', |
| 53 | + loadComponent: () => import('./lazy-b/lazy-b.component').then(m => m.LazyBComponent), |
| 54 | + }, |
| 55 | + { |
| 56 | + path: 'lazy-c', |
| 57 | + loadComponent: () => import('./lazy-c/lazy-c.component').then(m => m.LazyCComponent), |
| 58 | + },`, |
| 59 | + ); |
| 60 | + |
| 61 | + // Build with forced off |
| 62 | + await execWithEnv('ng', ['build', '--output-hashing=none'], { |
| 63 | + ...process.env, |
| 64 | + NG_BUILD_OPTIMIZE_CHUNKS: 'false', |
| 65 | + }); |
| 66 | + const files3Unopt = await readdir('dist/test-project/browser'); |
| 67 | + const jsFiles3Unopt = files3Unopt.filter((f) => f.endsWith('.js')); |
| 68 | + |
| 69 | + // Build with default (should optimize because 3 chunks) |
| 70 | + await ng('build', '--output-hashing=none'); |
| 71 | + const files3Default = await readdir('dist/test-project/browser'); |
| 72 | + const jsFiles3Default = files3Default.filter((f) => f.endsWith('.js')); |
| 73 | + |
| 74 | + assert.ok( |
| 75 | + jsFiles3Default.length < jsFiles3Unopt.length, |
| 76 | + `Expected default build (3 chunks) to be optimized compared to forced off. Default: ${jsFiles3Default.length}, Forced Off: ${jsFiles3Unopt.length}`, |
| 77 | + ); |
| 78 | + |
| 79 | + // Case 3: Custom threshold |
| 80 | + // Set threshold to 4 with 3 chunks -> should NOT optimize! |
| 81 | + await execWithEnv('ng', ['build', '--output-hashing=none'], { |
| 82 | + ...process.env, |
| 83 | + NG_BUILD_OPTIMIZE_CHUNKS: '4', |
| 84 | + }); |
| 85 | + const files3Thresh4 = await readdir('dist/test-project/browser'); |
| 86 | + const jsFiles3Thresh4 = files3Thresh4.filter((f) => f.endsWith('.js')); |
| 87 | + |
| 88 | + assert.ok( |
| 89 | + jsFiles3Thresh4.length >= jsFiles3Unopt.length, |
| 90 | + `Expected build with threshold 4 and 3 chunks to NOT be optimized. Thresh 4: ${jsFiles3Thresh4.length}, Unoptimized: ${jsFiles3Unopt.length}`, |
| 91 | + ); |
| 92 | + |
| 93 | + // Case 4: Opt into Rolldown |
| 94 | + await installPackage('rolldown@1.0.0-rc.12'); |
| 95 | + try { |
| 96 | + await execWithEnv('ng', ['build', '--output-hashing=none'], { |
| 97 | + ...process.env, |
| 98 | + NG_BUILD_CHUNKS_ROLLDOWN: '1', |
| 99 | + NG_BUILD_OPTIMIZE_CHUNKS: 'true', |
| 100 | + }); |
| 101 | + const filesRolldown = await readdir('dist/test-project/browser'); |
| 102 | + const jsFilesRolldown = filesRolldown.filter((f) => f.endsWith('.js')); |
| 103 | + |
| 104 | + assert.ok(jsFilesRolldown.length > 0, 'Expected Rolldown build to produce output files.'); |
| 105 | + } finally { |
| 106 | + // Clean up |
| 107 | + await uninstallPackage('rolldown'); |
| 108 | + } |
| 109 | +} |
0 commit comments