Skip to content

Commit 24c509f

Browse files
fix(@angular/build): Fixing the missing browser initial stats file that was in my main branch off of @angular/angular-cli repository
This addresses an issue of including the (browser|server)-initial-stats.json file that was in my original repository. It also fixes unit tests that were also addressed in the original repo.
1 parent 9545676 commit 24c509f

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

packages/angular/build/src/builders/application/execute-build.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,31 @@ export async function executeBuild(
308308
if (options.stats) {
309309
executionResult.addOutputFile(
310310
'browser-stats.json',
311-
JSON.stringify(buildMetafileForType(metafile, 'browser', outputFiles), null, 2),
311+
JSON.stringify(buildMetafileForType(metafile, 'browser', false, outputFiles), null, 2),
312+
BuildOutputFileType.Root,
313+
);
314+
executionResult.addOutputFile(
315+
'browser-initial-stats.json',
316+
JSON.stringify(
317+
buildMetafileForType(metafile, 'browser', true, outputFiles, initialFiles),
318+
null,
319+
2,
320+
),
312321
BuildOutputFileType.Root,
313322
);
314323
if (ssrOutputEnabled) {
315324
executionResult.addOutputFile(
316325
'server-stats.json',
317-
JSON.stringify(buildMetafileForType(metafile, 'server', outputFiles), null, 2),
326+
JSON.stringify(buildMetafileForType(metafile, 'server', false, outputFiles), null, 2),
327+
BuildOutputFileType.Root,
328+
);
329+
executionResult.addOutputFile(
330+
'server-initial-stats.json',
331+
JSON.stringify(
332+
buildMetafileForType(metafile, 'server', true, outputFiles, initialFiles),
333+
null,
334+
2,
335+
),
318336
BuildOutputFileType.Root,
319337
);
320338
}

packages/angular/build/src/tools/esbuild/utils.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,33 @@ import {
3232
export function buildMetafileForType(
3333
metafile: Metafile,
3434
type: 'browser' | 'server',
35+
initial: boolean,
3536
outputFiles: BuildOutputFile[],
37+
initialFiles?: Map<string, InitialFileRecord>,
3638
): Metafile {
39+
const isServer = type === 'server';
40+
3741
const outputPathsForType = new Set(
3842
outputFiles
3943
.filter(({ type: fileType }) => {
4044
const isServerFile =
4145
fileType === BuildOutputFileType.ServerApplication ||
4246
fileType === BuildOutputFileType.ServerRoot;
4347

44-
return type === 'server' ? isServerFile : !isServerFile;
48+
return isServer ? isServerFile : !isServerFile;
4549
})
4650
.map(({ path }) => path),
4751
);
4852

4953
const filteredOutputs: Metafile['outputs'] = {};
5054
for (const [outputPath, output] of Object.entries(metafile.outputs)) {
51-
if (outputPathsForType.has(outputPath)) {
52-
filteredOutputs[outputPath] = output;
55+
if (!outputPathsForType.has(outputPath)) {
56+
continue;
57+
}
58+
if (initial && !initialFiles?.has(outputPath)) {
59+
continue;
5360
}
61+
filteredOutputs[outputPath] = output;
5462
}
5563

5664
const referencedInputs = new Set<string>();

packages/angular_devkit/build_angular/src/builders/browser/tests/options/stats-json_spec.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
2626

2727
expect(result?.success).toBe(true);
2828

29-
if (harness.expectFile('dist/stats.json').toExist()) {
30-
const content = harness.readFile('dist/stats.json');
29+
if (harness.expectFile('dist/browser-stats.json').toExist()) {
30+
const content = harness.readFile('dist/browser-stats.json');
3131
expect(() => JSON.parse(content))
3232
.withContext('Expected Webpack Stats file to be valid JSON.')
3333
.not.toThrow();
3434
}
35+
if (harness.expectFile('dist/browser-initial-stats.json').toExist()) {
36+
const initialContent = harness.readFile('dist/browser-initial-stats.json');
37+
expect(() => JSON.parse(initialContent))
38+
.withContext('Expected Webpack Stats file to be valid JSON.')
39+
.not.toThrow();
40+
}
3541
});
3642

3743
// TODO: Investigate why this profiling object is no longer present in Webpack 5.90.3+ and if this should even be tested
@@ -45,8 +51,8 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
4551

4652
expect(result?.success).toBe(true);
4753

48-
if (harness.expectFile('dist/stats.json').toExist()) {
49-
const stats = JSON.parse(harness.readFile('dist/stats.json'));
54+
if (harness.expectFile('dist/browser-stats.json').toExist()) {
55+
const stats = JSON.parse(harness.readFile('dist/browser-stats.json'));
5056
expect(stats?.chunks?.[0]?.modules?.[0]?.profile?.building).toBeDefined();
5157
}
5258
});
@@ -61,7 +67,8 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
6167

6268
expect(result?.success).toBe(true);
6369

64-
harness.expectFile('dist/stats.json').toNotExist();
70+
harness.expectFile('dist/browser-stats.json').toNotExist();
71+
harness.expectFile('dist/browser-initial-stats.json').toNotExist();
6572
});
6673

6774
it('does not generate a Webpack Stats file in output when not present', async () => {
@@ -73,7 +80,8 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
7380

7481
expect(result?.success).toBe(true);
7582

76-
harness.expectFile('dist/stats.json').toNotExist();
83+
harness.expectFile('dist/browser-stats.json').toNotExist();
84+
harness.expectFile('dist/browser-initial-stats.json').toNotExist();
7785
});
7886
});
7987
});

packages/angular_devkit/build_angular/src/tools/webpack/configs/common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Config
245245
if (buildOptions.statsJson) {
246246
extraPlugins.push(
247247
new JsonStatsPlugin(path.resolve(root, buildOptions.outputPath, 'browser-stats.json')),
248+
new JsonStatsPlugin(
249+
path.resolve(root, buildOptions.outputPath, 'browser-initial-stats.json'),
250+
),
248251
);
249252
}
250253

0 commit comments

Comments
 (0)