/
SideEffectsFlagPlugin.js
169 lines (160 loc) · 5.14 KB
/
SideEffectsFlagPlugin.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const mm = require("micromatch");
const HarmonyExportImportedSpecifierDependency = require("../dependencies/HarmonyExportImportedSpecifierDependency");
const HarmonyImportSideEffectDependency = require("../dependencies/HarmonyImportSideEffectDependency");
const HarmonyImportSpecifierDependency = require("../dependencies/HarmonyImportSpecifierDependency");
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Dependency")} Dependency */
/**
* @typedef {Object} ExportInModule
* @property {Module} module the module
* @property {string} exportName the name of the export
*/
class SideEffectsFlagPlugin {
apply(compiler) {
compiler.hooks.normalModuleFactory.tap("SideEffectsFlagPlugin", nmf => {
nmf.hooks.module.tap("SideEffectsFlagPlugin", (module, data) => {
const resolveData = data.resourceResolveData;
if (
resolveData &&
resolveData.descriptionFileData &&
resolveData.relativePath
) {
const sideEffects = resolveData.descriptionFileData.sideEffects;
const hasSideEffects = SideEffectsFlagPlugin.moduleHasSideEffects(
resolveData.relativePath,
sideEffects
);
if (!hasSideEffects) {
module.factoryMeta.sideEffectFree = true;
}
}
return module;
});
nmf.hooks.module.tap("SideEffectsFlagPlugin", (module, data) => {
if (data.settings.sideEffects === false) {
module.factoryMeta.sideEffectFree = true;
} else if (data.settings.sideEffects === true) {
module.factoryMeta.sideEffectFree = false;
}
});
});
compiler.hooks.compilation.tap("SideEffectsFlagPlugin", compilation => {
compilation.hooks.optimizeDependencies.tap(
"SideEffectsFlagPlugin",
modules => {
/** @type {Map<Module, Map<string, ExportInModule>>} */
const reexportMaps = new Map();
// Capture reexports of sideEffectFree modules
for (const module of modules) {
/** @type {Dependency[]} */
const removeDependencies = [];
for (const dep of module.dependencies) {
if (dep instanceof HarmonyImportSideEffectDependency) {
if (dep.module && dep.module.factoryMeta.sideEffectFree) {
removeDependencies.push(dep);
}
} else if (
dep instanceof HarmonyExportImportedSpecifierDependency
) {
if (module.factoryMeta.sideEffectFree) {
const mode = dep.getMode(true);
if (mode.type === "safe-reexport") {
let map = reexportMaps.get(module);
if (!map) {
reexportMaps.set(module, (map = new Map()));
}
for (const pair of mode.map) {
map.set(pair[0], {
module: mode.module,
exportName: pair[1]
});
}
}
}
}
}
}
// Flatten reexports
for (const map of reexportMaps.values()) {
for (const pair of map) {
let mapping = pair[1];
while (mapping) {
const innerMap = reexportMaps.get(mapping.module);
if (!innerMap) break;
const newMapping = innerMap.get(mapping.exportName);
if (newMapping) {
map.set(pair[0], newMapping);
}
mapping = newMapping;
}
}
}
// Update imports along the reexports from sideEffectFree modules
for (const pair of reexportMaps) {
const module = pair[0];
const map = pair[1];
let newReasons = undefined;
for (let i = 0; i < module.reasons.length; i++) {
const reason = module.reasons[i];
const dep = reason.dependency;
if (
dep instanceof HarmonyExportImportedSpecifierDependency ||
(dep instanceof HarmonyImportSpecifierDependency &&
!dep.namespaceObjectAsContext)
) {
const mapping = map.get(dep._id);
if (mapping) {
dep.redirectedModule = mapping.module;
dep.redirectedId = mapping.exportName;
mapping.module.addReason(
reason.module,
dep,
reason.explanation
? reason.explanation +
" (skipped side-effect-free modules)"
: "(skipped side-effect-free modules)"
);
// removing the currect reason, by not adding it to the newReasons array
// lazily create the newReasons array
if (newReasons === undefined) {
newReasons = i === 0 ? [] : module.reasons.slice(0, i);
}
continue;
}
}
if (newReasons !== undefined) newReasons.push(reason);
}
if (newReasons !== undefined) {
module.reasons = newReasons;
}
}
}
);
});
}
static moduleHasSideEffects(moduleName, flagValue) {
switch (typeof flagValue) {
case "undefined":
return true;
case "boolean":
return flagValue;
case "string":
if (process.platform === "win32") {
flagValue = flagValue.replace(/\\/g, "/");
}
return mm.isMatch(moduleName, flagValue, {
matchBase: true
});
case "object":
return flagValue.some(glob =>
SideEffectsFlagPlugin.moduleHasSideEffects(moduleName, glob)
);
}
}
}
module.exports = SideEffectsFlagPlugin;