Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): separate initial total size in bu…
Browse files Browse the repository at this point in the history
…ild 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)
  • Loading branch information
alan-agius4 authored and clydin committed Nov 12, 2020
1 parent 63ca8b8 commit 4c6c016
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
10 changes: 6 additions & 4 deletions packages/angular_devkit/build_angular/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import { NgBuildAnalyticsPlugin } from '../webpack/plugins/analytics';
import { markAsyncChunksNonInitial } from '../webpack/utils/async-chunks';
import {
BundleStats,
ChunkType,
generateBundleStats,
statsErrorsToString,
statsHasErrors,
Expand Down Expand Up @@ -601,11 +602,11 @@ export function buildWebpackBrowser(
const chunk = webpackStats.chunks?.find((chunk) => chunk.id.toString() === result.name);

if (result.original) {
bundleInfoStats.push(generateBundleInfoStats(result.original, chunk));
bundleInfoStats.push(generateBundleInfoStats(result.original, chunk, 'modern'));
}

if (result.downlevel) {
bundleInfoStats.push(generateBundleInfoStats(result.downlevel, chunk));
bundleInfoStats.push(generateBundleInfoStats(result.downlevel, chunk, 'legacy'));
}
}

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

// Check for budget errors and display them to the user.
Expand Down Expand Up @@ -783,6 +784,7 @@ type ArrayElement<A> = A extends ReadonlyArray<infer T> ? T : never;
function generateBundleInfoStats(
bundle: ProcessBundleFile,
chunk: ArrayElement<webpack.Stats.ToJsonOutput['chunks']> | undefined,
chunkType: ChunkType,
): BundleStats {
return generateBundleStats(
{
Expand All @@ -792,8 +794,8 @@ function generateBundleInfoStats(
entry: !!chunk?.names.includes('runtime'),
initial: !!chunk?.initial,
rendered: true,
chunkType,
},
true,
);
}
export default createBuilder<json.JsonObject & BrowserBuilderSchema>(buildWebpackBrowser);
42 changes: 35 additions & 7 deletions packages/angular_devkit/build_angular/src/webpack/utils/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ export function formatSize(size: number): string {

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

export type ChunkType = 'modern' | 'legacy' | 'unknown';

export interface BundleStats {
initial: boolean;
stats: BundleStatsData;
chunkType: ChunkType;
};

export function generateBundleStats(
Expand All @@ -42,15 +45,17 @@ export function generateBundleStats(
entry: boolean;
initial: boolean;
rendered?: boolean;
chunkType?: ChunkType,
},
colors: boolean,
): BundleStats {
const size = typeof info.size === 'number' ? info.size : '-';
const files = info.files.filter(f => !f.endsWith('.map')).map(f => path.basename(f)).join(', ');
const names = info.names?.length ? info.names.join(', ') : '-';
const initial = !!(info.entry || info.initial);
const chunkType = info.chunkType || 'unknown';

return {
chunkType,
initial,
stats: [files, names, size],
}
Expand All @@ -65,9 +70,11 @@ function generateBuildStatsTable(data: BundleStats[], colors: boolean, showTotal
const changedEntryChunksStats: BundleStatsData[] = [];
const changedLazyChunksStats: BundleStatsData[] = [];

let initialTotalSize = 0;
let initialModernTotalSize = 0;
let initialLegacyTotalSize = 0;
let modernFileSuffix: string | undefined;

for (const { initial, stats } of data) {
for (const { initial, stats, chunkType } of data) {
const [files, names, size] = stats;

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

if (typeof size === 'number') {
initialTotalSize += size;
switch (chunkType) {
case 'modern':
initialModernTotalSize += size;
if (!modernFileSuffix) {
const match = files.match(/-(es20\d{2}|esnext)/);
modernFileSuffix = match?.[1].toString().toUpperCase();
}
break;
case 'legacy':
initialLegacyTotalSize += size;
break;
default:
initialModernTotalSize += size;
initialLegacyTotalSize += size;
break;
}
}

} else {
changedLazyChunksStats.push(data);
}
Expand All @@ -99,7 +120,14 @@ function generateBuildStatsTable(data: BundleStats[], colors: boolean, showTotal

if (showTotalSize) {
bundleInfo.push([]);
bundleInfo.push([' ', 'Initial Total', formatSize(initialTotalSize)].map(bold));
if (initialModernTotalSize === initialLegacyTotalSize) {
bundleInfo.push([' ', 'Initial Total', formatSize(initialModernTotalSize)].map(bold));
} else {
bundleInfo.push(
[' ', 'Initial ES5 Total', formatSize(initialLegacyTotalSize)].map(bold),
[' ', `Initial ${modernFileSuffix} Total`, formatSize(initialModernTotalSize)].map(bold),
);
}
}
}

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

const assets = json.assets.filter((asset: any) => chunk.files.includes(asset.name));
const summedSize = assets.filter((asset: any) => !asset.name.endsWith(".map")).reduce((total: number, asset: any) => { return total + asset.size }, 0);
changedChunksStats.push(generateBundleStats({ ...chunk, size: summedSize }, colors));
changedChunksStats.push(generateBundleStats({ ...chunk, size: summedSize }));
}
unchangedChunkNumber = json.chunks.length - changedChunksStats.length;
}
Expand Down
10 changes: 9 additions & 1 deletion tests/legacy-cli/e2e/tests/basic/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ export default async function() {
'IE 11',
);
// Production build
const { stderr: stderrProgress } = await ng('build', '--prod', '--progress');
const { stderr: stderrProgress, stdout } = await ng('build', '--prod', '--progress');
await expectFileToMatch('dist/test-project/index.html', /main-es5\.[a-zA-Z0-9]{20}\.js/);
await expectFileToMatch('dist/test-project/index.html', /main-es2015\.[a-zA-Z0-9]{20}\.js/);

if (!stdout.includes('Initial ES5 Total')) {
throw new Error(`Expected stdout not to contain 'Initial ES5 Total' but it did.\n${stdout}`);
}

if (!stdout.includes('Initial ES2015 Total')) {
throw new Error(`Expected stdout not to contain 'Initial ES2015 Total' but it did.\n${stdout}`);
}

const logs: string[] = [
'Browser application bundle generation complete',
'ES5 bundle generation complete',
Expand Down

0 comments on commit 4c6c016

Please sign in to comment.