Stats.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. options = this.compilation.createStatsOptions(options, {
  50. forToString: false
  51. });
  52. const statsFactory = this.compilation.createStatsFactory(
  53. /** @type {NormalizedStatsOptions} */ (options)
  54. );
  55. return statsFactory.create("compilation", this.compilation, {
  56. compilation: this.compilation
  57. });
  58. }
  59. /**
  60. * @param {(string | boolean | StatsOptions)=} options stats options
  61. * @returns {string} string output
  62. */
  63. toString(options) {
  64. options = this.compilation.createStatsOptions(options, {
  65. forToString: true
  66. });
  67. const statsFactory = this.compilation.createStatsFactory(
  68. /** @type {NormalizedStatsOptions} */ (options)
  69. );
  70. const statsPrinter = this.compilation.createStatsPrinter(
  71. /** @type {NormalizedStatsOptions} */ (options)
  72. );
  73. const data = statsFactory.create("compilation", this.compilation, {
  74. compilation: this.compilation
  75. });
  76. const result = statsPrinter.print("compilation", data);
  77. return result === undefined ? "" : result;
  78. }
  79. }
  80. module.exports = Stats;