DeterministicModuleIdsPlugin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /**
  17. * @typedef {object} DeterministicModuleIdsPluginOptions
  18. * @property {string=} context context relative to which module identifiers are computed
  19. * @property {((module: Module) => boolean)=} test selector function for modules
  20. * @property {number=} maxLength maximum id length in digits (used as starting point)
  21. * @property {number=} salt hash salt for ids
  22. * @property {boolean=} fixedLength do not increase the maxLength to find an optimal id space size
  23. * @property {boolean=} failOnConflict throw an error when id conflicts occur (instead of rehashing)
  24. */
  25. const PLUGIN_NAME = "DeterministicModuleIdsPlugin";
  26. class DeterministicModuleIdsPlugin {
  27. /**
  28. * @param {DeterministicModuleIdsPluginOptions=} options options
  29. */
  30. constructor(options = {}) {
  31. this.options = options;
  32. }
  33. /**
  34. * Apply the plugin
  35. * @param {Compiler} compiler the compiler instance
  36. * @returns {void}
  37. */
  38. apply(compiler) {
  39. compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
  40. compilation.hooks.moduleIds.tap(PLUGIN_NAME, () => {
  41. const chunkGraph = compilation.chunkGraph;
  42. const context = this.options.context
  43. ? this.options.context
  44. : compiler.context;
  45. const maxLength = this.options.maxLength || 3;
  46. const failOnConflict = this.options.failOnConflict || false;
  47. const fixedLength = this.options.fixedLength || false;
  48. const salt = this.options.salt || 0;
  49. let conflicts = 0;
  50. const [usedIds, modules] = getUsedModuleIdsAndModules(
  51. compilation,
  52. this.options.test
  53. );
  54. assignDeterministicIds(
  55. modules,
  56. module => getFullModuleName(module, context, compiler.root),
  57. failOnConflict
  58. ? () => 0
  59. : compareModulesByPreOrderIndexOrIdentifier(
  60. compilation.moduleGraph
  61. ),
  62. (module, id) => {
  63. const size = usedIds.size;
  64. usedIds.add(`${id}`);
  65. if (size === usedIds.size) {
  66. conflicts++;
  67. return false;
  68. }
  69. chunkGraph.setModuleId(module, id);
  70. return true;
  71. },
  72. [10 ** maxLength],
  73. fixedLength ? 0 : 10,
  74. usedIds.size,
  75. salt
  76. );
  77. if (failOnConflict && conflicts)
  78. throw new Error(
  79. `Assigning deterministic module ids has lead to ${conflicts} conflict${
  80. conflicts > 1 ? "s" : ""
  81. }.\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).`
  82. );
  83. });
  84. });
  85. }
  86. }
  87. module.exports = DeterministicModuleIdsPlugin;