Skip to content

Commit 4c6c016

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular-devkit/build-angular): separate initial total size in build output
``` Initial Chunk Files | Names | Size main-es5.6f60fbc22c7e19b5d179.js | main | 154.89 kB main-es2015.6f60fbc22c7e19b5d179.js | main | 135.87 kB polyfills-es5.0351d1276e488726c8dc.js | polyfills-es5 | 129.34 kB polyfills-es2015.904e51532a46df6b991c.js | polyfills | 36.12 kB scripts.b7f93721b30caf483f97.js | scripts | 3.45 kB runtime-es2015.76bfea807ccb0a24e182.js | runtime | 1.45 kB runtime-es5.76bfea807ccb0a24e182.js | runtime | 1.45 kB styles.3ff695c00d717f2d2a11.css | styles | 0 bytes | Initial ES5 Total | 289.13 kB | Initial ES2015 Total | 176.88 kB ``` Closes #19330 (cherry picked from commit 2a1b6b1)
1 parent 63ca8b8 commit 4c6c016

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

packages/angular_devkit/build_angular/src/browser/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import { NgBuildAnalyticsPlugin } from '../webpack/plugins/analytics';
6969
import { markAsyncChunksNonInitial } from '../webpack/utils/async-chunks';
7070
import {
7171
BundleStats,
72+
ChunkType,
7273
generateBundleStats,
7374
statsErrorsToString,
7475
statsHasErrors,
@@ -601,11 +602,11 @@ export function buildWebpackBrowser(
601602
const chunk = webpackStats.chunks?.find((chunk) => chunk.id.toString() === result.name);
602603

603604
if (result.original) {
604-
bundleInfoStats.push(generateBundleInfoStats(result.original, chunk));
605+
bundleInfoStats.push(generateBundleInfoStats(result.original, chunk, 'modern'));
605606
}
606607

607608
if (result.downlevel) {
608-
bundleInfoStats.push(generateBundleInfoStats(result.downlevel, chunk));
609+
bundleInfoStats.push(generateBundleInfoStats(result.downlevel, chunk, 'legacy'));
609610
}
610611
}
611612

@@ -614,7 +615,7 @@ export function buildWebpackBrowser(
614615
) || [];
615616
for (const chunk of unprocessedChunks) {
616617
const asset = webpackStats.assets?.find(a => a.name === chunk.files[0]);
617-
bundleInfoStats.push(generateBundleStats({ ...chunk, size: asset?.size }, true));
618+
bundleInfoStats.push(generateBundleStats({ ...chunk, size: asset?.size }));
618619
}
619620

620621
// Check for budget errors and display them to the user.
@@ -783,6 +784,7 @@ type ArrayElement<A> = A extends ReadonlyArray<infer T> ? T : never;
783784
function generateBundleInfoStats(
784785
bundle: ProcessBundleFile,
785786
chunk: ArrayElement<webpack.Stats.ToJsonOutput['chunks']> | undefined,
787+
chunkType: ChunkType,
786788
): BundleStats {
787789
return generateBundleStats(
788790
{
@@ -792,8 +794,8 @@ function generateBundleInfoStats(
792794
entry: !!chunk?.names.includes('runtime'),
793795
initial: !!chunk?.initial,
794796
rendered: true,
797+
chunkType,
795798
},
796-
true,
797799
);
798800
}
799801
export default createBuilder<json.JsonObject & BrowserBuilderSchema>(buildWebpackBrowser);

packages/angular_devkit/build_angular/src/webpack/utils/stats.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ export function formatSize(size: number): string {
2929

3030
export type BundleStatsData = [files: string, names: string, size: number | string];
3131

32+
export type ChunkType = 'modern' | 'legacy' | 'unknown';
33+
3234
export interface BundleStats {
3335
initial: boolean;
3436
stats: BundleStatsData;
37+
chunkType: ChunkType;
3538
};
3639

3740
export function generateBundleStats(
@@ -42,15 +45,17 @@ export function generateBundleStats(
4245
entry: boolean;
4346
initial: boolean;
4447
rendered?: boolean;
48+
chunkType?: ChunkType,
4549
},
46-
colors: boolean,
4750
): BundleStats {
4851
const size = typeof info.size === 'number' ? info.size : '-';
4952
const files = info.files.filter(f => !f.endsWith('.map')).map(f => path.basename(f)).join(', ');
5053
const names = info.names?.length ? info.names.join(', ') : '-';
5154
const initial = !!(info.entry || info.initial);
55+
const chunkType = info.chunkType || 'unknown';
5256

5357
return {
58+
chunkType,
5459
initial,
5560
stats: [files, names, size],
5661
}
@@ -65,9 +70,11 @@ function generateBuildStatsTable(data: BundleStats[], colors: boolean, showTotal
6570
const changedEntryChunksStats: BundleStatsData[] = [];
6671
const changedLazyChunksStats: BundleStatsData[] = [];
6772

68-
let initialTotalSize = 0;
73+
let initialModernTotalSize = 0;
74+
let initialLegacyTotalSize = 0;
75+
let modernFileSuffix: string | undefined;
6976

70-
for (const { initial, stats } of data) {
77+
for (const { initial, stats, chunkType } of data) {
7178
const [files, names, size] = stats;
7279

7380
const data: BundleStatsData = [
@@ -80,9 +87,23 @@ function generateBuildStatsTable(data: BundleStats[], colors: boolean, showTotal
8087
changedEntryChunksStats.push(data);
8188

8289
if (typeof size === 'number') {
83-
initialTotalSize += size;
90+
switch (chunkType) {
91+
case 'modern':
92+
initialModernTotalSize += size;
93+
if (!modernFileSuffix) {
94+
const match = files.match(/-(es20\d{2}|esnext)/);
95+
modernFileSuffix = match?.[1].toString().toUpperCase();
96+
}
97+
break;
98+
case 'legacy':
99+
initialLegacyTotalSize += size;
100+
break;
101+
default:
102+
initialModernTotalSize += size;
103+
initialLegacyTotalSize += size;
104+
break;
105+
}
84106
}
85-
86107
} else {
87108
changedLazyChunksStats.push(data);
88109
}
@@ -99,7 +120,14 @@ function generateBuildStatsTable(data: BundleStats[], colors: boolean, showTotal
99120

100121
if (showTotalSize) {
101122
bundleInfo.push([]);
102-
bundleInfo.push([' ', 'Initial Total', formatSize(initialTotalSize)].map(bold));
123+
if (initialModernTotalSize === initialLegacyTotalSize) {
124+
bundleInfo.push([' ', 'Initial Total', formatSize(initialModernTotalSize)].map(bold));
125+
} else {
126+
bundleInfo.push(
127+
[' ', 'Initial ES5 Total', formatSize(initialLegacyTotalSize)].map(bold),
128+
[' ', `Initial ${modernFileSuffix} Total`, formatSize(initialModernTotalSize)].map(bold),
129+
);
130+
}
103131
}
104132
}
105133

@@ -142,7 +170,7 @@ function statsToString(json: any, statsConfig: any, bundleState?: BundleStats[])
142170

143171
const assets = json.assets.filter((asset: any) => chunk.files.includes(asset.name));
144172
const summedSize = assets.filter((asset: any) => !asset.name.endsWith(".map")).reduce((total: number, asset: any) => { return total + asset.size }, 0);
145-
changedChunksStats.push(generateBundleStats({ ...chunk, size: summedSize }, colors));
173+
changedChunksStats.push(generateBundleStats({ ...chunk, size: summedSize }));
146174
}
147175
unchangedChunkNumber = json.chunks.length - changedChunksStats.length;
148176
}

tests/legacy-cli/e2e/tests/basic/build.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ export default async function() {
1919
'IE 11',
2020
);
2121
// Production build
22-
const { stderr: stderrProgress } = await ng('build', '--prod', '--progress');
22+
const { stderr: stderrProgress, stdout } = await ng('build', '--prod', '--progress');
2323
await expectFileToMatch('dist/test-project/index.html', /main-es5\.[a-zA-Z0-9]{20}\.js/);
2424
await expectFileToMatch('dist/test-project/index.html', /main-es2015\.[a-zA-Z0-9]{20}\.js/);
2525

26+
if (!stdout.includes('Initial ES5 Total')) {
27+
throw new Error(`Expected stdout not to contain 'Initial ES5 Total' but it did.\n${stdout}`);
28+
}
29+
30+
if (!stdout.includes('Initial ES2015 Total')) {
31+
throw new Error(`Expected stdout not to contain 'Initial ES2015 Total' but it did.\n${stdout}`);
32+
}
33+
2634
const logs: string[] = [
2735
'Browser application bundle generation complete',
2836
'ES5 bundle generation complete',

0 commit comments

Comments
 (0)