Stats.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("../declarations/WebpackOptions").StatsOptions} StatsOptions */
  7. /** @typedef {import("./Compilation")} Compilation */
  8. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsCompilation} StatsCompilation */
  9. class Stats {
  10. /**
  11. * @param {Compilation} compilation webpack compilation
  12. */
  13. constructor(compilation) {
  14. this.compilation = compilation;
  15. }
  16. get hash() {
  17. return this.compilation.hash;
  18. }
  19. get startTime() {
  20. return this.compilation.startTime;
  21. }
  22. get endTime() {
  23. return this.compilation.endTime;
  24. }
  25. /**
  26. * @returns {boolean} true if the compilation had a warning
  27. */
  28. hasWarnings() {
  29. return (
  30. this.compilation.warnings.length > 0 ||
  31. this.compilation.children.some(child => child.getStats().hasWarnings())
  32. );
  33. }
  34. /**
  35. * @returns {boolean} true if the compilation encountered an error
  36. */
  37. hasErrors() {
  38. return (
  39. this.compilation.errors.length > 0 ||
  40. this.compilation.children.some(child => child.getStats().hasErrors())
  41. );
  42. }
  43. /**
  44. * @param {(string | boolean | StatsOptions)=} options stats options
  45. * @returns {StatsCompilation} json output
  46. */
  47. toJson(options) {
  48. options = this.compilation.createStatsOptions(options, {
  49. forToString: false
  50. });
  51. const statsFactory = this.compilation.createStatsFactory(options);
  52. return statsFactory.create("compilation", this.compilation, {
  53. compilation: this.compilation
  54. });
  55. }
  56. /**
  57. * @param {(string | boolean | StatsOptions)=} options stats options
  58. * @returns {string} string output
  59. */
  60. toString(options) {
  61. options = this.compilation.createStatsOptions(options, {
  62. forToString: true
  63. });
  64. const statsFactory = this.compilation.createStatsFactory(options);
  65. const statsPrinter = this.compilation.createStatsPrinter(options);
  66. const data = statsFactory.create("compilation", this.compilation, {
  67. compilation: this.compilation
  68. });
  69. const result = statsPrinter.print("compilation", data);
  70. return result === undefined ? "" : result;
  71. }
  72. }
  73. module.exports = Stats;