DuplicateModulesWarning.js 1.3 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} DuplicateModuleDetails
  10. * @property {string} name
  11. * @property {number} chunks how many chunks carry a copy
  12. * @property {number} wasted bytes the extra copies cost
  13. */
  14. class DuplicateModulesWarning extends WebpackError {
  15. /**
  16. * Creates an instance of DuplicateModulesWarning.
  17. * @param {DuplicateModuleDetails[]} modules the worst offenders, largest waste first
  18. * @param {number} wasted bytes all duplicated copies cost together
  19. */
  20. constructor(modules, wasted) {
  21. const list = modules
  22. .map(
  23. (module) =>
  24. `\n ${module.name} (in ${module.chunks} chunks, ${formatSize(
  25. module.wasted
  26. )} extra)`
  27. )
  28. .join("");
  29. super(
  30. `duplicate modules: copies of the same module across chunks add ${formatSize(
  31. wasted
  32. )}:${list}\nA module reached from several chunks is emitted into each of them unless a shared chunk holds it. 'optimization.splitChunks' can pull it into one.\nFor more info visit https://webpack.js.org/plugins/split-chunks-plugin/`
  33. );
  34. /** @type {string} */
  35. this.name = "DuplicateModulesWarning";
  36. }
  37. }
  38. module.exports = DuplicateModulesWarning;