Skip to content

Commit 2a68219

Browse files
alan-agius4filipesilva
authored andcommitted
fix(@angular-devkit/build-angular): show verbose logging when using --verbose and differential loading
(cherry picked from commit 4f615e4)
1 parent a220f46 commit 2a68219

File tree

2 files changed

+49
-59
lines changed

2 files changed

+49
-59
lines changed

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

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,12 @@ import { NgBuildAnalyticsPlugin } from '../webpack/plugins/analytics';
6969
import { markAsyncChunksNonInitial } from '../webpack/utils/async-chunks';
7070
import {
7171
BundleStats,
72-
generateBuildStats,
73-
generateBuildStatsTable,
7472
generateBundleStats,
7573
statsErrorsToString,
7674
statsHasErrors,
7775
statsHasWarnings,
78-
statsToString,
7976
statsWarningsToString,
77+
webpackStatsLogger,
8078
} from '../webpack/utils/stats';
8179
import { Schema as BrowserBuilderSchema } from './schema';
8280

@@ -269,7 +267,6 @@ export function buildWebpackBrowser(
269267
}),
270268
// tslint:disable-next-line: no-big-function
271269
switchMap(({ config, projectRoot, projectSourceRoot, i18n, buildBrowserFeatures, isDifferentialLoadingNeeded, target }) => {
272-
const startTime = Date.now();
273270
const normalizedOptimization = normalizeOptimization(options.optimization);
274271
const indexTransforms = getHtmlTransforms(
275272
normalizedOptimization,
@@ -279,8 +276,13 @@ export function buildWebpackBrowser(
279276

280277
return runWebpack(config, context, {
281278
webpackFactory: require('webpack') as typeof webpack,
282-
logging:
283-
transforms.logging || (() => { }),
279+
logging: transforms.logging || (
280+
(stats, config) => {
281+
if (options.verbose) {
282+
context.logger.info(stats.toString(config.stats));
283+
}
284+
}
285+
),
284286
}).pipe(
285287
// tslint:disable-next-line: no-big-function
286288
concatMap(async buildEvent => {
@@ -730,33 +732,10 @@ export function buildWebpackBrowser(
730732
}
731733
}
732734

733-
if (bundleInfoStats.length) {
734-
context.logger.info(
735-
'\n' +
736-
generateBuildStatsTable(bundleInfoStats, colors.enabled) +
737-
'\n\n' +
738-
generateBuildStats(
739-
webpackStats?.hash || '<unknown>',
740-
Date.now() - startTime,
741-
true,
742-
),
743-
);
744-
} else {
745-
context.logger.info(statsToString(webpackStats, config.stats));
746-
}
735+
webpackStatsLogger(context.logger, webpackStats, config, bundleInfoStats);
747736

748-
if (statsHasWarnings(webpackStats)) {
749-
context.logger.warn(statsWarningsToString(webpackStats, { colors: true }));
750-
}
751-
752-
if (statsHasErrors(webpackStats)) {
753-
context.logger.error(statsErrorsToString(webpackStats, { colors: true }));
754-
755-
return { success: false };
756-
}
737+
return { success: !statsHasErrors(webpackStats) };
757738
}
758-
759-
return { success };
760739
}),
761740
map(
762741
event =>

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

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { WebpackLoggingCallback } from '@angular-devkit/build-webpack';
1212
import * as path from 'path';
1313
import * as textTable from 'text-table';
1414
import { colors as ansiColors, removeColor } from '../../utils/color';
15+
import { Configuration, Stats } from 'webpack';
1516

1617
export function formatSize(size: number): string {
1718
if (size <= 0) {
@@ -56,10 +57,10 @@ export function generateBundleStats(
5657
}
5758
}
5859

59-
export function generateBuildStatsTable(data: BundleStats[], colors: boolean): string {
60+
function generateBuildStatsTable(data: BundleStats[], colors: boolean): string {
6061
const changedEntryChunksStats: BundleStatsData[] = [];
6162
const changedLazyChunksStats: BundleStatsData[] = [];
62-
for (const {initial, stats} of data) {
63+
for (const { initial, stats } of data) {
6364
if (initial) {
6465
changedEntryChunksStats.push(stats);
6566
} else {
@@ -89,7 +90,7 @@ export function generateBuildStatsTable(data: BundleStats[], colors: boolean): s
8990
if (changedLazyChunksStats.length) {
9091
bundleInfo.push(
9192
['Lazy Chunk Files', 'Names', 'Size'].map(bold),
92-
...changedLazyChunksStats,
93+
...changedLazyChunksStats,
9394
);
9495
}
9596

@@ -99,27 +100,30 @@ export function generateBuildStatsTable(data: BundleStats[], colors: boolean): s
99100
});
100101
}
101102

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

107-
export function statsToString(json: any, statsConfig: any) {
108+
function statsToString(json: any, statsConfig: any, bundleState?: BundleStats[]): string {
108109
const colors = statsConfig.colors;
109110
const rs = (x: string) => colors ? ansiColors.reset(x) : x;
110111

111-
const changedChunksStats: BundleStats[] = [];
112-
for (const chunk of json.chunks) {
113-
if (!chunk.rendered) {
114-
continue;
115-
}
112+
const changedChunksStats: BundleStats[] = bundleState ?? [];
113+
let unchangedChunkNumber = 0;
114+
if (!bundleState?.length) {
115+
for (const chunk of json.chunks) {
116+
if (!chunk.rendered) {
117+
continue;
118+
}
116119

117-
const assets = json.assets.filter((asset: any) => chunk.files.includes(asset.name));
118-
const summedSize = assets.filter((asset: any) => !asset.name.endsWith(".map")).reduce((total: number, asset: any) => { return total + asset.size }, 0);
119-
changedChunksStats.push(generateBundleStats({ ...chunk, size: summedSize }, colors));
120+
const assets = json.assets.filter((asset: any) => chunk.files.includes(asset.name));
121+
const summedSize = assets.filter((asset: any) => !asset.name.endsWith(".map")).reduce((total: number, asset: any) => { return total + asset.size }, 0);
122+
changedChunksStats.push(generateBundleStats({ ...chunk, size: summedSize }, colors));
123+
}
124+
unchangedChunkNumber = json.chunks.length - changedChunksStats.length;
120125
}
121126

122-
const unchangedChunkNumber = json.chunks.length - changedChunksStats.length;
123127
const statsTable = generateBuildStatsTable(changedChunksStats, colors);
124128

125129
// In some cases we do things outside of webpack context
@@ -182,7 +186,7 @@ export function statsWarningsToString(json: any, statsConfig: any): string {
182186
output += yb(`Warning: ${warning}\n\n`);
183187
} else {
184188
if (!ERRONEOUS_WARNINGS_FILTER(warning.message)) {
185-
continue;
189+
continue;
186190
}
187191
const file = warning.file || warning.moduleName;
188192
if (file) {
@@ -200,7 +204,7 @@ export function statsWarningsToString(json: any, statsConfig: any): string {
200204
}
201205

202206
if (output) {
203-
return '\n' + output;
207+
return '\n' + output;
204208
}
205209

206210
return '';
@@ -241,7 +245,7 @@ export function statsErrorsToString(json: any, statsConfig: any): string {
241245
}
242246

243247
if (output) {
244-
return '\n' + output;
248+
return '\n' + output;
245249
}
246250

247251
return '';
@@ -261,19 +265,26 @@ export function createWebpackLoggingCallback(
261265
logger: logging.LoggerApi,
262266
): WebpackLoggingCallback {
263267
return (stats, config) => {
264-
// config.stats contains our own stats settings, added during buildWebpackConfig().
265-
const json = stats.toJson(config.stats);
266268
if (verbose) {
267269
logger.info(stats.toString(config.stats));
268-
} else {
269-
logger.info(statsToString(json, config.stats));
270270
}
271271

272-
if (statsHasWarnings(json)) {
273-
logger.warn(statsWarningsToString(json, config.stats));
274-
}
275-
if (statsHasErrors(json)) {
276-
logger.error(statsErrorsToString(json, config.stats));
277-
}
278-
};
272+
webpackStatsLogger(logger, stats.toJson(config.stats), config);
273+
}
279274
}
275+
276+
export function webpackStatsLogger(
277+
logger: logging.LoggerApi,
278+
json: Stats.ToJsonOutput,
279+
config: Configuration,
280+
bundleStats?: BundleStats[],
281+
): void {
282+
logger.info(statsToString(json, config.stats, bundleStats));
283+
284+
if (statsHasWarnings(json)) {
285+
logger.warn(statsWarningsToString(json, config.stats));
286+
}
287+
if (statsHasErrors(json)) {
288+
logger.error(statsErrorsToString(json, config.stats));
289+
}
290+
};

0 commit comments

Comments
 (0)