1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Florent Cailhol @ooflorent
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const {
|
---|
9 | compareModulesByPreOrderIndexOrIdentifier
|
---|
10 | } = require("../util/comparators");
|
---|
11 | const {
|
---|
12 | getUsedModuleIds,
|
---|
13 | getFullModuleName,
|
---|
14 | assignDeterministicIds
|
---|
15 | } = require("./IdHelpers");
|
---|
16 |
|
---|
17 | /** @typedef {import("../Compiler")} Compiler */
|
---|
18 | /** @typedef {import("../Module")} Module */
|
---|
19 |
|
---|
20 | class DeterministicModuleIdsPlugin {
|
---|
21 | constructor(options) {
|
---|
22 | this.options = options || {};
|
---|
23 | }
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Apply the plugin
|
---|
27 | * @param {Compiler} compiler the compiler instance
|
---|
28 | * @returns {void}
|
---|
29 | */
|
---|
30 | apply(compiler) {
|
---|
31 | compiler.hooks.compilation.tap(
|
---|
32 | "DeterministicModuleIdsPlugin",
|
---|
33 | compilation => {
|
---|
34 | compilation.hooks.moduleIds.tap(
|
---|
35 | "DeterministicModuleIdsPlugin",
|
---|
36 | modules => {
|
---|
37 | const chunkGraph = compilation.chunkGraph;
|
---|
38 | const context = this.options.context
|
---|
39 | ? this.options.context
|
---|
40 | : compiler.context;
|
---|
41 | const maxLength = this.options.maxLength || 3;
|
---|
42 |
|
---|
43 | const usedIds = getUsedModuleIds(compilation);
|
---|
44 | assignDeterministicIds(
|
---|
45 | Array.from(modules).filter(module => {
|
---|
46 | if (!module.needId) return false;
|
---|
47 | if (chunkGraph.getNumberOfModuleChunks(module) === 0)
|
---|
48 | return false;
|
---|
49 | return chunkGraph.getModuleId(module) === null;
|
---|
50 | }),
|
---|
51 | module => getFullModuleName(module, context, compiler.root),
|
---|
52 | compareModulesByPreOrderIndexOrIdentifier(
|
---|
53 | compilation.moduleGraph
|
---|
54 | ),
|
---|
55 | (module, id) => {
|
---|
56 | const size = usedIds.size;
|
---|
57 | usedIds.add(`${id}`);
|
---|
58 | if (size === usedIds.size) return false;
|
---|
59 | chunkGraph.setModuleId(module, id);
|
---|
60 | return true;
|
---|
61 | },
|
---|
62 | [Math.pow(10, maxLength)],
|
---|
63 | 10,
|
---|
64 | usedIds.size
|
---|
65 | );
|
---|
66 | }
|
---|
67 | );
|
---|
68 | }
|
---|
69 | );
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | module.exports = DeterministicModuleIdsPlugin;
|
---|