|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { buildApplication } from '../../index'; |
| 10 | +import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; |
| 11 | + |
| 12 | +/** Minimal subset of an esbuild metafile used by stats assertions. */ |
| 13 | +interface StatsMetafile { |
| 14 | + inputs: Record<string, unknown>; |
| 15 | + outputs: Record<string, { inputs: Record<string, unknown> }>; |
| 16 | +} |
| 17 | + |
| 18 | +describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { |
| 19 | + describe('Option: "statsJson"', () => { |
| 20 | + describe('browser-only build', () => { |
| 21 | + beforeEach(() => { |
| 22 | + harness.useTarget('build', { |
| 23 | + ...BASE_OPTIONS, |
| 24 | + statsJson: true, |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('generates browser-stats.json and browser-initial-stats.json', async () => { |
| 29 | + const { result } = await harness.executeOnce(); |
| 30 | + |
| 31 | + expect(result?.success).toBeTrue(); |
| 32 | + harness.expectFile('dist/browser-stats.json').toExist(); |
| 33 | + harness.expectFile('dist/browser-initial-stats.json').toExist(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('does not generate server stats files when SSR is disabled', async () => { |
| 37 | + const { result } = await harness.executeOnce(); |
| 38 | + |
| 39 | + expect(result?.success).toBeTrue(); |
| 40 | + harness.expectFile('dist/server-stats.json').toNotExist(); |
| 41 | + harness.expectFile('dist/server-initial-stats.json').toNotExist(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('does not generate the legacy stats.json file', async () => { |
| 45 | + const { result } = await harness.executeOnce(); |
| 46 | + |
| 47 | + expect(result?.success).toBeTrue(); |
| 48 | + harness.expectFile('dist/stats.json').toNotExist(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('stats files contain valid esbuild metafile structure', async () => { |
| 52 | + const { result } = await harness.executeOnce(); |
| 53 | + |
| 54 | + expect(result?.success).toBeTrue(); |
| 55 | + |
| 56 | + for (const filename of ['dist/browser-stats.json', 'dist/browser-initial-stats.json']) { |
| 57 | + const stats = JSON.parse(harness.readFile(filename)) as StatsMetafile; |
| 58 | + expect(stats.inputs).withContext(`${filename} must have an inputs field`).toBeDefined(); |
| 59 | + expect(stats.outputs).withContext(`${filename} must have an outputs field`).toBeDefined(); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it('output paths do not overlap between browser-stats.json and browser-initial-stats.json', async () => { |
| 64 | + const { result } = await harness.executeOnce(); |
| 65 | + |
| 66 | + expect(result?.success).toBeTrue(); |
| 67 | + |
| 68 | + const nonInitialPaths = new Set( |
| 69 | + Object.keys( |
| 70 | + (JSON.parse(harness.readFile('dist/browser-stats.json')) as StatsMetafile).outputs, |
| 71 | + ), |
| 72 | + ); |
| 73 | + const initialPaths = Object.keys( |
| 74 | + (JSON.parse(harness.readFile('dist/browser-initial-stats.json')) as StatsMetafile) |
| 75 | + .outputs, |
| 76 | + ); |
| 77 | + |
| 78 | + for (const outputPath of initialPaths) { |
| 79 | + expect(nonInitialPaths.has(outputPath)) |
| 80 | + .withContext(`Output '${outputPath}' must not appear in both stats files`) |
| 81 | + .toBeFalse(); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('inputs in each stats file are only those referenced by included outputs', async () => { |
| 86 | + const { result } = await harness.executeOnce(); |
| 87 | + |
| 88 | + expect(result?.success).toBeTrue(); |
| 89 | + |
| 90 | + for (const filename of ['dist/browser-stats.json', 'dist/browser-initial-stats.json']) { |
| 91 | + const stats = JSON.parse(harness.readFile(filename)) as StatsMetafile; |
| 92 | + const referencedInputs = new Set( |
| 93 | + Object.values(stats.outputs).flatMap((output) => Object.keys(output.inputs)), |
| 94 | + ); |
| 95 | + |
| 96 | + for (const inputPath of Object.keys(stats.inputs)) { |
| 97 | + expect(referencedInputs.has(inputPath)) |
| 98 | + .withContext( |
| 99 | + `Input '${inputPath}' in '${filename}' is not referenced by any included output`, |
| 100 | + ) |
| 101 | + .toBeTrue(); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('when statsJson is false', () => { |
| 108 | + it('does not generate any stats files', async () => { |
| 109 | + harness.useTarget('build', { |
| 110 | + ...BASE_OPTIONS, |
| 111 | + statsJson: false, |
| 112 | + }); |
| 113 | + |
| 114 | + const { result } = await harness.executeOnce(); |
| 115 | + |
| 116 | + expect(result?.success).toBeTrue(); |
| 117 | + harness.expectFile('dist/browser-stats.json').toNotExist(); |
| 118 | + harness.expectFile('dist/browser-initial-stats.json').toNotExist(); |
| 119 | + harness.expectFile('dist/stats.json').toNotExist(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('SSR build', () => { |
| 124 | + beforeEach(async () => { |
| 125 | + await harness.modifyFile('src/tsconfig.app.json', (content) => { |
| 126 | + const tsConfig = JSON.parse(content) as { files?: string[] }; |
| 127 | + tsConfig.files ??= []; |
| 128 | + tsConfig.files.push('main.server.ts'); |
| 129 | + |
| 130 | + return JSON.stringify(tsConfig); |
| 131 | + }); |
| 132 | + |
| 133 | + harness.useTarget('build', { |
| 134 | + ...BASE_OPTIONS, |
| 135 | + statsJson: true, |
| 136 | + server: 'src/main.server.ts', |
| 137 | + ssr: true, |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('generates all four stats files', async () => { |
| 142 | + const { result } = await harness.executeOnce(); |
| 143 | + |
| 144 | + expect(result?.success).toBeTrue(); |
| 145 | + harness.expectFile('dist/browser-stats.json').toExist(); |
| 146 | + harness.expectFile('dist/browser-initial-stats.json').toExist(); |
| 147 | + harness.expectFile('dist/server-stats.json').toExist(); |
| 148 | + harness.expectFile('dist/server-initial-stats.json').toExist(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('server stats files contain valid esbuild metafile structure', async () => { |
| 152 | + const { result } = await harness.executeOnce(); |
| 153 | + |
| 154 | + expect(result?.success).toBeTrue(); |
| 155 | + |
| 156 | + for (const filename of ['dist/server-stats.json', 'dist/server-initial-stats.json']) { |
| 157 | + const stats = JSON.parse(harness.readFile(filename)) as StatsMetafile; |
| 158 | + expect(stats.inputs).withContext(`${filename} must have an inputs field`).toBeDefined(); |
| 159 | + expect(stats.outputs).withContext(`${filename} must have an outputs field`).toBeDefined(); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + it('server output paths do not overlap between server-stats.json and server-initial-stats.json', async () => { |
| 164 | + const { result } = await harness.executeOnce(); |
| 165 | + |
| 166 | + expect(result?.success).toBeTrue(); |
| 167 | + |
| 168 | + const nonInitialPaths = new Set( |
| 169 | + Object.keys( |
| 170 | + (JSON.parse(harness.readFile('dist/server-stats.json')) as StatsMetafile).outputs, |
| 171 | + ), |
| 172 | + ); |
| 173 | + const initialPaths = Object.keys( |
| 174 | + (JSON.parse(harness.readFile('dist/server-initial-stats.json')) as StatsMetafile).outputs, |
| 175 | + ); |
| 176 | + |
| 177 | + for (const outputPath of initialPaths) { |
| 178 | + expect(nonInitialPaths.has(outputPath)) |
| 179 | + .withContext(`Output '${outputPath}' must not appear in both server stats files`) |
| 180 | + .toBeFalse(); |
| 181 | + } |
| 182 | + }); |
| 183 | + }); |
| 184 | + }); |
| 185 | +}); |
0 commit comments