source: frontend/node_modules/webpack/lib/ids/DeterministicModuleIdsPlugin.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.2 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 assignDeterministicIds,
13 getFullModuleName,
14 getUsedModuleIdsAndModules
15} = require("./IdHelpers");
16
17/** @typedef {import("../Compiler")} Compiler */
18/** @typedef {import("../Module")} Module */
19
20/**
21 * Defines the deterministic module ids plugin options type used by this module.
22 * @typedef {object} DeterministicModuleIdsPluginOptions
23 * @property {string=} context context relative to which module identifiers are computed
24 * @property {((module: Module) => boolean)=} test selector function for modules
25 * @property {number=} maxLength maximum id length in digits (used as starting point)
26 * @property {number=} salt hash salt for ids
27 * @property {boolean=} fixedLength do not increase the maxLength to find an optimal id space size
28 * @property {boolean=} failOnConflict throw an error when id conflicts occur (instead of rehashing)
29 */
30
31const PLUGIN_NAME = "DeterministicModuleIdsPlugin";
32
33class DeterministicModuleIdsPlugin {
34 /**
35 * Creates an instance of DeterministicModuleIdsPlugin.
36 * @param {DeterministicModuleIdsPluginOptions=} options options
37 */
38 constructor(options = {}) {
39 /** @type {DeterministicModuleIdsPluginOptions} */
40 this.options = options;
41 }
42
43 /**
44 * Applies the plugin by registering its hooks on the compiler.
45 * @param {Compiler} compiler the compiler instance
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
100module.exports = DeterministicModuleIdsPlugin;
Note: See TracBrowser for help on using the repository browser.