source: imaps-frontend/node_modules/webpack/lib/optimize/RemoveParentModulesPlugin.js

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

F4 Finalna Verzija

  • Property mode set to 100644
File size: 6.4 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { STAGE_BASIC } = require("../OptimizationStages");
9
10/** @typedef {import("../Chunk")} Chunk */
11/** @typedef {import("../ChunkGroup")} ChunkGroup */
12/** @typedef {import("../Compiler")} Compiler */
13/** @typedef {import("../Module")} Module */
14
15/**
16 * Intersects multiple masks represented as bigints
17 * @param {bigint[]} masks The module masks to intersect
18 * @returns {bigint} The intersection of all masks
19 */
20function intersectMasks(masks) {
21 let result = masks[0];
22 for (let i = masks.length - 1; i >= 1; i--) {
23 result &= masks[i];
24 }
25 return result;
26}
27
28const ZERO_BIGINT = BigInt(0);
29const ONE_BIGINT = BigInt(1);
30const THIRTY_TWO_BIGINT = BigInt(32);
31
32/**
33 * Parses the module mask and returns the modules represented by it
34 * @param {bigint} mask the module mask
35 * @param {Module[]} ordinalModules the modules in the order they were added to the mask (LSB is index 0)
36 * @returns {Generator<Module>} the modules represented by the mask
37 */
38function* getModulesFromMask(mask, ordinalModules) {
39 let offset = 31;
40 while (mask !== ZERO_BIGINT) {
41 // Consider the last 32 bits, since that's what Math.clz32 can handle
42 let last32 = Number(BigInt.asUintN(32, mask));
43 while (last32 > 0) {
44 const last = Math.clz32(last32);
45 // The number of trailing zeros is the number trimmed off the input mask + 31 - the number of leading zeros
46 // The 32 is baked into the initial value of offset
47 const moduleIndex = offset - last;
48 // The number of trailing zeros is the index into the array generated by getOrCreateModuleMask
49 const module = ordinalModules[moduleIndex];
50 yield module;
51 // Remove the matched module from the mask
52 // Since we can only count leading zeros, not trailing, we can't just downshift the mask
53 last32 &= ~(1 << (31 - last));
54 }
55
56 // Remove the processed chunk from the mask
57 mask >>= THIRTY_TWO_BIGINT;
58 offset += 32;
59 }
60}
61
62class RemoveParentModulesPlugin {
63 /**
64 * @param {Compiler} compiler the compiler
65 * @returns {void}
66 */
67 apply(compiler) {
68 compiler.hooks.compilation.tap("RemoveParentModulesPlugin", compilation => {
69 /**
70 * @param {Iterable<Chunk>} chunks the chunks
71 * @param {ChunkGroup[]} chunkGroups the chunk groups
72 */
73 const handler = (chunks, chunkGroups) => {
74 const chunkGraph = compilation.chunkGraph;
75 const queue = new Set();
76 const availableModulesMap = new WeakMap();
77
78 let nextModuleMask = ONE_BIGINT;
79 const maskByModule = new WeakMap();
80 /** @type {Module[]} */
81 const ordinalModules = [];
82
83 /**
84 * Gets or creates a unique mask for a module
85 * @param {Module} mod the module to get the mask for
86 * @returns {bigint} the module mask to uniquely identify the module
87 */
88 const getOrCreateModuleMask = mod => {
89 let id = maskByModule.get(mod);
90 if (id === undefined) {
91 id = nextModuleMask;
92 ordinalModules.push(mod);
93 maskByModule.set(mod, id);
94 nextModuleMask <<= ONE_BIGINT;
95 }
96 return id;
97 };
98
99 // Initialize masks by chunk and by chunk group for quicker comparisons
100 const chunkMasks = new WeakMap();
101 for (const chunk of chunks) {
102 let mask = ZERO_BIGINT;
103 for (const m of chunkGraph.getChunkModulesIterable(chunk)) {
104 const id = getOrCreateModuleMask(m);
105 mask |= id;
106 }
107 chunkMasks.set(chunk, mask);
108 }
109
110 const chunkGroupMasks = new WeakMap();
111 for (const chunkGroup of chunkGroups) {
112 let mask = ZERO_BIGINT;
113 for (const chunk of chunkGroup.chunks) {
114 const chunkMask = chunkMasks.get(chunk);
115 if (chunkMask !== undefined) {
116 mask |= chunkMask;
117 }
118 }
119 chunkGroupMasks.set(chunkGroup, mask);
120 }
121
122 for (const chunkGroup of compilation.entrypoints.values()) {
123 // initialize available modules for chunks without parents
124 availableModulesMap.set(chunkGroup, ZERO_BIGINT);
125 for (const child of chunkGroup.childrenIterable) {
126 queue.add(child);
127 }
128 }
129 for (const chunkGroup of compilation.asyncEntrypoints) {
130 // initialize available modules for chunks without parents
131 availableModulesMap.set(chunkGroup, ZERO_BIGINT);
132 for (const child of chunkGroup.childrenIterable) {
133 queue.add(child);
134 }
135 }
136
137 for (const chunkGroup of queue) {
138 let availableModulesMask = availableModulesMap.get(chunkGroup);
139 let changed = false;
140 for (const parent of chunkGroup.parentsIterable) {
141 const availableModulesInParent = availableModulesMap.get(parent);
142 if (availableModulesInParent !== undefined) {
143 const parentMask =
144 availableModulesInParent | chunkGroupMasks.get(parent);
145 // If we know the available modules in parent: process these
146 if (availableModulesMask === undefined) {
147 // if we have not own info yet: create new entry
148 availableModulesMask = parentMask;
149 changed = true;
150 } else {
151 const newMask = availableModulesMask & parentMask;
152 if (newMask !== availableModulesMask) {
153 changed = true;
154 availableModulesMask = newMask;
155 }
156 }
157 }
158 }
159
160 if (changed) {
161 availableModulesMap.set(chunkGroup, availableModulesMask);
162 // if something changed: enqueue our children
163 for (const child of chunkGroup.childrenIterable) {
164 // Push the child to the end of the queue
165 queue.delete(child);
166 queue.add(child);
167 }
168 }
169 }
170
171 // now we have available modules for every chunk
172 for (const chunk of chunks) {
173 const chunkMask = chunkMasks.get(chunk);
174 if (chunkMask === undefined) continue; // No info about this chunk
175
176 const availableModulesSets = Array.from(
177 chunk.groupsIterable,
178 chunkGroup => availableModulesMap.get(chunkGroup)
179 );
180 if (availableModulesSets.includes(undefined)) continue; // No info about this chunk group
181
182 const availableModulesMask = intersectMasks(availableModulesSets);
183 const toRemoveMask = chunkMask & availableModulesMask;
184 if (toRemoveMask !== ZERO_BIGINT) {
185 for (const module of getModulesFromMask(
186 toRemoveMask,
187 ordinalModules
188 )) {
189 chunkGraph.disconnectChunkAndModule(chunk, module);
190 }
191 }
192 }
193 };
194 compilation.hooks.optimizeChunks.tap(
195 {
196 name: "RemoveParentModulesPlugin",
197 stage: STAGE_BASIC
198 },
199 handler
200 );
201 });
202 }
203}
204module.exports = RemoveParentModulesPlugin;
Note: See TracBrowser for help on using the repository browser.