Stats.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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("./Compilation").NormalizedStatsOptions} NormalizedStatsOptions */
  9. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsCompilation} StatsCompilation */
  10. class Stats {
  11. /**
  12. * @param {Compilation} compilation webpack compilation
  13. */
  14. constructor(compilation) {
  15. this.compilation = compilation;
  16. }
  17. get hash() {
  18. return this.compilation.hash;
  19. }
  20. get startTime() {
  21. return this.compilation.startTime;
  22. }
  23. get endTime() {
  24. return this.compilation.endTime;
  25. }
  26. /**
  27. * @returns {boolean} true if the compilation had a warning
  28. */
  29. hasWarnings() {
  30. return (
  31. this.compilation.getWarnings().length > 0 ||
  32. this.compilation.children.some(child => child.getStats().hasWarnings())
  33. );
  34. }
  35. /**
  36. * @returns {boolean} true if the compilation encountered an error
  37. */
  38. hasErrors() {
  39. return (
  40. this.compilation.errors.length > 0 ||
  41. this.compilation.children.some(child => child.getStats().hasErrors())
  42. );
  43. }
  44. /**
  45. * @param {(string | boolean | StatsOptions)=} options stats options
  46. * @returns {StatsCompilation} json output
  47. */
  48. toJson(options) {
  49. const normalizedOptions = this.compilation.createStatsOptions(options, {
  50. forToString: false
  51. });
  52. const statsFactory = this.compilation.createStatsFactory(normalizedOptions);
  53. return statsFactory.create("compilation", this.compilation, {
  54. compilation: this.compilation
  55. });
  56. }
  57. /**
  58. * @param {(string | boolean | StatsOptions)=} options stats options
  59. * @returns {string} string output
  60. */
  61. toString(options) {
  62. const normalizedOptions = this.compilation.createStatsOptions(options, {
  63. forToString: true
  64. });
  65. const statsFactory = this.compilation.createStatsFactory(normalizedOptions);
  66. const statsPrinter = this.compilation.createStatsPrinter(normalizedOptions);
  67. const data = statsFactory.create("compilation", this.compilation, {
  68. compilation: this.compilation
  69. });
  70. const result = statsPrinter.print("compilation", data);
  71. return result === undefined ? "" : result;
  72. }
  73. }
  74. module.exports = Stats;