HashedModuleIdsPlugin.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { DEFAULTS } = require("../config/defaults");
  7. const {
  8. compareModulesByPreOrderIndexOrIdentifier
  9. } = require("../util/comparators");
  10. const createSchemaValidation = require("../util/create-schema-validation");
  11. const createHash = require("../util/createHash");
  12. const {
  13. getUsedModuleIdsAndModules,
  14. getFullModuleName
  15. } = require("./IdHelpers");
  16. /** @typedef {import("../../declarations/plugins/HashedModuleIdsPlugin").HashedModuleIdsPluginOptions} HashedModuleIdsPluginOptions */
  17. /** @typedef {import("../Compiler")} Compiler */
  18. const validate = createSchemaValidation(
  19. require("../../schemas/plugins/HashedModuleIdsPlugin.check.js"),
  20. () => require("../../schemas/plugins/HashedModuleIdsPlugin.json"),
  21. {
  22. name: "Hashed Module Ids Plugin",
  23. baseDataPath: "options"
  24. }
  25. );
  26. class HashedModuleIdsPlugin {
  27. /**
  28. * @param {HashedModuleIdsPluginOptions=} options options object
  29. */
  30. constructor(options = {}) {
  31. validate(options);
  32. /** @type {HashedModuleIdsPluginOptions} */
  33. this.options = {
  34. context: undefined,
  35. hashFunction: DEFAULTS.HASH_FUNCTION,
  36. hashDigest: "base64",
  37. hashDigestLength: 4,
  38. ...options
  39. };
  40. }
  41. /**
  42. * Apply the plugin
  43. * @param {Compiler} compiler the compiler instance
  44. * @returns {void}
  45. */
  46. apply(compiler) {
  47. const options = this.options;
  48. compiler.hooks.compilation.tap("HashedModuleIdsPlugin", compilation => {
  49. compilation.hooks.moduleIds.tap("HashedModuleIdsPlugin", () => {
  50. const chunkGraph = compilation.chunkGraph;
  51. const context = this.options.context
  52. ? this.options.context
  53. : compiler.context;
  54. const [usedIds, modules] = getUsedModuleIdsAndModules(compilation);
  55. const modulesInNaturalOrder = modules.sort(
  56. compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph)
  57. );
  58. for (const module of modulesInNaturalOrder) {
  59. const ident = getFullModuleName(module, context, compiler.root);
  60. const hash = createHash(
  61. /** @type {NonNullable<HashedModuleIdsPluginOptions["hashFunction"]>} */ (
  62. options.hashFunction
  63. )
  64. );
  65. hash.update(ident || "");
  66. const hashId = /** @type {string} */ (
  67. hash.digest(options.hashDigest)
  68. );
  69. let len = options.hashDigestLength;
  70. while (usedIds.has(hashId.slice(0, len)))
  71. /** @type {number} */ (len)++;
  72. const moduleId = hashId.slice(0, len);
  73. chunkGraph.setModuleId(module, moduleId);
  74. usedIds.add(moduleId);
  75. }
  76. });
  77. });
  78. }
  79. }
  80. module.exports = HashedModuleIdsPlugin;