* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 36 | * @returns {void} |
| 37 | */ |
| 38 | apply(compiler) { |
| 39 | compiler.hooks.validate.tap(PLUGIN_NAME, () => { |
| 40 | compiler.validate( |
| 41 | () => require("../../schemas/plugins/ids/HashedModuleIdsPlugin.json"), |
| 42 | this.options, |
| 43 | { |
| 44 | name: "Hashed Module Ids Plugin", |
| 45 | baseDataPath: "options" |
| 46 | }, |
| 47 | (options) => |
| 48 | require("../../schemas/plugins/ids/HashedModuleIdsPlugin.check")( |
| 49 | options |
| 50 | ) |
| 51 | ); |
| 52 | }); |
| 53 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { |
| 54 | compilation.hooks.moduleIds.tap(PLUGIN_NAME, () => { |
| 55 | const chunkGraph = compilation.chunkGraph; |
| 56 | const context = this.options.context |
| 57 | ? this.options.context |
| 58 | : compiler.context; |
| 59 | |
| 60 | const [usedIds, modules] = getUsedModuleIdsAndModules(compilation); |
| 61 | const modulesInNaturalOrder = modules.sort( |
| 62 | compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph) |
| 63 | ); |
| 64 | for (const module of modulesInNaturalOrder) { |
| 65 | const ident = getFullModuleName(module, context, compiler.root); |
| 66 | const hash = createHash( |
| 67 | this.options.hashFunction || DEFAULTS.HASH_FUNCTION |
| 68 | ); |
| 69 | hash.update(ident || ""); |
| 70 | const hashId = hash.digest(this.options.hashDigest || "base64"); |
| 71 | let len = this.options.hashDigestLength || 4; |
| 72 | while (usedIds.has(hashId.slice(0, len))) { |
| 73 | /** @type {number} */ (len)++; |
| 74 | } |
| 75 | const moduleId = hashId.slice(0, len); |
| 76 | chunkGraph.setModuleId(module, moduleId); |
| 77 | usedIds.add(moduleId); |
| 78 | } |
| 79 | }); |
| 80 | }); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | module.exports = HashedModuleIdsPlugin; |
nothing calls this directly
no test coverage detected