source: imaps-frontend/node_modules/webpack/lib/ids/DeterministicModuleIdsPlugin.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Florent Cailhol @ooflorent
4*/
5
6"use strict";
7
8const {
9 compareModulesByPreOrderIndexOrIdentifier
10} = require("../util/comparators");
11const {
12 getUsedModuleIdsAndModules,
13 getFullModuleName,
14 assignDeterministicIds
15} = require("./IdHelpers");
16
17/** @typedef {import("../Compiler")} Compiler */
18/** @typedef {import("../Module")} Module */
19
20/**
21 * @typedef {object} DeterministicModuleIdsPluginOptions
22 * @property {string=} context context relative to which module identifiers are computed
23 * @property {function(Module): boolean=} test selector function for modules
24 * @property {number=} maxLength maximum id length in digits (used as starting point)
25 * @property {number=} salt hash salt for ids
26 * @property {boolean=} fixedLength do not increase the maxLength to find an optimal id space size
27 * @property {boolean=} failOnConflict throw an error when id conflicts occur (instead of rehashing)
28 */
29
30class DeterministicModuleIdsPlugin {
31 /**
32 * @param {DeterministicModuleIdsPluginOptions} [options] options
33 */
34 constructor(options = {}) {
35 this.options = options;
36 }
37
38 /**
39 * Apply the plugin
40 * @param {Compiler} compiler the compiler instance
41 * @returns {void}
42 */
43 apply(compiler) {
44 compiler.hooks.compilation.tap(
45 "DeterministicModuleIdsPlugin",
46 compilation => {
47 compilation.hooks.moduleIds.tap("DeterministicModuleIdsPlugin", () => {
48 const chunkGraph = compilation.chunkGraph;
49 const context = this.options.context
50 ? this.options.context
51 : compiler.context;
52 const maxLength = this.options.maxLength || 3;
53 const failOnConflict = this.options.failOnConflict || false;
54 const fixedLength = this.options.fixedLength || false;
55 const salt = this.options.salt || 0;
56 let conflicts = 0;
57
58 const [usedIds, modules] = getUsedModuleIdsAndModules(
59 compilation,
60 this.options.test
61 );
62 assignDeterministicIds(
63 modules,
64 module => getFullModuleName(module, context, compiler.root),
65 failOnConflict
66 ? () => 0
67 : compareModulesByPreOrderIndexOrIdentifier(
68 compilation.moduleGraph
69 ),
70 (module, id) => {
71 const size = usedIds.size;
72 usedIds.add(`${id}`);
73 if (size === usedIds.size) {
74 conflicts++;
75 return false;
76 }
77 chunkGraph.setModuleId(module, id);
78 return true;
79 },
80 [10 ** maxLength],
81 fixedLength ? 0 : 10,
82 usedIds.size,
83 salt
84 );
85 if (failOnConflict && conflicts)
86 throw new Error(
87 `Assigning deterministic module ids has lead to ${conflicts} conflict${
88 conflicts > 1 ? "s" : ""
89 }.\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).`
90 );
91 });
92 }
93 );
94 }
95}
96
97module.exports = DeterministicModuleIdsPlugin;
Note: See TracBrowser for help on using the repository browser.