Skip to content

Commit 627a9fb

Browse files
committed
fix(@angular-devkit/build-angular): display accurate sizes for downlevelled files
Fixes angular#15425
1 parent 1583f04 commit 627a9fb

File tree

4 files changed

+123
-21
lines changed

4 files changed

+123
-21
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/utilities/package-chunk-sort.ts

+2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ export function generateEntryPoints(appConfig: {
2626
};
2727

2828
const entryPoints = [
29+
'runtime',
2930
'polyfills-nomodule-es5',
3031
'polyfills-es5',
3132
'polyfills',
3233
'sw-register',
3334
...extraEntryPoints(appConfig.styles, 'styles'),
3435
...extraEntryPoints(appConfig.scripts, 'scripts'),
36+
'vendor',
3537
'main',
3638
];
3739

packages/angular_devkit/build_angular/src/angular-cli-files/utilities/stats.ts

+32-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// tslint:disable
99
// TODO: cleanup this file, it's copied as is from Angular CLI.
1010
import { tags, terminal } from '@angular-devkit/core';
11+
import * as path from 'path';
1112

1213

1314
const { bold, green, red, reset, white, yellow } = terminal;
@@ -23,27 +24,47 @@ export function formatSize(size: number): string {
2324
return `${+(size / Math.pow(1024, index)).toPrecision(3)} ${abbreviations[index]}`;
2425
}
2526

27+
export function generateBundleStats(
28+
info: {
29+
id: string | number;
30+
size?: number;
31+
files: string[];
32+
names?: string[];
33+
entry: boolean;
34+
initial: boolean;
35+
rendered?: boolean;
36+
},
37+
colors: boolean,
38+
): string {
39+
const g = (x: string) => (colors ? bold(green(x)) : x);
40+
const y = (x: string) => (colors ? bold(yellow(x)) : x);
41+
42+
const size = typeof info.size === 'number' ? ` ${formatSize(info.size)}` : '';
43+
const files = info.files.map(f => path.basename(f)).join(', ');
44+
const names = info.names ? ` (${info.names.join(', ')})` : '';
45+
const initial = y(info.entry ? '[entry]' : info.initial ? '[initial]' : '');
46+
const flags = ['rendered', 'recorded']
47+
.map(f => (f && (info as any)[f] ? g(` [${f}]`) : ''))
48+
.join('');
49+
50+
return `chunk {${y(info.id.toString())}} ${g(files)}${names}${size} ${initial}${flags}`;
51+
}
52+
53+
export function generateBuildStats(hash: string, time: number, colors: boolean): string {
54+
const w = (x: string) => colors ? bold(white(x)) : x;
55+
return `Date: ${w(new Date().toISOString())} - Hash: ${w(hash)} - Time: ${w('' + time)}ms`
56+
}
2657

2758
export function statsToString(json: any, statsConfig: any) {
2859
const colors = statsConfig.colors;
2960
const rs = (x: string) => colors ? reset(x) : x;
3061
const w = (x: string) => colors ? bold(white(x)) : x;
31-
const g = (x: string) => colors ? bold(green(x)) : x;
32-
const y = (x: string) => colors ? bold(yellow(x)) : x;
3362

3463
const changedChunksStats = json.chunks
3564
.filter((chunk: any) => chunk.rendered)
3665
.map((chunk: any) => {
3766
const asset = json.assets.filter((x: any) => x.name == chunk.files[0])[0];
38-
const size = asset ? ` ${formatSize(asset.size)}` : '';
39-
const files = chunk.files.join(', ');
40-
const names = chunk.names ? ` (${chunk.names.join(', ')})` : '';
41-
const initial = y(chunk.entry ? '[entry]' : chunk.initial ? '[initial]' : '');
42-
const flags = ['rendered', 'recorded']
43-
.map(f => f && chunk[f] ? g(` [${f}]`) : '')
44-
.join('');
45-
46-
return `chunk {${y(chunk.id)}} ${g(files)}${names}${size} ${initial}${flags}`;
67+
return generateBundleStats({ ...chunk, size: asset && asset.size }, colors);
4768
});
4869

4970
const unchangedChunkNumber = json.chunks.length - changedChunksStats.length;

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

+87-8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import {
5151
import { readTsconfig } from '../angular-cli-files/utilities/read-tsconfig';
5252
import { augmentAppWithServiceWorker } from '../angular-cli-files/utilities/service-worker';
5353
import {
54+
generateBuildStats,
55+
generateBundleStats,
5456
statsErrorsToString,
5557
statsToString,
5658
statsWarningsToString,
@@ -64,7 +66,12 @@ import {
6466
normalizeSourceMaps,
6567
} from '../utils';
6668
import { manglingDisabled } from '../utils/mangle-options';
67-
import { CacheKey, ProcessBundleOptions, ProcessBundleResult } from '../utils/process-bundle';
69+
import {
70+
CacheKey,
71+
ProcessBundleFile,
72+
ProcessBundleOptions,
73+
ProcessBundleResult,
74+
} from '../utils/process-bundle';
6875
import { assertCompatibleAngularVersion } from '../utils/version';
6976
import {
7077
generateBrowserWebpackConfigFromContext,
@@ -202,9 +209,6 @@ export function buildWebpackBrowser(
202209
// Check Angular version.
203210
assertCompatibleAngularVersion(context.workspaceRoot, context.logger);
204211

205-
const loggingFn =
206-
transforms.logging || createBrowserLoggingCallback(!!options.verbose, context.logger);
207-
208212
return from(initialize(options, context, host, transforms.webpackConfiguration)).pipe(
209213
// tslint:disable-next-line: no-big-function
210214
switchMap(({ config: configs, projectRoot }) => {
@@ -222,14 +226,24 @@ export function buildWebpackBrowser(
222226
`);
223227
}
224228

229+
const useBundleDownleveling =
230+
isDifferentialLoadingNeeded && !(fullDifferential || options.watch);
231+
const startTime = Date.now();
232+
225233
return from(configs).pipe(
226234
// the concurrency parameter (3rd parameter of mergeScan) is deliberately
227235
// set to 1 to make sure the build steps are executed in sequence.
228236
mergeScan(
229237
(lastResult, config) => {
230238
// Make sure to only run the 2nd build step, if 1st one succeeded
231239
if (lastResult.success) {
232-
return runWebpack(config, context, { logging: loggingFn });
240+
return runWebpack(config, context, {
241+
logging:
242+
transforms.logging ||
243+
(useBundleDownleveling
244+
? () => {}
245+
: createBrowserLoggingCallback(!!options.verbose, context.logger)),
246+
});
233247
} else {
234248
return of();
235249
}
@@ -263,7 +277,7 @@ export function buildWebpackBrowser(
263277
noModuleFiles = secondBuild.emittedFiles;
264278
}
265279
} else if (isDifferentialLoadingNeeded && !fullDifferential) {
266-
const { emittedFiles = [] } = firstBuild;
280+
const { emittedFiles = [], webpackStats } = firstBuild;
267281
moduleFiles = [];
268282
noModuleFiles = [];
269283

@@ -342,7 +356,9 @@ export function buildWebpackBrowser(
342356
filename,
343357
code,
344358
map,
345-
name: file.name,
359+
// id is always present for non-assets
360+
// tslint:disable-next-line: no-non-null-assertion
361+
name: file.id!,
346362
optimizeOnly: true,
347363
});
348364

@@ -356,7 +372,9 @@ export function buildWebpackBrowser(
356372
filename,
357373
code,
358374
map,
359-
name: file.name,
375+
// id is always present for non-assets
376+
// tslint:disable-next-line: no-non-null-assertion
377+
name: file.id!,
360378
runtime: file.file.startsWith('runtime'),
361379
ignoreOriginal: es5Polyfills,
362380
});
@@ -600,6 +618,67 @@ export function buildWebpackBrowser(
600618
}
601619

602620
context.logger.info('ES5 bundle generation complete.');
621+
622+
type ArrayElement<A> = A extends ReadonlyArray<infer T> ? T : never;
623+
function generateBundleInfoStats(
624+
id: string | number,
625+
bundle: ProcessBundleFile,
626+
chunk: ArrayElement<webpack.Stats.ToJsonOutput['chunks']> | undefined,
627+
): string {
628+
return generateBundleStats(
629+
{
630+
id,
631+
size: bundle.size,
632+
files: bundle.map ? [bundle.filename, bundle.map.filename] : [bundle.filename],
633+
names: chunk && chunk.names,
634+
entry: !!chunk && chunk.names.includes('runtime'),
635+
initial: !!chunk && chunk.initial,
636+
rendered: true,
637+
},
638+
true,
639+
);
640+
}
641+
642+
let bundleInfoText = '';
643+
const processedNames = new Set<string>();
644+
for (const result of processResults) {
645+
processedNames.add(result.name);
646+
647+
const chunk =
648+
webpackStats &&
649+
webpackStats.chunks &&
650+
webpackStats.chunks.find(c => result.name === c.id.toString());
651+
if (result.original) {
652+
bundleInfoText +=
653+
'\n' + generateBundleInfoStats(result.name, result.original, chunk);
654+
}
655+
if (result.downlevel) {
656+
bundleInfoText +=
657+
'\n' + generateBundleInfoStats(result.name, result.downlevel, chunk);
658+
}
659+
}
660+
661+
if (webpackStats && webpackStats.chunks) {
662+
for (const chunk of webpackStats.chunks) {
663+
if (processedNames.has(chunk.id.toString())) {
664+
continue;
665+
}
666+
667+
const asset =
668+
webpackStats.assets && webpackStats.assets.find(a => a.name === chunk.files[0]);
669+
bundleInfoText +=
670+
'\n' + generateBundleStats({ ...chunk, size: asset && asset.size }, true);
671+
}
672+
}
673+
674+
bundleInfoText +=
675+
'\n' +
676+
generateBuildStats(
677+
(webpackStats && webpackStats.hash) || '<unknown>',
678+
Date.now() - startTime,
679+
true,
680+
);
681+
context.logger.info(bundleInfoText);
603682
} else {
604683
const { emittedFiles = [] } = firstBuild;
605684
files = emittedFiles.filter(x => x.name !== 'polyfills-es5');

packages/angular_devkit/build_angular/src/utils/process-bundle.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface ProcessBundleOptions {
1919
filename: string;
2020
code: string;
2121
map?: string;
22-
name?: string;
22+
name: string;
2323
sourceMaps?: boolean;
2424
hiddenSourceMaps?: boolean;
2525
vendorSourceMaps?: boolean;
@@ -34,7 +34,7 @@ export interface ProcessBundleOptions {
3434
}
3535

3636
export interface ProcessBundleResult {
37-
name?: string;
37+
name: string;
3838
integrity?: string;
3939
original?: ProcessBundleFile;
4040
downlevel?: ProcessBundleFile;

0 commit comments

Comments
 (0)