|
| 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('provideRouter(routes),', 'provideAnimationsAsync(), provideRouter(routes),')} |
| 25 | + `, |
| 26 | + ); |
| 27 | + |
| 28 | + // Build without chunk optimization |
| 29 | + await execWithEnv('ng', ['build', '--output-hashing=none'], { |
| 30 | + ...process.env, |
| 31 | + NG_BUILD_OPTIMIZE_CHUNKS: '0', |
| 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