webpack/lib/MultiStats.js

98 lines
2.3 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
const Stats = require("./Stats");
2018-02-25 09:00:20 +08:00
const optionOrFallback = (optionValue, fallbackValue) =>
optionValue !== undefined ? optionValue : fallbackValue;
class MultiStats {
2018-12-10 18:34:59 +08:00
/**
* @param {Stats[]} stats the child stats
*/
constructor(stats) {
this.stats = stats;
this.hash = stats.map(stat => stat.hash).join("");
}
2018-12-10 18:34:59 +08:00
/**
* @returns {boolean} true if a child compilation encountered an error
*/
hasErrors() {
2018-12-10 18:34:59 +08:00
return this.stats.some(stat => stat.hasErrors());
}
2018-12-10 18:34:59 +08:00
/**
* @returns {boolean} true if a child compilation had a warning
*/
hasWarnings() {
2018-12-10 18:34:59 +08:00
return this.stats.some(stat => stat.hasWarnings());
}
toJson(options, forToString) {
2018-02-25 09:00:20 +08:00
if (typeof options === "boolean" || typeof options === "string") {
options = Stats.presetToOptions(options);
2018-02-25 09:00:20 +08:00
} else if (!options) {
options = {};
}
const jsons = this.stats.map((stat, idx) => {
const childOptions = Stats.getChildOptions(options, idx);
const obj = stat.toJson(childOptions, forToString);
obj.name = stat.compilation && stat.compilation.name;
return obj;
});
2018-02-25 09:00:20 +08:00
const showVersion =
2018-08-21 08:26:50 +08:00
options.version === undefined
2018-02-25 09:00:20 +08:00
? jsons.every(j => j.version)
: options.version !== false;
const showHash =
2018-08-21 08:26:50 +08:00
options.hash === undefined
2018-02-25 09:00:20 +08:00
? jsons.every(j => j.hash)
: options.hash !== false;
if (showVersion) {
for (const j of jsons) {
delete j.version;
2018-01-22 20:52:43 +08:00
}
}
const obj = {
errors: jsons.reduce((arr, j) => {
2018-02-25 09:00:20 +08:00
return arr.concat(
j.errors.map(msg => {
return `(${j.name}) ${msg}`;
})
);
}, []),
warnings: jsons.reduce((arr, j) => {
2018-02-25 09:00:20 +08:00
return arr.concat(
j.warnings.map(msg => {
return `(${j.name}) ${msg}`;
})
);
}, [])
};
2018-02-25 09:00:20 +08:00
if (showVersion) obj.version = require("../package.json").version;
if (showHash) obj.hash = this.hash;
if (options.children !== false) obj.children = jsons;
return obj;
}
toString(options) {
2018-02-25 09:00:20 +08:00
if (typeof options === "boolean" || typeof options === "string") {
options = Stats.presetToOptions(options);
2018-02-25 09:00:20 +08:00
} else if (!options) {
options = {};
}
2017-03-05 05:04:35 +08:00
const useColors = optionOrFallback(options.colors, false);
const obj = this.toJson(options, true);
return Stats.jsonToString(obj, useColors);
}
}
module.exports = MultiStats;