FlagDependencyUsagePlugin.js
2.33 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
class FlagDependencyUsagePlugin {
apply(compiler) {
compiler.plugin("compilation", compilation => {
compilation.plugin("optimize-modules-advanced", modules => {
modules.forEach(module => module.used = false);
const queue = [];
compilation.chunks.forEach(chunk => {
if(chunk.entryModule) {
processModule(chunk.entryModule, true);
}
});
while(queue.length) {
const queueItem = queue.pop();
processDependenciesBlock(queueItem[0], queueItem[1]);
}
function processModule(module, usedExports) {
module.used = true;
if(module.usedExports === true)
return;
else if(usedExports === true)
module.usedExports = true;
else if(Array.isArray(usedExports)) {
const old = module.usedExports ? module.usedExports.length : -1;
module.usedExports = addToSet(module.usedExports || [], usedExports);
if(module.usedExports.length === old)
return;
} else if(Array.isArray(module.usedExports))
return;
else
module.usedExports = false;
queue.push([module, module.usedExports]);
}
function processDependenciesBlock(depBlock, usedExports) {
depBlock.dependencies.forEach(dep => processDependency(dep));
depBlock.variables.forEach(variable => variable.dependencies.forEach(dep => processDependency(dep)));
depBlock.blocks.forEach(block => queue.push([block, usedExports]));
}
function processDependency(dep) {
const reference = dep.getReference && dep.getReference();
if(!reference) return;
const module = reference.module;
const importedNames = reference.importedNames;
const oldUsed = module.used;
const oldUsedExports = module.usedExports;
if(!oldUsed || (importedNames && (!oldUsedExports || !isSubset(oldUsedExports, importedNames)))) {
processModule(module, importedNames);
}
}
});
function addToSet(a, b) {
b.forEach(item => {
if(a.indexOf(item) < 0)
a.push(item);
});
return a;
}
function isSubset(biggerSet, subset) {
if(biggerSet === true) return true;
if(subset === true) return false;
return subset.every(item => biggerSet.indexOf(item) >= 0);
}
});
}
}
module.exports = FlagDependencyUsagePlugin;