DeterministicModuleIdsPlugin.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. assignDeterministicIds,
  11. getFullModuleName,
  12. getUsedModuleIdsAndModules
  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. /** @type {DeterministicModuleIdsPluginOptions} */
  32. this.options = options;
  33. }
  34. /**
  35. * Apply the plugin
  36. * @param {Compiler} compiler the compiler instance
  37. * @returns {void}
  38. */
  39. apply(compiler) {
  40. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  41. compilation.hooks.moduleIds.tap(PLUGIN_NAME, () => {
  42. const chunkGraph = compilation.chunkGraph;
  43. const context = this.options.context
  44. ? this.options.context
  45. : compiler.context;
  46. const maxLength = this.options.maxLength || 3;
  47. const failOnConflict = this.options.failOnConflict || false;
  48. const fixedLength = this.options.fixedLength || false;
  49. const salt = this.options.salt || 0;
  50. let conflicts = 0;
  51. const [usedIds, modules] = getUsedModuleIdsAndModules(
  52. compilation,
  53. this.options.test
  54. );
  55. assignDeterministicIds(
  56. modules,
  57. (module) => getFullModuleName(module, context, compiler.root),
  58. failOnConflict
  59. ? () => 0
  60. : compareModulesByPreOrderIndexOrIdentifier(
  61. compilation.moduleGraph
  62. ),
  63. (module, id) => {
  64. const size = usedIds.size;
  65. usedIds.add(`${id}`);
  66. if (size === usedIds.size) {
  67. conflicts++;
  68. return false;
  69. }
  70. chunkGraph.setModuleId(module, id);
  71. return true;
  72. },
  73. [10 ** maxLength],
  74. fixedLength ? 0 : 10,
  75. usedIds.size,
  76. salt
  77. );
  78. if (failOnConflict && conflicts) {
  79. throw new Error(
  80. `Assigning deterministic module ids has lead to ${conflicts} conflict${
  81. conflicts > 1 ? "s" : ""
  82. }.\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).`
  83. );
  84. }
  85. });
  86. });
  87. }
  88. }
  89. module.exports = DeterministicModuleIdsPlugin;