1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const { compareIds } = require("../util/comparators");
|
---|
9 |
|
---|
10 | /** @typedef {import("../Chunk")} Chunk */
|
---|
11 | /** @typedef {import("../Chunk").ChunkId} ChunkId */
|
---|
12 | /** @typedef {import("../Compiler")} Compiler */
|
---|
13 | /** @typedef {import("../Module")} Module */
|
---|
14 |
|
---|
15 | class FlagIncludedChunksPlugin {
|
---|
16 | /**
|
---|
17 | * Apply the plugin
|
---|
18 | * @param {Compiler} compiler the compiler instance
|
---|
19 | * @returns {void}
|
---|
20 | */
|
---|
21 | apply(compiler) {
|
---|
22 | compiler.hooks.compilation.tap("FlagIncludedChunksPlugin", compilation => {
|
---|
23 | compilation.hooks.optimizeChunkIds.tap(
|
---|
24 | "FlagIncludedChunksPlugin",
|
---|
25 | chunks => {
|
---|
26 | const chunkGraph = compilation.chunkGraph;
|
---|
27 |
|
---|
28 | // prepare two bit integers for each module
|
---|
29 | // 2^31 is the max number represented as SMI in v8
|
---|
30 | // we want the bits distributed this way:
|
---|
31 | // the bit 2^31 is pretty rar and only one module should get it
|
---|
32 | // so it has a probability of 1 / modulesCount
|
---|
33 | // the first bit (2^0) is the easiest and every module could get it
|
---|
34 | // if it doesn't get a better bit
|
---|
35 | // from bit 2^n to 2^(n+1) there is a probability of p
|
---|
36 | // so 1 / modulesCount == p^31
|
---|
37 | // <=> p = sqrt31(1 / modulesCount)
|
---|
38 | // so we use a modulo of 1 / sqrt31(1 / modulesCount)
|
---|
39 | /** @type {WeakMap<Module, number>} */
|
---|
40 | const moduleBits = new WeakMap();
|
---|
41 | const modulesCount = compilation.modules.size;
|
---|
42 |
|
---|
43 | // precalculate the modulo values for each bit
|
---|
44 | const modulo = 1 / (1 / modulesCount) ** (1 / 31);
|
---|
45 | const modulos = Array.from(
|
---|
46 | { length: 31 },
|
---|
47 | (x, i) => (modulo ** i) | 0
|
---|
48 | );
|
---|
49 |
|
---|
50 | // iterate all modules to generate bit values
|
---|
51 | let i = 0;
|
---|
52 | for (const module of compilation.modules) {
|
---|
53 | let bit = 30;
|
---|
54 | while (i % modulos[bit] !== 0) {
|
---|
55 | bit--;
|
---|
56 | }
|
---|
57 | moduleBits.set(module, 1 << bit);
|
---|
58 | i++;
|
---|
59 | }
|
---|
60 |
|
---|
61 | // iterate all chunks to generate bitmaps
|
---|
62 | /** @type {WeakMap<Chunk, number>} */
|
---|
63 | const chunkModulesHash = new WeakMap();
|
---|
64 | for (const chunk of chunks) {
|
---|
65 | let hash = 0;
|
---|
66 | for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
|
---|
67 | hash |= /** @type {number} */ (moduleBits.get(module));
|
---|
68 | }
|
---|
69 | chunkModulesHash.set(chunk, hash);
|
---|
70 | }
|
---|
71 |
|
---|
72 | for (const chunkA of chunks) {
|
---|
73 | const chunkAHash =
|
---|
74 | /** @type {number} */
|
---|
75 | (chunkModulesHash.get(chunkA));
|
---|
76 | const chunkAModulesCount =
|
---|
77 | chunkGraph.getNumberOfChunkModules(chunkA);
|
---|
78 | if (chunkAModulesCount === 0) continue;
|
---|
79 | let bestModule;
|
---|
80 | for (const module of chunkGraph.getChunkModulesIterable(chunkA)) {
|
---|
81 | if (
|
---|
82 | bestModule === undefined ||
|
---|
83 | chunkGraph.getNumberOfModuleChunks(bestModule) >
|
---|
84 | chunkGraph.getNumberOfModuleChunks(module)
|
---|
85 | )
|
---|
86 | bestModule = module;
|
---|
87 | }
|
---|
88 | loopB: for (const chunkB of chunkGraph.getModuleChunksIterable(
|
---|
89 | /** @type {Module} */ (bestModule)
|
---|
90 | )) {
|
---|
91 | // as we iterate the same iterables twice
|
---|
92 | // skip if we find ourselves
|
---|
93 | if (chunkA === chunkB) continue;
|
---|
94 |
|
---|
95 | const chunkBModulesCount =
|
---|
96 | chunkGraph.getNumberOfChunkModules(chunkB);
|
---|
97 |
|
---|
98 | // ids for empty chunks are not included
|
---|
99 | if (chunkBModulesCount === 0) continue;
|
---|
100 |
|
---|
101 | // instead of swapping A and B just bail
|
---|
102 | // as we loop twice the current A will be B and B then A
|
---|
103 | if (chunkAModulesCount > chunkBModulesCount) continue;
|
---|
104 |
|
---|
105 | // is chunkA in chunkB?
|
---|
106 |
|
---|
107 | // we do a cheap check for the hash value
|
---|
108 | const chunkBHash =
|
---|
109 | /** @type {number} */
|
---|
110 | (chunkModulesHash.get(chunkB));
|
---|
111 | if ((chunkBHash & chunkAHash) !== chunkAHash) continue;
|
---|
112 |
|
---|
113 | // compare all modules
|
---|
114 | for (const m of chunkGraph.getChunkModulesIterable(chunkA)) {
|
---|
115 | if (!chunkGraph.isModuleInChunk(m, chunkB)) continue loopB;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** @type {ChunkId[]} */
|
---|
119 | (chunkB.ids).push(/** @type {ChunkId} */ (chunkA.id));
|
---|
120 | // https://github.com/webpack/webpack/issues/18837
|
---|
121 | /** @type {ChunkId[]} */
|
---|
122 | (chunkB.ids).sort(compareIds);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | );
|
---|
127 | });
|
---|
128 | }
|
---|
129 | }
|
---|
130 | module.exports = FlagIncludedChunksPlugin;
|
---|