EnsureChunkConditionsPlugin.js
1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
class EnsureChunkConditionsPlugin {
apply(compiler) {
compiler.plugin("compilation", (compilation) => {
const triesMap = new Map();
compilation.plugin(["optimize-chunks-basic", "optimize-extracted-chunks-basic"], (chunks) => {
let changed = false;
chunks.forEach((chunk) => {
chunk.forEachModule((module) => {
if(!module.chunkCondition) return;
if(!module.chunkCondition(chunk)) {
let usedChunks = triesMap.get(module);
if(!usedChunks) triesMap.set(module, usedChunks = new Set());
usedChunks.add(chunk);
const newChunks = [];
chunk.parents.forEach((parent) => {
if(!usedChunks.has(parent)) {
parent.addModule(module);
module.addChunk(parent);
newChunks.push(parent);
}
});
module.rewriteChunkInReasons(chunk, newChunks);
chunk.removeModule(module);
changed = true;
}
});
});
if(changed) return true;
});
});
}
}
module.exports = EnsureChunkConditionsPlugin;