UNPKG

39.1 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5"use strict";
6
7const RequestShortener = require("./RequestShortener");
8const SizeFormatHelpers = require("./SizeFormatHelpers");
9const formatLocation = require("./formatLocation");
10const identifierUtils = require("./util/identifier");
11const compareLocations = require("./compareLocations");
12
13const optionsOrFallback = (...args) => {
14 let optionValues = [];
15 optionValues.push(...args);
16 return optionValues.find(optionValue => optionValue !== undefined);
17};
18
19const compareId = (a, b) => {
20 if (typeof a !== typeof b) {
21 return typeof a < typeof b ? -1 : 1;
22 }
23 if (a < b) return -1;
24 if (a > b) return 1;
25 return 0;
26};
27
28class Stats {
29 constructor(compilation) {
30 this.compilation = compilation;
31 this.hash = compilation.hash;
32 this.startTime = undefined;
33 this.endTime = undefined;
34 }
35
36 static filterWarnings(warnings, warningsFilter) {
37 // we dont have anything to filter so all warnings can be shown
38 if (!warningsFilter) {
39 return warnings;
40 }
41
42 // create a chain of filters
43 // if they return "true" a warning should be suppressed
44 const normalizedWarningsFilters = [].concat(warningsFilter).map(filter => {
45 if (typeof filter === "string") {
46 return warning => warning.includes(filter);
47 }
48
49 if (filter instanceof RegExp) {
50 return warning => filter.test(warning);
51 }
52
53 if (typeof filter === "function") {
54 return filter;
55 }
56
57 throw new Error(
58 `Can only filter warnings with Strings or RegExps. (Given: ${filter})`
59 );
60 });
61 return warnings.filter(warning => {
62 return !normalizedWarningsFilters.some(check => check(warning));
63 });
64 }
65
66 formatFilePath(filePath) {
67 const OPTIONS_REGEXP = /^(\s|\S)*!/;
68 return filePath.includes("!")
69 ? `${filePath.replace(OPTIONS_REGEXP, "")} (${filePath})`
70 : `${filePath}`;
71 }
72
73 hasWarnings() {
74 return (
75 this.compilation.warnings.length > 0 ||
76 this.compilation.children.some(child => child.getStats().hasWarnings())
77 );
78 }
79
80 hasErrors() {
81 return (
82 this.compilation.errors.length > 0 ||
83 this.compilation.children.some(child => child.getStats().hasErrors())
84 );
85 }
86
87 // remove a prefixed "!" that can be specified to reverse sort order
88 normalizeFieldKey(field) {
89 if (field[0] === "!") {
90 return field.substr(1);
91 }
92 return field;
93 }
94
95 // if a field is prefixed by a "!" reverse sort order
96 sortOrderRegular(field) {
97 if (field[0] === "!") {
98 return false;
99 }
100 return true;
101 }
102
103 toJson(options, forToString) {
104 if (typeof options === "boolean" || typeof options === "string") {
105 options = Stats.presetToOptions(options);
106 } else if (!options) {
107 options = {};
108 }
109
110 const optionOrLocalFallback = (v, def) =>
111 v !== undefined ? v : options.all !== undefined ? options.all : def;
112
113 const testAgainstGivenOption = item => {
114 if (typeof item === "string") {
115 const regExp = new RegExp(
116 `[\\\\/]${item.replace(
117 // eslint-disable-next-line no-useless-escape
118 /[-[\]{}()*+?.\\^$|]/g,
119 "\\$&"
120 )}([\\\\/]|$|!|\\?)`
121 );
122 return ident => regExp.test(ident);
123 }
124 if (item && typeof item === "object" && typeof item.test === "function") {
125 return ident => item.test(ident);
126 }
127 if (typeof item === "function") {
128 return item;
129 }
130 if (typeof item === "boolean") {
131 return () => item;
132 }
133 };
134
135 const compilation = this.compilation;
136 const context = optionsOrFallback(
137 options.context,
138 compilation.compiler.context
139 );
140 const requestShortener =
141 compilation.compiler.context === context
142 ? compilation.requestShortener
143 : new RequestShortener(context);
144 const showPerformance = optionOrLocalFallback(options.performance, true);
145 const showHash = optionOrLocalFallback(options.hash, true);
146 const showEnv = optionOrLocalFallback(options.env, false);
147 const showVersion = optionOrLocalFallback(options.version, true);
148 const showTimings = optionOrLocalFallback(options.timings, true);
149 const showBuiltAt = optionOrLocalFallback(options.builtAt, true);
150 const showAssets = optionOrLocalFallback(options.assets, true);
151 const showEntrypoints = optionOrLocalFallback(options.entrypoints, true);
152 const showChunkGroups = optionOrLocalFallback(
153 options.chunkGroups,
154 !forToString
155 );
156 const showChunks = optionOrLocalFallback(options.chunks, !forToString);
157 const showChunkModules = optionOrLocalFallback(options.chunkModules, true);
158 const showChunkOrigins = optionOrLocalFallback(
159 options.chunkOrigins,
160 !forToString
161 );
162 const showModules = optionOrLocalFallback(options.modules, true);
163 const showNestedModules = optionOrLocalFallback(
164 options.nestedModules,
165 true
166 );
167 const showModuleAssets = optionOrLocalFallback(
168 options.moduleAssets,
169 !forToString
170 );
171 const showDepth = optionOrLocalFallback(options.depth, !forToString);
172 const showCachedModules = optionOrLocalFallback(options.cached, true);
173 const showCachedAssets = optionOrLocalFallback(options.cachedAssets, true);
174 const showReasons = optionOrLocalFallback(options.reasons, !forToString);
175 const showUsedExports = optionOrLocalFallback(
176 options.usedExports,
177 !forToString
178 );
179 const showProvidedExports = optionOrLocalFallback(
180 options.providedExports,
181 !forToString
182 );
183 const showOptimizationBailout = optionOrLocalFallback(
184 options.optimizationBailout,
185 !forToString
186 );
187 const showChildren = optionOrLocalFallback(options.children, true);
188 const showSource = optionOrLocalFallback(options.source, !forToString);
189 const showModuleTrace = optionOrLocalFallback(options.moduleTrace, true);
190 const showErrors = optionOrLocalFallback(options.errors, true);
191 const showErrorDetails = optionOrLocalFallback(
192 options.errorDetails,
193 !forToString
194 );
195 const showWarnings = optionOrLocalFallback(options.warnings, true);
196 const warningsFilter = optionsOrFallback(options.warningsFilter, null);
197 const showPublicPath = optionOrLocalFallback(
198 options.publicPath,
199 !forToString
200 );
201 const excludeModules = []
202 .concat(optionsOrFallback(options.excludeModules, options.exclude, []))
203 .map(testAgainstGivenOption);
204 const excludeAssets = []
205 .concat(optionsOrFallback(options.excludeAssets, []))
206 .map(testAgainstGivenOption);
207 const maxModules = optionsOrFallback(
208 options.maxModules,
209 forToString ? 15 : Infinity
210 );
211 const sortModules = optionsOrFallback(options.modulesSort, "id");
212 const sortChunks = optionsOrFallback(options.chunksSort, "id");
213 const sortAssets = optionsOrFallback(options.assetsSort, "");
214 const showOutputPath = optionOrLocalFallback(
215 options.outputPath,
216 !forToString
217 );
218
219 if (!showCachedModules) {
220 excludeModules.push((ident, module) => !module.built);
221 }
222
223 const createModuleFilter = () => {
224 let i = 0;
225 return module => {
226 if (excludeModules.length > 0) {
227 const ident = requestShortener.shorten(module.resource);
228 const excluded = excludeModules.some(fn => fn(ident, module));
229 if (excluded) return false;
230 }
231 const result = i < maxModules;
232 i++;
233 return result;
234 };
235 };
236
237 const createAssetFilter = () => {
238 return asset => {
239 if (excludeAssets.length > 0) {
240 const ident = asset.name;
241 const excluded = excludeAssets.some(fn => fn(ident, asset));
242 if (excluded) return false;
243 }
244 return showCachedAssets || asset.emitted;
245 };
246 };
247
248 const sortByFieldAndOrder = (fieldKey, a, b) => {
249 if (a[fieldKey] === null && b[fieldKey] === null) return 0;
250 if (a[fieldKey] === null) return 1;
251 if (b[fieldKey] === null) return -1;
252 if (a[fieldKey] === b[fieldKey]) return 0;
253 if (typeof a[fieldKey] !== typeof b[fieldKey])
254 return typeof a[fieldKey] < typeof b[fieldKey] ? -1 : 1;
255 return a[fieldKey] < b[fieldKey] ? -1 : 1;
256 };
257
258 const sortByField = (field, originalArray) => {
259 const originalMap = originalArray.reduce((map, v, i) => {
260 map.set(v, i);
261 return map;
262 }, new Map());
263 return (a, b) => {
264 if (field) {
265 const fieldKey = this.normalizeFieldKey(field);
266
267 // if a field is prefixed with a "!" the sort is reversed!
268 const sortIsRegular = this.sortOrderRegular(field);
269
270 const cmp = sortByFieldAndOrder(
271 fieldKey,
272 sortIsRegular ? a : b,
273 sortIsRegular ? b : a
274 );
275 if (cmp) return cmp;
276 }
277 return originalMap.get(a) - originalMap.get(b);
278 };
279 };
280
281 const formatError = e => {
282 let text = "";
283 if (typeof e === "string") {
284 e = { message: e };
285 }
286 if (e.chunk) {
287 text += `chunk ${e.chunk.name || e.chunk.id}${
288 e.chunk.hasRuntime()
289 ? " [entry]"
290 : e.chunk.canBeInitial()
291 ? " [initial]"
292 : ""
293 }\n`;
294 }
295 if (e.file) {
296 text += `${e.file}\n`;
297 }
298 if (
299 e.module &&
300 e.module.readableIdentifier &&
301 typeof e.module.readableIdentifier === "function"
302 ) {
303 text += this.formatFilePath(
304 e.module.readableIdentifier(requestShortener)
305 );
306 if (typeof e.loc === "object") {
307 const locInfo = formatLocation(e.loc);
308 if (locInfo) text += ` ${locInfo}`;
309 }
310 text += "\n";
311 }
312 text += e.message;
313 if (showErrorDetails && e.details) {
314 text += `\n${e.details}`;
315 }
316 if (showErrorDetails && e.missing) {
317 text += e.missing.map(item => `\n[${item}]`).join("");
318 }
319 if (showModuleTrace && e.origin) {
320 text += `\n @ ${this.formatFilePath(
321 e.origin.readableIdentifier(requestShortener)
322 )}`;
323 if (typeof e.originLoc === "object") {
324 const locInfo = formatLocation(e.originLoc);
325 if (locInfo) text += ` ${locInfo}`;
326 }
327 if (e.dependencies) {
328 for (const dep of e.dependencies) {
329 if (!dep.loc) continue;
330 if (typeof dep.loc === "string") continue;
331 const locInfo = formatLocation(dep.loc);
332 if (!locInfo) continue;
333 text += ` ${locInfo}`;
334 }
335 }
336 let current = e.origin;
337 while (current.issuer) {
338 current = current.issuer;
339 text += `\n @ ${current.readableIdentifier(requestShortener)}`;
340 }
341 }
342 return text;
343 };
344
345 const obj = {
346 errors: compilation.errors.map(formatError),
347 warnings: Stats.filterWarnings(
348 compilation.warnings.map(formatError),
349 warningsFilter
350 )
351 };
352
353 //We just hint other renderers since actually omitting
354 //errors/warnings from the JSON would be kind of weird.
355 Object.defineProperty(obj, "_showWarnings", {
356 value: showWarnings,
357 enumerable: false
358 });
359 Object.defineProperty(obj, "_showErrors", {
360 value: showErrors,
361 enumerable: false
362 });
363
364 if (showVersion) {
365 obj.version = require("../package.json").version;
366 }
367
368 if (showHash) obj.hash = this.hash;
369 if (showTimings && this.startTime && this.endTime) {
370 obj.time = this.endTime - this.startTime;
371 }
372
373 if (showBuiltAt && this.endTime) {
374 obj.builtAt = this.endTime;
375 }
376
377 if (showEnv && options._env) {
378 obj.env = options._env;
379 }
380
381 if (compilation.needAdditionalPass) {
382 obj.needAdditionalPass = true;
383 }
384 if (showPublicPath) {
385 obj.publicPath = this.compilation.mainTemplate.getPublicPath({
386 hash: this.compilation.hash
387 });
388 }
389 if (showOutputPath) {
390 obj.outputPath = this.compilation.mainTemplate.outputOptions.path;
391 }
392 if (showAssets) {
393 const assetsByFile = {};
394 const compilationAssets = Object.keys(compilation.assets).sort();
395 obj.assetsByChunkName = {};
396 obj.assets = compilationAssets
397 .map(asset => {
398 const obj = {
399 name: asset,
400 size: compilation.assets[asset].size(),
401 chunks: [],
402 chunkNames: [],
403 emitted: compilation.assets[asset].emitted
404 };
405
406 if (showPerformance) {
407 obj.isOverSizeLimit = compilation.assets[asset].isOverSizeLimit;
408 }
409
410 assetsByFile[asset] = obj;
411 return obj;
412 })
413 .filter(createAssetFilter());
414 obj.filteredAssets = compilationAssets.length - obj.assets.length;
415
416 for (const chunk of compilation.chunks) {
417 for (const asset of chunk.files) {
418 if (assetsByFile[asset]) {
419 for (const id of chunk.ids) {
420 assetsByFile[asset].chunks.push(id);
421 }
422 if (chunk.name) {
423 assetsByFile[asset].chunkNames.push(chunk.name);
424 if (obj.assetsByChunkName[chunk.name]) {
425 obj.assetsByChunkName[chunk.name] = []
426 .concat(obj.assetsByChunkName[chunk.name])
427 .concat([asset]);
428 } else {
429 obj.assetsByChunkName[chunk.name] = asset;
430 }
431 }
432 }
433 }
434 }
435 obj.assets.sort(sortByField(sortAssets, obj.assets));
436 }
437
438 const fnChunkGroup = groupMap => {
439 const obj = {};
440 for (const keyValuePair of groupMap) {
441 const name = keyValuePair[0];
442 const cg = keyValuePair[1];
443 const children = cg.getChildrenByOrders();
444 obj[name] = {
445 chunks: cg.chunks.map(c => c.id),
446 assets: cg.chunks.reduce(
447 (array, c) => array.concat(c.files || []),
448 []
449 ),
450 children: Object.keys(children).reduce((obj, key) => {
451 const groups = children[key];
452 obj[key] = groups.map(group => ({
453 name: group.name,
454 chunks: group.chunks.map(c => c.id),
455 assets: group.chunks.reduce(
456 (array, c) => array.concat(c.files || []),
457 []
458 )
459 }));
460 return obj;
461 }, Object.create(null)),
462 childAssets: Object.keys(children).reduce((obj, key) => {
463 const groups = children[key];
464 obj[key] = Array.from(
465 groups.reduce((set, group) => {
466 for (const chunk of group.chunks) {
467 for (const asset of chunk.files) {
468 set.add(asset);
469 }
470 }
471 return set;
472 }, new Set())
473 );
474 return obj;
475 }, Object.create(null))
476 };
477 if (showPerformance) {
478 obj[name].isOverSizeLimit = cg.isOverSizeLimit;
479 }
480 }
481
482 return obj;
483 };
484
485 if (showEntrypoints) {
486 obj.entrypoints = fnChunkGroup(compilation.entrypoints);
487 }
488
489 if (showChunkGroups) {
490 obj.namedChunkGroups = fnChunkGroup(compilation.namedChunkGroups);
491 }
492
493 const fnModule = module => {
494 const path = [];
495 let current = module;
496 while (current.issuer) {
497 path.push((current = current.issuer));
498 }
499 path.reverse();
500 const obj = {
501 id: module.id,
502 identifier: module.identifier(),
503 name: module.readableIdentifier(requestShortener),
504 index: module.index,
505 index2: module.index2,
506 size: module.size(),
507 cacheable: module.buildInfo.cacheable,
508 built: !!module.built,
509 optional: module.optional,
510 prefetched: module.prefetched,
511 chunks: Array.from(module.chunksIterable, chunk => chunk.id),
512 issuer: module.issuer && module.issuer.identifier(),
513 issuerId: module.issuer && module.issuer.id,
514 issuerName:
515 module.issuer && module.issuer.readableIdentifier(requestShortener),
516 issuerPath:
517 module.issuer &&
518 path.map(module => ({
519 id: module.id,
520 identifier: module.identifier(),
521 name: module.readableIdentifier(requestShortener),
522 profile: module.profile
523 })),
524 profile: module.profile,
525 failed: !!module.error,
526 errors: module.errors ? module.errors.length : 0,
527 warnings: module.warnings ? module.warnings.length : 0
528 };
529 if (showModuleAssets) {
530 obj.assets = Object.keys(module.buildInfo.assets || {});
531 }
532 if (showReasons) {
533 obj.reasons = module.reasons
534 .sort((a, b) => {
535 if (a.module && !b.module) return -1;
536 if (!a.module && b.module) return 1;
537 if (a.module && b.module) {
538 const cmp = compareId(a.module.id, b.module.id);
539 if (cmp) return cmp;
540 }
541 if (a.dependency && !b.dependency) return -1;
542 if (!a.dependency && b.dependency) return 1;
543 if (a.dependency && b.dependency) {
544 const cmp = compareLocations(a.dependency.loc, b.dependency.loc);
545 if (cmp) return cmp;
546 if (a.dependency.type < b.dependency.type) return -1;
547 if (a.dependency.type > b.dependency.type) return 1;
548 }
549 return 0;
550 })
551 .map(reason => {
552 const obj = {
553 moduleId: reason.module ? reason.module.id : null,
554 moduleIdentifier: reason.module
555 ? reason.module.identifier()
556 : null,
557 module: reason.module
558 ? reason.module.readableIdentifier(requestShortener)
559 : null,
560 moduleName: reason.module
561 ? reason.module.readableIdentifier(requestShortener)
562 : null,
563 type: reason.dependency ? reason.dependency.type : null,
564 explanation: reason.explanation,
565 userRequest: reason.dependency
566 ? reason.dependency.userRequest
567 : null
568 };
569 if (reason.dependency) {
570 const locInfo = formatLocation(reason.dependency.loc);
571 if (locInfo) {
572 obj.loc = locInfo;
573 }
574 }
575 return obj;
576 });
577 }
578 if (showUsedExports) {
579 if (module.used === true) {
580 obj.usedExports = module.usedExports;
581 } else if (module.used === false) {
582 obj.usedExports = false;
583 }
584 }
585 if (showProvidedExports) {
586 obj.providedExports = Array.isArray(module.buildMeta.providedExports)
587 ? module.buildMeta.providedExports
588 : null;
589 }
590 if (showOptimizationBailout) {
591 obj.optimizationBailout = module.optimizationBailout.map(item => {
592 if (typeof item === "function") return item(requestShortener);
593 return item;
594 });
595 }
596 if (showDepth) {
597 obj.depth = module.depth;
598 }
599 if (showNestedModules) {
600 if (module.modules) {
601 const modules = module.modules;
602 obj.modules = modules
603 .sort(sortByField("depth", modules))
604 .filter(createModuleFilter())
605 .map(fnModule);
606 obj.filteredModules = modules.length - obj.modules.length;
607 obj.modules.sort(sortByField(sortModules, obj.modules));
608 }
609 }
610 if (showSource && module._source) {
611 obj.source = module._source.source();
612 }
613 return obj;
614 };
615 if (showChunks) {
616 obj.chunks = compilation.chunks.map(chunk => {
617 const parents = new Set();
618 const children = new Set();
619 const siblings = new Set();
620 const childIdByOrder = chunk.getChildIdsByOrders();
621 for (const chunkGroup of chunk.groupsIterable) {
622 for (const parentGroup of chunkGroup.parentsIterable) {
623 for (const chunk of parentGroup.chunks) {
624 parents.add(chunk.id);
625 }
626 }
627 for (const childGroup of chunkGroup.childrenIterable) {
628 for (const chunk of childGroup.chunks) {
629 children.add(chunk.id);
630 }
631 }
632 for (const sibling of chunkGroup.chunks) {
633 if (sibling !== chunk) siblings.add(sibling.id);
634 }
635 }
636 const obj = {
637 id: chunk.id,
638 rendered: chunk.rendered,
639 initial: chunk.canBeInitial(),
640 entry: chunk.hasRuntime(),
641 recorded: chunk.recorded,
642 reason: chunk.chunkReason,
643 size: chunk.modulesSize(),
644 names: chunk.name ? [chunk.name] : [],
645 files: chunk.files.slice(),
646 hash: chunk.renderedHash,
647 siblings: Array.from(siblings).sort(compareId),
648 parents: Array.from(parents).sort(compareId),
649 children: Array.from(children).sort(compareId),
650 childrenByOrder: childIdByOrder
651 };
652 if (showChunkModules) {
653 const modules = chunk.getModules();
654 obj.modules = modules
655 .slice()
656 .sort(sortByField("depth", modules))
657 .filter(createModuleFilter())
658 .map(fnModule);
659 obj.filteredModules = chunk.getNumberOfModules() - obj.modules.length;
660 obj.modules.sort(sortByField(sortModules, obj.modules));
661 }
662 if (showChunkOrigins) {
663 obj.origins = Array.from(chunk.groupsIterable, g => g.origins)
664 .reduce((a, b) => a.concat(b), [])
665 .map(origin => ({
666 moduleId: origin.module ? origin.module.id : undefined,
667 module: origin.module ? origin.module.identifier() : "",
668 moduleIdentifier: origin.module ? origin.module.identifier() : "",
669 moduleName: origin.module
670 ? origin.module.readableIdentifier(requestShortener)
671 : "",
672 loc: formatLocation(origin.loc),
673 request: origin.request,
674 reasons: origin.reasons || []
675 }))
676 .sort((a, b) => {
677 const cmp1 = compareId(a.moduleId, b.moduleId);
678 if (cmp1) return cmp1;
679 const cmp2 = compareId(a.loc, b.loc);
680 if (cmp2) return cmp2;
681 const cmp3 = compareId(a.request, b.request);
682 if (cmp3) return cmp3;
683 return 0;
684 });
685 }
686 return obj;
687 });
688 obj.chunks.sort(sortByField(sortChunks, obj.chunks));
689 }
690 if (showModules) {
691 obj.modules = compilation.modules
692 .slice()
693 .sort(sortByField("depth", compilation.modules))
694 .filter(createModuleFilter())
695 .map(fnModule);
696 obj.filteredModules = compilation.modules.length - obj.modules.length;
697 obj.modules.sort(sortByField(sortModules, obj.modules));
698 }
699 if (showChildren) {
700 obj.children = compilation.children.map((child, idx) => {
701 const childOptions = Stats.getChildOptions(options, idx);
702 const obj = new Stats(child).toJson(childOptions, forToString);
703 delete obj.hash;
704 delete obj.version;
705 if (child.name) {
706 obj.name = identifierUtils.makePathsRelative(
707 context,
708 child.name,
709 compilation.cache
710 );
711 }
712 return obj;
713 });
714 }
715
716 return obj;
717 }
718
719 toString(options) {
720 if (typeof options === "boolean" || typeof options === "string") {
721 options = Stats.presetToOptions(options);
722 } else if (!options) {
723 options = {};
724 }
725
726 const useColors = optionsOrFallback(options.colors, false);
727
728 const obj = this.toJson(options, true);
729
730 return Stats.jsonToString(obj, useColors);
731 }
732
733 static jsonToString(obj, useColors) {
734 const buf = [];
735
736 const defaultColors = {
737 bold: "\u001b[1m",
738 yellow: "\u001b[1m\u001b[33m",
739 red: "\u001b[1m\u001b[31m",
740 green: "\u001b[1m\u001b[32m",
741 cyan: "\u001b[1m\u001b[36m",
742 magenta: "\u001b[1m\u001b[35m"
743 };
744
745 const colors = Object.keys(defaultColors).reduce(
746 (obj, color) => {
747 obj[color] = str => {
748 if (useColors) {
749 buf.push(
750 useColors === true || useColors[color] === undefined
751 ? defaultColors[color]
752 : useColors[color]
753 );
754 }
755 buf.push(str);
756 if (useColors) {
757 buf.push("\u001b[39m\u001b[22m");
758 }
759 };
760 return obj;
761 },
762 {
763 normal: str => buf.push(str)
764 }
765 );
766
767 const coloredTime = time => {
768 let times = [800, 400, 200, 100];
769 if (obj.time) {
770 times = [obj.time / 2, obj.time / 4, obj.time / 8, obj.time / 16];
771 }
772 if (time < times[3]) colors.normal(`${time}ms`);
773 else if (time < times[2]) colors.bold(`${time}ms`);
774 else if (time < times[1]) colors.green(`${time}ms`);
775 else if (time < times[0]) colors.yellow(`${time}ms`);
776 else colors.red(`${time}ms`);
777 };
778
779 const newline = () => buf.push("\n");
780
781 const getText = (arr, row, col) => {
782 return arr[row][col].value;
783 };
784
785 const table = (array, align, splitter) => {
786 const rows = array.length;
787 const cols = array[0].length;
788 const colSizes = new Array(cols);
789 for (let col = 0; col < cols; col++) {
790 colSizes[col] = 0;
791 }
792 for (let row = 0; row < rows; row++) {
793 for (let col = 0; col < cols; col++) {
794 const value = `${getText(array, row, col)}`;
795 if (value.length > colSizes[col]) {
796 colSizes[col] = value.length;
797 }
798 }
799 }
800 for (let row = 0; row < rows; row++) {
801 for (let col = 0; col < cols; col++) {
802 const format = array[row][col].color;
803 const value = `${getText(array, row, col)}`;
804 let l = value.length;
805 if (align[col] === "l") {
806 format(value);
807 }
808 for (; l < colSizes[col] && col !== cols - 1; l++) {
809 colors.normal(" ");
810 }
811 if (align[col] === "r") {
812 format(value);
813 }
814 if (col + 1 < cols && colSizes[col] !== 0) {
815 colors.normal(splitter || " ");
816 }
817 }
818 newline();
819 }
820 };
821
822 const getAssetColor = (asset, defaultColor) => {
823 if (asset.isOverSizeLimit) {
824 return colors.yellow;
825 }
826
827 return defaultColor;
828 };
829
830 if (obj.hash) {
831 colors.normal("Hash: ");
832 colors.bold(obj.hash);
833 newline();
834 }
835 if (obj.version) {
836 colors.normal("Version: webpack ");
837 colors.bold(obj.version);
838 newline();
839 }
840 if (typeof obj.time === "number") {
841 colors.normal("Time: ");
842 colors.bold(obj.time);
843 colors.normal("ms");
844 newline();
845 }
846 if (typeof obj.builtAt === "number") {
847 const builtAtDate = new Date(obj.builtAt);
848 colors.normal("Built at: ");
849 colors.normal(
850 builtAtDate.toLocaleDateString(undefined, {
851 day: "2-digit",
852 month: "2-digit",
853 year: "numeric"
854 })
855 );
856 colors.normal(" ");
857 colors.bold(builtAtDate.toLocaleTimeString());
858 newline();
859 }
860 if (obj.env) {
861 colors.normal("Environment (--env): ");
862 colors.bold(JSON.stringify(obj.env, null, 2));
863 newline();
864 }
865 if (obj.publicPath) {
866 colors.normal("PublicPath: ");
867 colors.bold(obj.publicPath);
868 newline();
869 }
870
871 if (obj.assets && obj.assets.length > 0) {
872 const t = [
873 [
874 {
875 value: "Asset",
876 color: colors.bold
877 },
878 {
879 value: "Size",
880 color: colors.bold
881 },
882 {
883 value: "Chunks",
884 color: colors.bold
885 },
886 {
887 value: "",
888 color: colors.bold
889 },
890 {
891 value: "",
892 color: colors.bold
893 },
894 {
895 value: "Chunk Names",
896 color: colors.bold
897 }
898 ]
899 ];
900 for (const asset of obj.assets) {
901 t.push([
902 {
903 value: asset.name,
904 color: getAssetColor(asset, colors.green)
905 },
906 {
907 value: SizeFormatHelpers.formatSize(asset.size),
908 color: getAssetColor(asset, colors.normal)
909 },
910 {
911 value: asset.chunks.join(", "),
912 color: colors.bold
913 },
914 {
915 value: asset.emitted ? "[emitted]" : "",
916 color: colors.green
917 },
918 {
919 value: asset.isOverSizeLimit ? "[big]" : "",
920 color: getAssetColor(asset, colors.normal)
921 },
922 {
923 value: asset.chunkNames.join(", "),
924 color: colors.normal
925 }
926 ]);
927 }
928 table(t, "rrrlll");
929 }
930 if (obj.filteredAssets > 0) {
931 colors.normal(" ");
932 if (obj.assets.length > 0) colors.normal("+ ");
933 colors.normal(obj.filteredAssets);
934 if (obj.assets.length > 0) colors.normal(" hidden");
935 colors.normal(obj.filteredAssets !== 1 ? " assets" : " asset");
936 newline();
937 }
938
939 const processChunkGroups = (namedGroups, prefix) => {
940 for (const name of Object.keys(namedGroups)) {
941 const cg = namedGroups[name];
942 colors.normal(`${prefix} `);
943 colors.bold(name);
944 if (cg.isOverSizeLimit) {
945 colors.normal(" ");
946 colors.yellow("[big]");
947 }
948 colors.normal(" =");
949 for (const asset of cg.assets) {
950 colors.normal(" ");
951 colors.green(asset);
952 }
953 for (const name of Object.keys(cg.childAssets)) {
954 const assets = cg.childAssets[name];
955 if (assets && assets.length > 0) {
956 colors.normal(" ");
957 colors.magenta(`(${name}:`);
958 for (const asset of assets) {
959 colors.normal(" ");
960 colors.green(asset);
961 }
962 colors.magenta(")");
963 }
964 }
965 newline();
966 }
967 };
968
969 if (obj.entrypoints) {
970 processChunkGroups(obj.entrypoints, "Entrypoint");
971 }
972
973 if (obj.namedChunkGroups) {
974 let outputChunkGroups = obj.namedChunkGroups;
975 if (obj.entrypoints) {
976 outputChunkGroups = Object.keys(outputChunkGroups)
977 .filter(name => !obj.entrypoints[name])
978 .reduce((result, name) => {
979 result[name] = obj.namedChunkGroups[name];
980 return result;
981 }, {});
982 }
983 processChunkGroups(outputChunkGroups, "Chunk Group");
984 }
985
986 const modulesByIdentifier = {};
987 if (obj.modules) {
988 for (const module of obj.modules) {
989 modulesByIdentifier[`$${module.identifier}`] = module;
990 }
991 } else if (obj.chunks) {
992 for (const chunk of obj.chunks) {
993 if (chunk.modules) {
994 for (const module of chunk.modules) {
995 modulesByIdentifier[`$${module.identifier}`] = module;
996 }
997 }
998 }
999 }
1000
1001 const processModuleAttributes = module => {
1002 colors.normal(" ");
1003 colors.normal(SizeFormatHelpers.formatSize(module.size));
1004 if (module.chunks) {
1005 for (const chunk of module.chunks) {
1006 colors.normal(" {");
1007 colors.yellow(chunk);
1008 colors.normal("}");
1009 }
1010 }
1011 if (typeof module.depth === "number") {
1012 colors.normal(` [depth ${module.depth}]`);
1013 }
1014 if (module.cacheable === false) {
1015 colors.red(" [not cacheable]");
1016 }
1017 if (module.optional) {
1018 colors.yellow(" [optional]");
1019 }
1020 if (module.built) {
1021 colors.green(" [built]");
1022 }
1023 if (module.assets && module.assets.length) {
1024 colors.magenta(
1025 ` [${module.assets.length} asset${
1026 module.assets.length === 1 ? "" : "s"
1027 }]`
1028 );
1029 }
1030 if (module.prefetched) {
1031 colors.magenta(" [prefetched]");
1032 }
1033 if (module.failed) colors.red(" [failed]");
1034 if (module.warnings) {
1035 colors.yellow(
1036 ` [${module.warnings} warning${module.warnings === 1 ? "" : "s"}]`
1037 );
1038 }
1039 if (module.errors) {
1040 colors.red(
1041 ` [${module.errors} error${module.errors === 1 ? "" : "s"}]`
1042 );
1043 }
1044 };
1045
1046 const processModuleContent = (module, prefix) => {
1047 if (Array.isArray(module.providedExports)) {
1048 colors.normal(prefix);
1049 if (module.providedExports.length === 0) {
1050 colors.cyan("[no exports]");
1051 } else {
1052 colors.cyan(`[exports: ${module.providedExports.join(", ")}]`);
1053 }
1054 newline();
1055 }
1056 if (module.usedExports !== undefined) {
1057 if (module.usedExports !== true) {
1058 colors.normal(prefix);
1059 if (module.usedExports === null) {
1060 colors.cyan("[used exports unknown]");
1061 } else if (module.usedExports === false) {
1062 colors.cyan("[no exports used]");
1063 } else if (
1064 Array.isArray(module.usedExports) &&
1065 module.usedExports.length === 0
1066 ) {
1067 colors.cyan("[no exports used]");
1068 } else if (Array.isArray(module.usedExports)) {
1069 const providedExportsCount = Array.isArray(module.providedExports)
1070 ? module.providedExports.length
1071 : null;
1072 if (
1073 providedExportsCount !== null &&
1074 providedExportsCount === module.usedExports.length
1075 ) {
1076 colors.cyan("[all exports used]");
1077 } else {
1078 colors.cyan(
1079 `[only some exports used: ${module.usedExports.join(", ")}]`
1080 );
1081 }
1082 }
1083 newline();
1084 }
1085 }
1086 if (Array.isArray(module.optimizationBailout)) {
1087 for (const item of module.optimizationBailout) {
1088 colors.normal(prefix);
1089 colors.yellow(item);
1090 newline();
1091 }
1092 }
1093 if (module.reasons) {
1094 for (const reason of module.reasons) {
1095 colors.normal(prefix);
1096 if (reason.type) {
1097 colors.normal(reason.type);
1098 colors.normal(" ");
1099 }
1100 if (reason.userRequest) {
1101 colors.cyan(reason.userRequest);
1102 colors.normal(" ");
1103 }
1104 if (reason.moduleId !== null) {
1105 colors.normal("[");
1106 colors.normal(reason.moduleId);
1107 colors.normal("]");
1108 }
1109 if (reason.module && reason.module !== reason.moduleId) {
1110 colors.normal(" ");
1111 colors.magenta(reason.module);
1112 }
1113 if (reason.loc) {
1114 colors.normal(" ");
1115 colors.normal(reason.loc);
1116 }
1117 if (reason.explanation) {
1118 colors.normal(" ");
1119 colors.cyan(reason.explanation);
1120 }
1121 newline();
1122 }
1123 }
1124 if (module.profile) {
1125 colors.normal(prefix);
1126 let sum = 0;
1127 if (module.issuerPath) {
1128 for (const m of module.issuerPath) {
1129 colors.normal("[");
1130 colors.normal(m.id);
1131 colors.normal("] ");
1132 if (m.profile) {
1133 const time = (m.profile.factory || 0) + (m.profile.building || 0);
1134 coloredTime(time);
1135 sum += time;
1136 colors.normal(" ");
1137 }
1138 colors.normal("-> ");
1139 }
1140 }
1141 for (const key of Object.keys(module.profile)) {
1142 colors.normal(`${key}:`);
1143 const time = module.profile[key];
1144 coloredTime(time);
1145 colors.normal(" ");
1146 sum += time;
1147 }
1148 colors.normal("= ");
1149 coloredTime(sum);
1150 newline();
1151 }
1152 if (module.modules) {
1153 processModulesList(module, prefix + "| ");
1154 }
1155 };
1156
1157 const processModulesList = (obj, prefix) => {
1158 if (obj.modules) {
1159 let maxModuleId = 0;
1160 for (const module of obj.modules) {
1161 if (typeof module.id === "number") {
1162 if (maxModuleId < module.id) maxModuleId = module.id;
1163 }
1164 }
1165 let contentPrefix = prefix + " ";
1166 if (maxModuleId >= 10) contentPrefix += " ";
1167 if (maxModuleId >= 100) contentPrefix += " ";
1168 if (maxModuleId >= 1000) contentPrefix += " ";
1169 for (const module of obj.modules) {
1170 colors.normal(prefix);
1171 const name = module.name || module.identifier;
1172 if (typeof module.id === "string" || typeof module.id === "number") {
1173 if (typeof module.id === "number") {
1174 if (module.id < 1000 && maxModuleId >= 1000) colors.normal(" ");
1175 if (module.id < 100 && maxModuleId >= 100) colors.normal(" ");
1176 if (module.id < 10 && maxModuleId >= 10) colors.normal(" ");
1177 } else {
1178 if (maxModuleId >= 1000) colors.normal(" ");
1179 if (maxModuleId >= 100) colors.normal(" ");
1180 if (maxModuleId >= 10) colors.normal(" ");
1181 }
1182 if (name !== module.id) {
1183 colors.normal("[");
1184 colors.normal(module.id);
1185 colors.normal("]");
1186 colors.normal(" ");
1187 } else {
1188 colors.normal("[");
1189 colors.bold(module.id);
1190 colors.normal("]");
1191 }
1192 }
1193 if (name !== module.id) {
1194 colors.bold(name);
1195 }
1196 processModuleAttributes(module);
1197 newline();
1198 processModuleContent(module, contentPrefix);
1199 }
1200 if (obj.filteredModules > 0) {
1201 colors.normal(prefix);
1202 colors.normal(" ");
1203 if (obj.modules.length > 0) colors.normal(" + ");
1204 colors.normal(obj.filteredModules);
1205 if (obj.modules.length > 0) colors.normal(" hidden");
1206 colors.normal(obj.filteredModules !== 1 ? " modules" : " module");
1207 newline();
1208 }
1209 }
1210 };
1211
1212 if (obj.chunks) {
1213 for (const chunk of obj.chunks) {
1214 colors.normal("chunk ");
1215 if (chunk.id < 1000) colors.normal(" ");
1216 if (chunk.id < 100) colors.normal(" ");
1217 if (chunk.id < 10) colors.normal(" ");
1218 colors.normal("{");
1219 colors.yellow(chunk.id);
1220 colors.normal("} ");
1221 colors.green(chunk.files.join(", "));
1222 if (chunk.names && chunk.names.length > 0) {
1223 colors.normal(" (");
1224 colors.normal(chunk.names.join(", "));
1225 colors.normal(")");
1226 }
1227 colors.normal(" ");
1228 colors.normal(SizeFormatHelpers.formatSize(chunk.size));
1229 for (const id of chunk.parents) {
1230 colors.normal(" <{");
1231 colors.yellow(id);
1232 colors.normal("}>");
1233 }
1234 for (const id of chunk.siblings) {
1235 colors.normal(" ={");
1236 colors.yellow(id);
1237 colors.normal("}=");
1238 }
1239 for (const id of chunk.children) {
1240 colors.normal(" >{");
1241 colors.yellow(id);
1242 colors.normal("}<");
1243 }
1244 if (chunk.childrenByOrder) {
1245 for (const name of Object.keys(chunk.childrenByOrder)) {
1246 const children = chunk.childrenByOrder[name];
1247 colors.normal(" ");
1248 colors.magenta(`(${name}:`);
1249 for (const id of children) {
1250 colors.normal(" {");
1251 colors.yellow(id);
1252 colors.normal("}");
1253 }
1254 colors.magenta(")");
1255 }
1256 }
1257 if (chunk.entry) {
1258 colors.yellow(" [entry]");
1259 } else if (chunk.initial) {
1260 colors.yellow(" [initial]");
1261 }
1262 if (chunk.rendered) {
1263 colors.green(" [rendered]");
1264 }
1265 if (chunk.recorded) {
1266 colors.green(" [recorded]");
1267 }
1268 if (chunk.reason) {
1269 colors.yellow(` ${chunk.reason}`);
1270 }
1271 newline();
1272 if (chunk.origins) {
1273 for (const origin of chunk.origins) {
1274 colors.normal(" > ");
1275 if (origin.reasons && origin.reasons.length) {
1276 colors.yellow(origin.reasons.join(" "));
1277 colors.normal(" ");
1278 }
1279 if (origin.request) {
1280 colors.normal(origin.request);
1281 colors.normal(" ");
1282 }
1283 if (origin.module) {
1284 colors.normal("[");
1285 colors.normal(origin.moduleId);
1286 colors.normal("] ");
1287 const module = modulesByIdentifier[`$${origin.module}`];
1288 if (module) {
1289 colors.bold(module.name);
1290 colors.normal(" ");
1291 }
1292 }
1293 if (origin.loc) {
1294 colors.normal(origin.loc);
1295 }
1296 newline();
1297 }
1298 }
1299 processModulesList(chunk, " ");
1300 }
1301 }
1302
1303 processModulesList(obj, "");
1304
1305 if (obj._showWarnings && obj.warnings) {
1306 for (const warning of obj.warnings) {
1307 newline();
1308 colors.yellow(`WARNING in ${warning}`);
1309 newline();
1310 }
1311 }
1312 if (obj._showErrors && obj.errors) {
1313 for (const error of obj.errors) {
1314 newline();
1315 colors.red(`ERROR in ${error}`);
1316 newline();
1317 }
1318 }
1319 if (obj.children) {
1320 for (const child of obj.children) {
1321 const childString = Stats.jsonToString(child, useColors);
1322 if (childString) {
1323 if (child.name) {
1324 colors.normal("Child ");
1325 colors.bold(child.name);
1326 colors.normal(":");
1327 } else {
1328 colors.normal("Child");
1329 }
1330 newline();
1331 buf.push(" ");
1332 buf.push(childString.replace(/\n/g, "\n "));
1333 newline();
1334 }
1335 }
1336 }
1337 if (obj.needAdditionalPass) {
1338 colors.yellow(
1339 "Compilation needs an additional pass and will compile again."
1340 );
1341 }
1342
1343 while (buf[buf.length - 1] === "\n") {
1344 buf.pop();
1345 }
1346 return buf.join("");
1347 }
1348
1349 static presetToOptions(name) {
1350 // Accepted values: none, errors-only, minimal, normal, detailed, verbose
1351 // Any other falsy value will behave as 'none', truthy values as 'normal'
1352 const pn =
1353 (typeof name === "string" && name.toLowerCase()) || name || "none";
1354 switch (pn) {
1355 case "none":
1356 return {
1357 all: false
1358 };
1359 case "verbose":
1360 return {
1361 entrypoints: true,
1362 chunkGroups: true,
1363 modules: false,
1364 chunks: true,
1365 chunkModules: true,
1366 chunkOrigins: true,
1367 depth: true,
1368 env: true,
1369 reasons: true,
1370 usedExports: true,
1371 providedExports: true,
1372 optimizationBailout: true,
1373 errorDetails: true,
1374 publicPath: true,
1375 exclude: false,
1376 maxModules: Infinity
1377 };
1378 case "detailed":
1379 return {
1380 entrypoints: true,
1381 chunkGroups: true,
1382 chunks: true,
1383 chunkModules: false,
1384 chunkOrigins: true,
1385 depth: true,
1386 usedExports: true,
1387 providedExports: true,
1388 optimizationBailout: true,
1389 errorDetails: true,
1390 publicPath: true,
1391 exclude: false,
1392 maxModules: Infinity
1393 };
1394 case "minimal":
1395 return {
1396 all: false,
1397 modules: true,
1398 maxModules: 0,
1399 errors: true,
1400 warnings: true
1401 };
1402 case "errors-only":
1403 return {
1404 all: false,
1405 errors: true,
1406 moduleTrace: true
1407 };
1408 default:
1409 return {};
1410 }
1411 }
1412
1413 static getChildOptions(options, idx) {
1414 let innerOptions;
1415 if (Array.isArray(options.children)) {
1416 if (idx < options.children.length) {
1417 innerOptions = options.children[idx];
1418 }
1419 } else if (typeof options.children === "object" && options.children) {
1420 innerOptions = options.children;
1421 }
1422 if (typeof innerOptions === "boolean" || typeof innerOptions === "string") {
1423 innerOptions = Stats.presetToOptions(innerOptions);
1424 }
1425 if (!innerOptions) {
1426 return options;
1427 }
1428 const childOptions = Object.assign({}, options);
1429 delete childOptions.children; // do not inherit children
1430 return Object.assign(childOptions, innerOptions);
1431 }
1432}
1433
1434module.exports = Stats;