DeterministicModuleIdsPlugin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Florent Cailhol @ooflorent
  4. */
  5. "use strict";
  6. const {
  7. compareModulesByPreOrderIndexOrIdentifier
  8. } = require("../util/comparators");
  9. const {
  10. getUsedModuleIdsAndModules,
  11. getFullModuleName,
  12. assignDeterministicIds
  13. } = require("./IdHelpers");
  14. /** @typedef {import("../Compiler")} Compiler */
  15. /** @typedef {import("../Module")} Module */
  16. class DeterministicModuleIdsPlugin {
  17. /**
  18. * @param {Object} options options
  19. * @param {string=} options.context context relative to which module identifiers are computed
  20. * @param {function(Module): boolean=} options.test selector function for modules
  21. * @param {number=} options.maxLength maximum id length in digits (used as starting point)
  22. * @param {number=} options.salt hash salt for ids
  23. * @param {boolean=} options.fixedLength do not increase the maxLength to find an optimal id space size
  24. * @param {boolean=} options.failOnConflict throw an error when id conflicts occur (instead of rehashing)
  25. */
  26. constructor(options = {}) {
  27. this.options = options;
  28. }
  29. /**
  30. * Apply the plugin
  31. * @param {Compiler} compiler the compiler instance
  32. * @returns {void}
  33. */
  34. apply(compiler) {
  35. compiler.hooks.compilation.tap(
  36. "DeterministicModuleIdsPlugin",
  37. compilation => {
  38. compilation.hooks.moduleIds.tap("DeterministicModuleIdsPlugin", () => {
  39. const chunkGraph = compilation.chunkGraph;
  40. const context = this.options.context
  41. ? this.options.context
  42. : compiler.context;
  43. const maxLength = this.options.maxLength || 3;
  44. const failOnConflict = this.options.failOnConflict || false;
  45. const fixedLength = this.options.fixedLength || false;
  46. const salt = this.options.salt || 0;
  47. let conflicts = 0;
  48. const [usedIds, modules] = getUsedModuleIdsAndModules(
  49. compilation,
  50. this.options.test
  51. );
  52. assignDeterministicIds(
  53. modules,
  54. module => getFullModuleName(module, context, compiler.root),
  55. failOnConflict
  56. ? () => 0
  57. : compareModulesByPreOrderIndexOrIdentifier(
  58. compilation.moduleGraph
  59. ),
  60. (module, id) => {
  61. const size = usedIds.size;
  62. usedIds.add(`${id}`);
  63. if (size === usedIds.size) {
  64. conflicts++;
  65. return false;
  66. }
  67. chunkGraph.setModuleId(module, id);
  68. return true;
  69. },
  70. [Math.pow(10, maxLength)],
  71. fixedLength ? 0 : 10,
  72. usedIds.size,
  73. salt
  74. );
  75. if (failOnConflict && conflicts)
  76. throw new Error(
  77. `Assigning deterministic module ids has lead to ${conflicts} conflict${
  78. conflicts > 1 ? "s" : ""
  79. }.\nIncrease the 'maxLength' to increase the id space and make conflicts less likely (recommended when there are many conflicts or application is expected to grow), or add an 'salt' number to try another hash starting value in the same id space (recommended when there is only a single conflict).`
  80. );
  81. });
  82. }
  83. );
  84. }
  85. }
  86. module.exports = DeterministicModuleIdsPlugin;