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 { STAGE_BASIC } = require("../OptimizationStages");
|
---|
9 | const Queue = require("../util/Queue");
|
---|
10 | const { intersect } = require("../util/SetHelpers");
|
---|
11 |
|
---|
12 | /** @typedef {import("../Compiler")} Compiler */
|
---|
13 |
|
---|
14 | class RemoveParentModulesPlugin {
|
---|
15 | /**
|
---|
16 | * @param {Compiler} compiler the compiler
|
---|
17 | * @returns {void}
|
---|
18 | */
|
---|
19 | apply(compiler) {
|
---|
20 | compiler.hooks.compilation.tap("RemoveParentModulesPlugin", compilation => {
|
---|
21 | const handler = (chunks, chunkGroups) => {
|
---|
22 | const chunkGraph = compilation.chunkGraph;
|
---|
23 | const queue = new Queue();
|
---|
24 | const availableModulesMap = new WeakMap();
|
---|
25 |
|
---|
26 | for (const chunkGroup of compilation.entrypoints.values()) {
|
---|
27 | // initialize available modules for chunks without parents
|
---|
28 | availableModulesMap.set(chunkGroup, new Set());
|
---|
29 | for (const child of chunkGroup.childrenIterable) {
|
---|
30 | queue.enqueue(child);
|
---|
31 | }
|
---|
32 | }
|
---|
33 | for (const chunkGroup of compilation.asyncEntrypoints) {
|
---|
34 | // initialize available modules for chunks without parents
|
---|
35 | availableModulesMap.set(chunkGroup, new Set());
|
---|
36 | for (const child of chunkGroup.childrenIterable) {
|
---|
37 | queue.enqueue(child);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | while (queue.length > 0) {
|
---|
42 | const chunkGroup = queue.dequeue();
|
---|
43 | let availableModules = availableModulesMap.get(chunkGroup);
|
---|
44 | let changed = false;
|
---|
45 | for (const parent of chunkGroup.parentsIterable) {
|
---|
46 | const availableModulesInParent = availableModulesMap.get(parent);
|
---|
47 | if (availableModulesInParent !== undefined) {
|
---|
48 | // If we know the available modules in parent: process these
|
---|
49 | if (availableModules === undefined) {
|
---|
50 | // if we have not own info yet: create new entry
|
---|
51 | availableModules = new Set(availableModulesInParent);
|
---|
52 | for (const chunk of parent.chunks) {
|
---|
53 | for (const m of chunkGraph.getChunkModulesIterable(chunk)) {
|
---|
54 | availableModules.add(m);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | availableModulesMap.set(chunkGroup, availableModules);
|
---|
58 | changed = true;
|
---|
59 | } else {
|
---|
60 | for (const m of availableModules) {
|
---|
61 | if (
|
---|
62 | !chunkGraph.isModuleInChunkGroup(m, parent) &&
|
---|
63 | !availableModulesInParent.has(m)
|
---|
64 | ) {
|
---|
65 | availableModules.delete(m);
|
---|
66 | changed = true;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 | if (changed) {
|
---|
73 | // if something changed: enqueue our children
|
---|
74 | for (const child of chunkGroup.childrenIterable) {
|
---|
75 | queue.enqueue(child);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | // now we have available modules for every chunk
|
---|
81 | for (const chunk of chunks) {
|
---|
82 | const availableModulesSets = Array.from(
|
---|
83 | chunk.groupsIterable,
|
---|
84 | chunkGroup => availableModulesMap.get(chunkGroup)
|
---|
85 | );
|
---|
86 | if (availableModulesSets.some(s => s === undefined)) continue; // No info about this chunk group
|
---|
87 | const availableModules =
|
---|
88 | availableModulesSets.length === 1
|
---|
89 | ? availableModulesSets[0]
|
---|
90 | : intersect(availableModulesSets);
|
---|
91 | const numberOfModules = chunkGraph.getNumberOfChunkModules(chunk);
|
---|
92 | const toRemove = new Set();
|
---|
93 | if (numberOfModules < availableModules.size) {
|
---|
94 | for (const m of chunkGraph.getChunkModulesIterable(chunk)) {
|
---|
95 | if (availableModules.has(m)) {
|
---|
96 | toRemove.add(m);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | } else {
|
---|
100 | for (const m of availableModules) {
|
---|
101 | if (chunkGraph.isModuleInChunk(m, chunk)) {
|
---|
102 | toRemove.add(m);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 | for (const module of toRemove) {
|
---|
107 | chunkGraph.disconnectChunkAndModule(chunk, module);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | };
|
---|
111 | compilation.hooks.optimizeChunks.tap(
|
---|
112 | {
|
---|
113 | name: "RemoveParentModulesPlugin",
|
---|
114 | stage: STAGE_BASIC
|
---|
115 | },
|
---|
116 | handler
|
---|
117 | );
|
---|
118 | });
|
---|
119 | }
|
---|
120 | }
|
---|
121 | module.exports = RemoveParentModulesPlugin;
|
---|