HotspotsWarning.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const WebpackError = require("./WebpackError");
  7. /**
  8. * @typedef {object} HotspotDetails
  9. * @property {"loader" | "plugin"} kind what the name refers to
  10. * @property {string} name the loader or plugin, as it identified itself
  11. * @property {number} ms milliseconds it held the main thread
  12. * @property {number} runs how many times it ran
  13. */
  14. /**
  15. * @typedef {object} HookDetails
  16. * @property {string} name the hook
  17. * @property {number} ms milliseconds its taps held the main thread
  18. */
  19. class HotspotsWarning extends WebpackError {
  20. /**
  21. * Creates an instance of HotspotsWarning.
  22. * @param {HotspotDetails[]} hotspots the worst offenders, slowest first
  23. * @param {number} total how many are over the threshold in all
  24. * @param {HookDetails[]} hooks the same time grouped by hook instead
  25. */
  26. constructor(hotspots, total, hooks) {
  27. const list = hotspots
  28. .map(
  29. (hotspot) =>
  30. `\n ${hotspot.kind} ${hotspot.name} (${Math.round(hotspot.ms)} ms over ${
  31. hotspot.runs
  32. } ${hotspot.runs === 1 ? "run" : "runs"})`
  33. )
  34. .join("");
  35. const byHook =
  36. hooks.length > 0
  37. ? `\nThe same time, grouped by the hook it ran under:${hooks
  38. .map((hook) => `\n ${hook.name} (${Math.round(hook.ms)} ms)`)
  39. .join("")}`
  40. : "";
  41. super(
  42. `hotspots: ${total} ${total === 1 ? "thing holds" : "things hold"} the main thread long enough to be worth looking at:${list}${byHook}\nThis is the time each one held the main thread itself, with anything it called out to charged to that instead — so it is what the loader or plugin costs, not what it waited for. Only synchronous stretches count, so work resumed after an await is missing from it. 'ProfilingPlugin' records the same work as a trace when the ordering matters.\nFor more info visit https://webpack.js.org/plugins/profiling-plugin/`
  43. );
  44. /** @type {string} */
  45. this.name = "HotspotsWarning";
  46. }
  47. }
  48. module.exports = HotspotsWarning;