Stats.js 2.4 KB

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