webpack/lib/Stats.js

85 lines
1.9 KiB
JavaScript
Raw Normal View History

2013-01-31 01:49:25 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
2013-01-31 01:49:25 +08:00
/** @typedef {import("../declarations/WebpackOptions").StatsOptions} StatsOptions */
/** @typedef {import("./Compilation")} Compilation */
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsCompilation} ToJsonOutput */
2018-12-10 18:34:59 +08:00
class Stats {
/**
* @param {Compilation} compilation webpack compilation
*/
constructor(compilation) {
this.compilation = compilation;
}
get hash() {
return this.compilation.hash;
}
get startTime() {
return this.compilation.startTime;
}
get endTime() {
return this.compilation.endTime;
}
2013-01-31 01:49:25 +08:00
2018-12-10 18:34:59 +08:00
/**
2020-06-04 14:04:07 +08:00
* @returns {boolean} true if the compilation had a warning
2018-12-10 18:34:59 +08:00
*/
hasWarnings() {
2018-02-25 09:00:20 +08:00
return (
this.compilation.warnings.length > 0 ||
this.compilation.children.some(child => child.getStats().hasWarnings())
);
}
2015-07-13 06:20:09 +08:00
2018-12-10 18:34:59 +08:00
/**
2020-06-04 14:04:07 +08:00
* @returns {boolean} true if the compilation encountered an error
2018-12-10 18:34:59 +08:00
*/
hasErrors() {
2018-02-25 09:00:20 +08:00
return (
this.compilation.errors.length > 0 ||
this.compilation.children.some(child => child.getStats().hasErrors())
);
2015-07-13 06:20:09 +08:00
}
/**
* @param {(string|StatsOptions)=} options stats options
* @returns {ToJsonOutput} json output
*/
toJson(options) {
options = this.compilation.createStatsOptions(options, {
forToString: false
2015-04-17 16:17:10 +08:00
});
const statsFactory = this.compilation.createStatsFactory(options);
return statsFactory.create("compilation", this.compilation, {
compilation: this.compilation
});
2013-01-31 01:49:25 +08:00
}
toString(options) {
options = this.compilation.createStatsOptions(options, {
forToString: true
});
const statsFactory = this.compilation.createStatsFactory(options);
const statsPrinter = this.compilation.createStatsPrinter(options);
const data = statsFactory.create("compilation", this.compilation, {
compilation: this.compilation
});
const result = statsPrinter.print("compilation", data);
return result === undefined ? "" : result;
}
}
module.exports = Stats;