1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
10 | if (k2 === undefined) k2 = k;
|
---|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
12 | }) : (function(o, m, k, k2) {
|
---|
13 | if (k2 === undefined) k2 = k;
|
---|
14 | o[k2] = m[k];
|
---|
15 | }));
|
---|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
18 | }) : function(o, v) {
|
---|
19 | o["default"] = v;
|
---|
20 | });
|
---|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
22 | if (mod && mod.__esModule) return mod;
|
---|
23 | var result = {};
|
---|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
25 | __setModuleDefault(result, mod);
|
---|
26 | return result;
|
---|
27 | };
|
---|
28 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
---|
29 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
---|
30 | };
|
---|
31 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
32 | exports.webpackStatsLogger = exports.createWebpackLoggingCallback = exports.statsHasWarnings = exports.statsHasErrors = exports.statsErrorsToString = exports.statsWarningsToString = exports.generateBundleStats = exports.formatSize = void 0;
|
---|
33 | const core_1 = require("@angular-devkit/core");
|
---|
34 | const path = __importStar(require("path"));
|
---|
35 | const text_table_1 = __importDefault(require("text-table"));
|
---|
36 | const color_1 = require("../../utils/color");
|
---|
37 | const stats_1 = require("../configs/stats");
|
---|
38 | const async_chunks_1 = require("./async-chunks");
|
---|
39 | const helpers_1 = require("./helpers");
|
---|
40 | function formatSize(size) {
|
---|
41 | if (size <= 0) {
|
---|
42 | return '0 bytes';
|
---|
43 | }
|
---|
44 | const abbreviations = ['bytes', 'kB', 'MB', 'GB'];
|
---|
45 | const index = Math.floor(Math.log(size) / Math.log(1024));
|
---|
46 | const roundedSize = size / Math.pow(1024, index);
|
---|
47 | // bytes don't have a fraction
|
---|
48 | const fractionDigits = index === 0 ? 0 : 2;
|
---|
49 | return `${roundedSize.toFixed(fractionDigits)} ${abbreviations[index]}`;
|
---|
50 | }
|
---|
51 | exports.formatSize = formatSize;
|
---|
52 | function generateBundleStats(info) {
|
---|
53 | var _a, _b, _c;
|
---|
54 | const size = typeof info.size === 'number' ? info.size : '-';
|
---|
55 | const files = (_b = (_a = info.files) === null || _a === void 0 ? void 0 : _a.filter((f) => !f.endsWith('.map')).map((f) => path.basename(f)).join(', ')) !== null && _b !== void 0 ? _b : '';
|
---|
56 | const names = ((_c = info.names) === null || _c === void 0 ? void 0 : _c.length) ? info.names.join(', ') : '-';
|
---|
57 | const initial = !!info.initial;
|
---|
58 | const chunkType = info.chunkType || 'unknown';
|
---|
59 | return {
|
---|
60 | chunkType,
|
---|
61 | initial,
|
---|
62 | stats: [files, names, size],
|
---|
63 | };
|
---|
64 | }
|
---|
65 | exports.generateBundleStats = generateBundleStats;
|
---|
66 | function generateBuildStatsTable(data, colors, showTotalSize) {
|
---|
67 | const g = (x) => (colors ? color_1.colors.greenBright(x) : x);
|
---|
68 | const c = (x) => (colors ? color_1.colors.cyanBright(x) : x);
|
---|
69 | const bold = (x) => (colors ? color_1.colors.bold(x) : x);
|
---|
70 | const dim = (x) => (colors ? color_1.colors.dim(x) : x);
|
---|
71 | const changedEntryChunksStats = [];
|
---|
72 | const changedLazyChunksStats = [];
|
---|
73 | let initialModernTotalSize = 0;
|
---|
74 | let initialLegacyTotalSize = 0;
|
---|
75 | let modernFileSuffix;
|
---|
76 | for (const { initial, stats, chunkType } of data) {
|
---|
77 | const [files, names, size] = stats;
|
---|
78 | const data = [
|
---|
79 | g(files),
|
---|
80 | names,
|
---|
81 | c(typeof size === 'number' ? formatSize(size) : size),
|
---|
82 | ];
|
---|
83 | if (initial) {
|
---|
84 | changedEntryChunksStats.push(data);
|
---|
85 | if (typeof size === 'number') {
|
---|
86 | switch (chunkType) {
|
---|
87 | case 'modern':
|
---|
88 | initialModernTotalSize += size;
|
---|
89 | if (!modernFileSuffix) {
|
---|
90 | const match = files.match(/-(es20\d{2}|esnext)/);
|
---|
91 | modernFileSuffix = match === null || match === void 0 ? void 0 : match[1].toString().toUpperCase();
|
---|
92 | }
|
---|
93 | break;
|
---|
94 | case 'legacy':
|
---|
95 | initialLegacyTotalSize += size;
|
---|
96 | break;
|
---|
97 | default:
|
---|
98 | initialModernTotalSize += size;
|
---|
99 | initialLegacyTotalSize += size;
|
---|
100 | break;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | else {
|
---|
105 | changedLazyChunksStats.push(data);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | const bundleInfo = [];
|
---|
109 | // Entry chunks
|
---|
110 | if (changedEntryChunksStats.length) {
|
---|
111 | bundleInfo.push(['Initial Chunk Files', 'Names', 'Size'].map(bold), ...changedEntryChunksStats);
|
---|
112 | if (showTotalSize) {
|
---|
113 | bundleInfo.push([]);
|
---|
114 | if (initialModernTotalSize === initialLegacyTotalSize) {
|
---|
115 | bundleInfo.push([' ', 'Initial Total', formatSize(initialModernTotalSize)].map(bold));
|
---|
116 | }
|
---|
117 | else {
|
---|
118 | bundleInfo.push([' ', 'Initial ES5 Total', formatSize(initialLegacyTotalSize)].map(bold), [' ', `Initial ${modernFileSuffix} Total`, formatSize(initialModernTotalSize)].map(bold));
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 | // Seperator
|
---|
123 | if (changedEntryChunksStats.length && changedLazyChunksStats.length) {
|
---|
124 | bundleInfo.push([]);
|
---|
125 | }
|
---|
126 | // Lazy chunks
|
---|
127 | if (changedLazyChunksStats.length) {
|
---|
128 | bundleInfo.push(['Lazy Chunk Files', 'Names', 'Size'].map(bold), ...changedLazyChunksStats);
|
---|
129 | }
|
---|
130 | return text_table_1.default(bundleInfo, {
|
---|
131 | hsep: dim(' | '),
|
---|
132 | stringLength: (s) => color_1.removeColor(s).length,
|
---|
133 | align: ['l', 'l', 'r'],
|
---|
134 | });
|
---|
135 | }
|
---|
136 | function generateBuildStats(hash, time, colors) {
|
---|
137 | const w = (x) => (colors ? color_1.colors.bold.white(x) : x);
|
---|
138 | return `Build at: ${w(new Date().toISOString())} - Hash: ${w(hash)} - Time: ${w('' + time)}ms`;
|
---|
139 | }
|
---|
140 | // We use this cache because we can have multiple builders running in the same process,
|
---|
141 | // where each builder has different output path.
|
---|
142 | // Ideally, we should create the logging callback as a factory, but that would need a refactoring.
|
---|
143 | const runsCache = new Set();
|
---|
144 | function statsToString(json,
|
---|
145 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
146 | statsConfig, bundleState) {
|
---|
147 | var _a, _b;
|
---|
148 | if (!((_a = json.chunks) === null || _a === void 0 ? void 0 : _a.length)) {
|
---|
149 | return '';
|
---|
150 | }
|
---|
151 | const colors = statsConfig.colors;
|
---|
152 | const rs = (x) => (colors ? color_1.colors.reset(x) : x);
|
---|
153 | const changedChunksStats = bundleState !== null && bundleState !== void 0 ? bundleState : [];
|
---|
154 | let unchangedChunkNumber = 0;
|
---|
155 | if (!(bundleState === null || bundleState === void 0 ? void 0 : bundleState.length)) {
|
---|
156 | const isFirstRun = !runsCache.has(json.outputPath || '');
|
---|
157 | for (const chunk of json.chunks) {
|
---|
158 | // During first build we want to display unchanged chunks
|
---|
159 | // but unchanged cached chunks are always marked as not rendered.
|
---|
160 | if (!isFirstRun && !chunk.rendered) {
|
---|
161 | continue;
|
---|
162 | }
|
---|
163 | const assets = (_b = json.assets) === null || _b === void 0 ? void 0 : _b.filter((asset) => { var _a; return (_a = chunk.files) === null || _a === void 0 ? void 0 : _a.includes(asset.name); });
|
---|
164 | const summedSize = assets === null || assets === void 0 ? void 0 : assets.filter((asset) => !asset.name.endsWith('.map')).reduce((total, asset) => total + asset.size, 0);
|
---|
165 | changedChunksStats.push(generateBundleStats({ ...chunk, size: summedSize }));
|
---|
166 | }
|
---|
167 | unchangedChunkNumber = json.chunks.length - changedChunksStats.length;
|
---|
168 | runsCache.add(json.outputPath || '');
|
---|
169 | }
|
---|
170 | // Sort chunks by size in descending order
|
---|
171 | changedChunksStats.sort((a, b) => {
|
---|
172 | if (a.stats[2] > b.stats[2]) {
|
---|
173 | return -1;
|
---|
174 | }
|
---|
175 | if (a.stats[2] < b.stats[2]) {
|
---|
176 | return 1;
|
---|
177 | }
|
---|
178 | return 0;
|
---|
179 | });
|
---|
180 | const statsTable = generateBuildStatsTable(changedChunksStats, colors, unchangedChunkNumber === 0);
|
---|
181 | // In some cases we do things outside of webpack context
|
---|
182 | // Such us index generation, service worker augmentation etc...
|
---|
183 | // This will correct the time and include these.
|
---|
184 | let time = 0;
|
---|
185 | if (json.builtAt !== undefined && json.time !== undefined) {
|
---|
186 | time = Date.now() - json.builtAt + json.time;
|
---|
187 | }
|
---|
188 | if (unchangedChunkNumber > 0) {
|
---|
189 | return ('\n' +
|
---|
190 | rs(core_1.tags.stripIndents `
|
---|
191 | ${statsTable}
|
---|
192 |
|
---|
193 | ${unchangedChunkNumber} unchanged chunks
|
---|
194 |
|
---|
195 | ${generateBuildStats(json.hash || '', time, colors)}
|
---|
196 | `));
|
---|
197 | }
|
---|
198 | else {
|
---|
199 | return ('\n' +
|
---|
200 | rs(core_1.tags.stripIndents `
|
---|
201 | ${statsTable}
|
---|
202 |
|
---|
203 | ${generateBuildStats(json.hash || '', time, colors)}
|
---|
204 | `));
|
---|
205 | }
|
---|
206 | }
|
---|
207 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
208 | function statsWarningsToString(json, statsConfig) {
|
---|
209 | const colors = statsConfig.colors;
|
---|
210 | const c = (x) => (colors ? color_1.colors.reset.cyan(x) : x);
|
---|
211 | const y = (x) => (colors ? color_1.colors.reset.yellow(x) : x);
|
---|
212 | const yb = (x) => (colors ? color_1.colors.reset.yellowBright(x) : x);
|
---|
213 | const warnings = json.warnings ? [...json.warnings] : [];
|
---|
214 | if (json.children) {
|
---|
215 | warnings.push(...json.children.map((c) => { var _a; return (_a = c.warnings) !== null && _a !== void 0 ? _a : []; }).reduce((a, b) => [...a, ...b], []));
|
---|
216 | }
|
---|
217 | let output = '';
|
---|
218 | for (const warning of warnings) {
|
---|
219 | if (typeof warning === 'string') {
|
---|
220 | output += yb(`Warning: ${warning}\n\n`);
|
---|
221 | }
|
---|
222 | else {
|
---|
223 | const file = warning.file || warning.moduleName;
|
---|
224 | if (file) {
|
---|
225 | output += c(file);
|
---|
226 | if (warning.loc) {
|
---|
227 | output += ':' + yb(warning.loc);
|
---|
228 | }
|
---|
229 | output += ' - ';
|
---|
230 | }
|
---|
231 | if (!/^warning/i.test(warning.message)) {
|
---|
232 | output += y('Warning: ');
|
---|
233 | }
|
---|
234 | output += `${warning.message}\n\n`;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | return output ? '\n' + output : output;
|
---|
238 | }
|
---|
239 | exports.statsWarningsToString = statsWarningsToString;
|
---|
240 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
241 | function statsErrorsToString(json, statsConfig) {
|
---|
242 | const colors = statsConfig.colors;
|
---|
243 | const c = (x) => (colors ? color_1.colors.reset.cyan(x) : x);
|
---|
244 | const yb = (x) => (colors ? color_1.colors.reset.yellowBright(x) : x);
|
---|
245 | const r = (x) => (colors ? color_1.colors.reset.redBright(x) : x);
|
---|
246 | const errors = json.errors ? [...json.errors] : [];
|
---|
247 | if (json.children) {
|
---|
248 | errors.push(...json.children.map((c) => (c === null || c === void 0 ? void 0 : c.errors) || []).reduce((a, b) => [...a, ...b], []));
|
---|
249 | }
|
---|
250 | let output = '';
|
---|
251 | for (const error of errors) {
|
---|
252 | if (typeof error === 'string') {
|
---|
253 | output += r(`Error: ${error}\n\n`);
|
---|
254 | }
|
---|
255 | else {
|
---|
256 | const file = error.file || error.moduleName;
|
---|
257 | if (file) {
|
---|
258 | output += c(file);
|
---|
259 | if (error.loc) {
|
---|
260 | output += ':' + yb(error.loc);
|
---|
261 | }
|
---|
262 | output += ' - ';
|
---|
263 | }
|
---|
264 | if (!/^error/i.test(error.message)) {
|
---|
265 | output += r('Error: ');
|
---|
266 | }
|
---|
267 | output += `${error.message}\n\n`;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | return output ? '\n' + output : output;
|
---|
271 | }
|
---|
272 | exports.statsErrorsToString = statsErrorsToString;
|
---|
273 | function statsHasErrors(json) {
|
---|
274 | var _a, _b;
|
---|
275 | return !!(((_a = json.errors) === null || _a === void 0 ? void 0 : _a.length) || ((_b = json.children) === null || _b === void 0 ? void 0 : _b.some((c) => { var _a; return (_a = c.errors) === null || _a === void 0 ? void 0 : _a.length; })));
|
---|
276 | }
|
---|
277 | exports.statsHasErrors = statsHasErrors;
|
---|
278 | function statsHasWarnings(json) {
|
---|
279 | var _a, _b;
|
---|
280 | return !!(((_a = json.warnings) === null || _a === void 0 ? void 0 : _a.length) || ((_b = json.children) === null || _b === void 0 ? void 0 : _b.some((c) => { var _a; return (_a = c.warnings) === null || _a === void 0 ? void 0 : _a.length; })));
|
---|
281 | }
|
---|
282 | exports.statsHasWarnings = statsHasWarnings;
|
---|
283 | function createWebpackLoggingCallback(options, logger) {
|
---|
284 | const { verbose = false, scripts = [], styles = [] } = options;
|
---|
285 | const extraEntryPoints = [
|
---|
286 | ...helpers_1.normalizeExtraEntryPoints(styles, 'styles'),
|
---|
287 | ...helpers_1.normalizeExtraEntryPoints(scripts, 'scripts'),
|
---|
288 | ];
|
---|
289 | return (stats, config) => {
|
---|
290 | if (verbose) {
|
---|
291 | logger.info(stats.toString(config.stats));
|
---|
292 | }
|
---|
293 | const rawStats = stats.toJson(stats_1.getWebpackStatsConfig(false));
|
---|
294 | const webpackStats = {
|
---|
295 | ...rawStats,
|
---|
296 | chunks: async_chunks_1.markAsyncChunksNonInitial(rawStats, extraEntryPoints),
|
---|
297 | };
|
---|
298 | webpackStatsLogger(logger, webpackStats, config);
|
---|
299 | };
|
---|
300 | }
|
---|
301 | exports.createWebpackLoggingCallback = createWebpackLoggingCallback;
|
---|
302 | function webpackStatsLogger(logger, json, config, bundleStats) {
|
---|
303 | logger.info(statsToString(json, config.stats, bundleStats));
|
---|
304 | if (statsHasWarnings(json)) {
|
---|
305 | logger.warn(statsWarningsToString(json, config.stats));
|
---|
306 | }
|
---|
307 | if (statsHasErrors(json)) {
|
---|
308 | logger.error(statsErrorsToString(json, config.stats));
|
---|
309 | }
|
---|
310 | }
|
---|
311 | exports.webpackStatsLogger = webpackStatsLogger;
|
---|