* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 46 | * @returns {void} |
| 47 | */ |
| 48 | apply(compiler) { |
| 49 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { |
| 50 | compilation.hooks.moduleIds.tap(PLUGIN_NAME, () => { |
| 51 | const chunkGraph = compilation.chunkGraph; |
| 52 | const context = this.options.context |
| 53 | ? this.options.context |
| 54 | : compiler.context; |
| 55 | const maxLength = this.options.maxLength || 3; |
| 56 | const failOnConflict = this.options.failOnConflict || false; |
| 57 | const fixedLength = this.options.fixedLength || false; |
| 58 | const salt = this.options.salt || 0; |
| 59 | let conflicts = 0; |
| 60 | |
| 61 | const [usedIds, modules] = getUsedModuleIdsAndModules( |
| 62 | compilation, |
| 63 | this.options.test |
| 64 | ); |
| 65 | assignDeterministicIds( |
| 66 | modules, |
| 67 | (module) => getFullModuleName(module, context, compiler.root), |
| 68 | failOnConflict |
| 69 | ? () => 0 |
| 70 | : compareModulesByPreOrderIndexOrIdentifier( |
| 71 | compilation.moduleGraph |
| 72 | ), |
| 73 | (module, id) => { |
| 74 | const size = usedIds.size; |
| 75 | usedIds.add(`${id}`); |
| 76 | if (size === usedIds.size) { |
| 77 | conflicts++; |
| 78 | return false; |
| 79 | } |
| 80 | chunkGraph.setModuleId(module, id); |
| 81 | return true; |
| 82 | }, |
| 83 | [10 ** maxLength], |
| 84 | fixedLength ? 0 : 10, |
| 85 | usedIds.size, |
| 86 | salt |
| 87 | ); |
| 88 | if (failOnConflict && conflicts) { |
| 89 | throw new Error( |
| 90 | `Assigning deterministic module ids has lead to ${conflicts} conflict${ |
| 91 | conflicts > 1 ? "s" : "" |
| 92 | }.\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).` |
| 93 | ); |
| 94 | } |
| 95 | }); |
| 96 | }); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | module.exports = DeterministicModuleIdsPlugin; |
nothing calls this directly
no test coverage detected