Stats.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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|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. toString(options) {
  57. options = this.compilation.createStatsOptions(options, {
  58. forToString: true
  59. });
  60. const statsFactory = this.compilation.createStatsFactory(options);
  61. const statsPrinter = this.compilation.createStatsPrinter(options);
  62. const data = statsFactory.create("compilation", this.compilation, {
  63. compilation: this.compilation
  64. });
  65. const result = statsPrinter.print("compilation", data);
  66. return result === undefined ? "" : result;
  67. }
  68. }
  69. module.exports = Stats;