Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): display accurate sizes for downle…
Browse files Browse the repository at this point in the history
…velled files

Fixes angular#15425
  • Loading branch information
clydin committed Sep 25, 2019
1 parent f02a8cc commit 889e751
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// tslint:disable
// TODO: cleanup this file, it's copied as is from Angular CLI.
import { tags, terminal } from '@angular-devkit/core';
import * as path from 'path';


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

export function generateBundleStats(
info: {
id: string | number;
size?: number;
files: string[];
names?: string[];
entry: boolean;
initial: boolean;
rendered?: boolean;
},
colors: boolean,
): string {
const g = (x: string) => (colors ? bold(green(x)) : x);
const y = (x: string) => (colors ? bold(yellow(x)) : x);

const size = typeof info.size === 'number' ? ` ${formatSize(info.size)}` : '';
const files = info.files.map(f => path.basename(f)).join(', ');
const names = info.names ? ` (${info.names.join(', ')})` : '';
const initial = y(info.entry ? '[entry]' : info.initial ? '[initial]' : '');
const flags = ['rendered', 'recorded']
.map(f => (f && (info as any)[f] ? g(` [${f}]`) : ''))
.join('');

return `chunk {${y(info.id.toString())}} ${g(files)}${names}${size} ${initial}${flags}`;
}

export function generateBuildStats(hash: string, time: number, colors: boolean): string {
const w = (x: string) => colors ? bold(white(x)) : x;
return `Date: ${w(new Date().toISOString())} - Hash: ${w(hash)} - Time: ${w('' + time)}ms`
}

export function statsToString(json: any, statsConfig: any) {
const colors = statsConfig.colors;
const rs = (x: string) => colors ? reset(x) : x;
const w = (x: string) => colors ? bold(white(x)) : x;
const g = (x: string) => colors ? bold(green(x)) : x;
const y = (x: string) => colors ? bold(yellow(x)) : x;

const changedChunksStats = json.chunks
.filter((chunk: any) => chunk.rendered)
.map((chunk: any) => {
const asset = json.assets.filter((x: any) => x.name == chunk.files[0])[0];
const size = asset ? ` ${formatSize(asset.size)}` : '';
const files = chunk.files.join(', ');
const names = chunk.names ? ` (${chunk.names.join(', ')})` : '';
const initial = y(chunk.entry ? '[entry]' : chunk.initial ? '[initial]' : '');
const flags = ['rendered', 'recorded']
.map(f => f && chunk[f] ? g(` [${f}]`) : '')
.join('');

return `chunk {${y(chunk.id)}} ${g(files)}${names}${size} ${initial}${flags}`;
return generateBundleStats({ ...chunk, size: asset && asset.size }, colors);
});

const unchangedChunkNumber = json.chunks.length - changedChunksStats.length;
Expand Down
89 changes: 82 additions & 7 deletions packages/angular_devkit/build_angular/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { NodeJsSyncHost } from '@angular-devkit/core/node';
import { createHash } from 'crypto';
import * as findCacheDirectory from 'find-cache-dir';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { from, of } from 'rxjs';
import { bufferCount, catchError, concatMap, map, mergeScan, switchMap } from 'rxjs/operators';
Expand All @@ -53,6 +52,8 @@ import {
import { readTsconfig } from '../angular-cli-files/utilities/read-tsconfig';
import { augmentAppWithServiceWorker } from '../angular-cli-files/utilities/service-worker';
import {
generateBuildStats,
generateBundleStats,
statsErrorsToString,
statsToString,
statsWarningsToString,
Expand All @@ -66,7 +67,12 @@ import {
normalizeSourceMaps,
} from '../utils';
import { manglingDisabled } from '../utils/mangle-options';
import { CacheKey, ProcessBundleOptions, ProcessBundleResult } from '../utils/process-bundle';
import {
CacheKey,
ProcessBundleFile,
ProcessBundleOptions,
ProcessBundleResult,
} from '../utils/process-bundle';
import { assertCompatibleAngularVersion } from '../utils/version';
import {
generateBrowserWebpackConfigFromContext,
Expand Down Expand Up @@ -200,9 +206,6 @@ export function buildWebpackBrowser(
// Check Angular version.
assertCompatibleAngularVersion(context.workspaceRoot, context.logger);

const loggingFn =
transforms.logging || createBrowserLoggingCallback(!!options.verbose, context.logger);

return from(initialize(options, context, host, transforms.webpackConfiguration)).pipe(
// tslint:disable-next-line: no-big-function
switchMap(({ workspace, config: configs }) => {
Expand Down Expand Up @@ -233,14 +236,24 @@ export function buildWebpackBrowser(
`);
}

const useBundleDownleveling =
isDifferentialLoadingNeeded && !(fullDifferential || options.watch);
const startTime = Date.now();

return from(configs).pipe(
// the concurrency parameter (3rd parameter of mergeScan) is deliberately
// set to 1 to make sure the build steps are executed in sequence.
mergeScan(
(lastResult, config) => {
// Make sure to only run the 2nd build step, if 1st one succeeded
if (lastResult.success) {
return runWebpack(config, context, { logging: loggingFn });
return runWebpack(config, context, {
logging:
transforms.logging ||
(useBundleDownleveling
? () => {}
: createBrowserLoggingCallback(!!options.verbose, context.logger)),
});
} else {
return of();
}
Expand Down Expand Up @@ -274,7 +287,7 @@ export function buildWebpackBrowser(
noModuleFiles = secondBuild.emittedFiles;
}
} else if (isDifferentialLoadingNeeded && !fullDifferential) {
const { emittedFiles = [] } = firstBuild;
const { emittedFiles = [], webpackStats } = firstBuild;
moduleFiles = [];
noModuleFiles = [];

Expand Down Expand Up @@ -611,6 +624,68 @@ export function buildWebpackBrowser(
}

context.logger.info('ES5 bundle generation complete.');

type ArrayElement<A> = A extends ReadonlyArray<infer T> ? T : never;
function generateBundleInfoStats(
name: string | undefined,
bundle: ProcessBundleFile,
chunk: ArrayElement<webpack.Stats.ToJsonOutput['chunks']> | undefined,
): string {
return generateBundleStats(
{
id: chunk ? chunk.id.toString() : '',
size: bundle.size,
files: bundle.map ? [bundle.filename, bundle.map.filename] : [bundle.filename],
names: name ? [name] : [],
entry: name === 'runtime',
initial: !!chunk && chunk.initial,
rendered: true,
},
true,
);
}

let bundleInfoText = '';
const processedNames = new Set<string>();
for (const result of processResults) {
if (result.name) {
processedNames.add(result.name);
}
const chunk =
webpackStats &&
webpackStats.chunks &&
webpackStats.chunks.find(c => result.name && c.names.includes(result.name));
if (result.original) {
bundleInfoText +=
'\n' + generateBundleInfoStats(result.name, result.original, chunk);
}
if (result.downlevel) {
bundleInfoText +=
'\n' + generateBundleInfoStats(result.name, result.downlevel, chunk);
}
}

if (webpackStats && webpackStats.chunks) {
for (const chunk of webpackStats.chunks) {
if (chunk.names.some(n => processedNames.has(n))) {
continue;
}

const asset =
webpackStats.assets && webpackStats.assets.find(a => a.name === chunk.files[0]);
bundleInfoText +=
'\n' + generateBundleStats({ ...chunk, size: asset && asset.size }, true);
}
}

bundleInfoText +=
'\n' +
generateBuildStats(
(webpackStats && webpackStats.hash) || '<unknown>',
Date.now() - startTime,
true,
);
context.logger.info(bundleInfoText);
} else {
const { emittedFiles = [] } = firstBuild;
files = emittedFiles.filter(x => x.name !== 'polyfills-es5');
Expand Down

0 comments on commit 889e751

Please sign in to comment.