123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- "use strict";
- const identifierUtils = require("./util/identifier");
- const indent = (str, prefix) => {
- const rem = str.replace(/\n([^\n])/g, `\n${prefix}$1`);
- return prefix + rem;
- };
- class MultiStats {
-
- constructor(stats) {
- this.stats = stats;
- }
- get hash() {
- return this.stats.map(stat => stat.hash).join("");
- }
-
- hasErrors() {
- return this.stats.some(stat => stat.hasErrors());
- }
-
- hasWarnings() {
- return this.stats.some(stat => stat.hasWarnings());
- }
-
- _createChildOptions(options, context) {
- const getCreateStatsOptions = () => {
- if (!options) {
- options = {};
- }
- const { children: childrenOptions = undefined, ...baseOptions } =
- typeof options === "string"
- ? { preset: options }
- : (options);
- return { childrenOptions, baseOptions };
- };
- const children = this.stats.map((stat, idx) => {
- if (typeof options === "boolean") {
- return stat.compilation.createStatsOptions(options, context);
- }
- const { childrenOptions, baseOptions } = getCreateStatsOptions();
- const childOptions = Array.isArray(childrenOptions)
- ? childrenOptions[idx]
- : childrenOptions;
- return stat.compilation.createStatsOptions(
- {
- ...baseOptions,
- ...(typeof childOptions === "string"
- ? { preset: childOptions }
- : childOptions && typeof childOptions === "object"
- ? childOptions
- : undefined)
- },
- context
- );
- });
- return {
- version: children.every(o => o.version),
- hash: children.every(o => o.hash),
- errorsCount: children.every(o => o.errorsCount),
- warningsCount: children.every(o => o.warningsCount),
- errors: children.every(o => o.errors),
- warnings: children.every(o => o.warnings),
- children
- };
- }
-
- toJson(options) {
- const childOptions = this._createChildOptions(options, {
- forToString: false
- });
-
- const obj = {};
- obj.children = this.stats.map((stat, idx) => {
- const obj = stat.toJson(childOptions.children[idx]);
- const compilationName = stat.compilation.name;
- const name =
- compilationName &&
- identifierUtils.makePathsRelative(
- stat.compilation.compiler.context,
- compilationName,
- stat.compilation.compiler.root
- );
- obj.name = name;
- return obj;
- });
- if (childOptions.version) {
- obj.version = obj.children[0].version;
- }
- if (childOptions.hash) {
- obj.hash = obj.children.map(j => j.hash).join("");
- }
-
- const mapError = (j, obj) => ({
- ...obj,
- compilerPath: obj.compilerPath ? `${j.name}.${obj.compilerPath}` : j.name
- });
- if (childOptions.errors) {
- obj.errors = [];
- for (const j of obj.children) {
- const errors =
-
- (j.errors);
- for (const i of errors) {
- obj.errors.push(mapError(j, i));
- }
- }
- }
- if (childOptions.warnings) {
- obj.warnings = [];
- for (const j of obj.children) {
- const warnings =
-
- (j.warnings);
- for (const i of warnings) {
- obj.warnings.push(mapError(j, i));
- }
- }
- }
- if (childOptions.errorsCount) {
- obj.errorsCount = 0;
- for (const j of obj.children) {
- obj.errorsCount += (j.errorsCount);
- }
- }
- if (childOptions.warningsCount) {
- obj.warningsCount = 0;
- for (const j of obj.children) {
- obj.warningsCount += (j.warningsCount);
- }
- }
- return obj;
- }
-
- toString(options) {
- const childOptions = this._createChildOptions(options, {
- forToString: true
- });
- const results = this.stats.map((stat, idx) => {
- const str = stat.toString(childOptions.children[idx]);
- const compilationName = stat.compilation.name;
- const name =
- compilationName &&
- identifierUtils
- .makePathsRelative(
- stat.compilation.compiler.context,
- compilationName,
- stat.compilation.compiler.root
- )
- .replace(/\|/g, " ");
- if (!str) return str;
- return name ? `${name}:\n${indent(str, " ")}` : str;
- });
- return results.filter(Boolean).join("\n\n");
- }
- }
- module.exports = MultiStats;
|