LargeModulesWarning.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const formatSize = require("../util/formatSize");
  7. const WebpackError = require("./WebpackError");
  8. /**
  9. * @typedef {object} LargeModuleDetails
  10. * @property {string} name the module, shortened for the report
  11. * @property {string} chunk the chunk it weighs down
  12. * @property {number} size bytes the module contributes
  13. * @property {number} chunkSize bytes the whole chunk contributes
  14. */
  15. class LargeModulesWarning extends WebpackError {
  16. /**
  17. * Creates an instance of LargeModulesWarning.
  18. * @param {LargeModuleDetails[]} modules the worst offenders, largest first
  19. * @param {number} total how many dominating modules there are in all
  20. */
  21. constructor(modules, total) {
  22. const list = modules
  23. .map((module) => {
  24. const share = Math.round((module.size / module.chunkSize) * 100);
  25. return `\n ${module.name} is ${share}% of '${module.chunk}' (${formatSize(
  26. module.size
  27. )} of ${formatSize(module.chunkSize)})`;
  28. })
  29. .join("");
  30. super(
  31. `large modules: ${total} ${total === 1 ? "module carries" : "modules carry"} most of the chunk ${total === 1 ? "it is" : "they are"} in:${list}\nEverything else in the chunk together weighs less than this one module, so splitting it out with 'optimization.splitChunks', loading it on demand, or replacing it is what changes the size.\nFor more info visit https://webpack.js.org/guides/code-splitting/`
  32. );
  33. /** @type {string} */
  34. this.name = "LargeModulesWarning";
  35. }
  36. }
  37. module.exports = LargeModulesWarning;