1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 | const ConstDependency = require("./dependencies/ConstDependency");
|
7 | const NullFactory = require("./NullFactory");
|
8 | const ParserHelpers = require("./ParserHelpers");
|
9 |
|
10 | const getQuery = request => {
|
11 | const i = request.indexOf("?");
|
12 | return i !== -1 ? request.substr(i) : "";
|
13 | };
|
14 |
|
15 | const collectDeclaration = (declarations, pattern) => {
|
16 | const stack = [pattern];
|
17 | while (stack.length > 0) {
|
18 | const node = stack.pop();
|
19 | switch (node.type) {
|
20 | case "Identifier":
|
21 | declarations.add(node.name);
|
22 | break;
|
23 | case "ArrayPattern":
|
24 | for (const element of node.elements) {
|
25 | if (element) {
|
26 | stack.push(element);
|
27 | }
|
28 | }
|
29 | break;
|
30 | case "AssignmentPattern":
|
31 | stack.push(node.left);
|
32 | break;
|
33 | case "ObjectPattern":
|
34 | for (const property of node.properties) {
|
35 | stack.push(property.value);
|
36 | }
|
37 | break;
|
38 | case "RestElement":
|
39 | stack.push(node.argument);
|
40 | break;
|
41 | }
|
42 | }
|
43 | };
|
44 |
|
45 | const getHoistedDeclarations = (branch, includeFunctionDeclarations) => {
|
46 | const declarations = new Set();
|
47 | const stack = [branch];
|
48 | while (stack.length > 0) {
|
49 | const node = stack.pop();
|
50 |
|
51 | if (!node) continue;
|
52 | switch (node.type) {
|
53 |
|
54 |
|
55 | case "BlockStatement":
|
56 | for (const stmt of node.body) {
|
57 | stack.push(stmt);
|
58 | }
|
59 | break;
|
60 | case "IfStatement":
|
61 | stack.push(node.consequent);
|
62 | stack.push(node.alternate);
|
63 | break;
|
64 | case "ForStatement":
|
65 | stack.push(node.init);
|
66 | stack.push(node.body);
|
67 | break;
|
68 | case "ForInStatement":
|
69 | case "ForOfStatement":
|
70 | stack.push(node.left);
|
71 | stack.push(node.body);
|
72 | break;
|
73 | case "DoWhileStatement":
|
74 | case "WhileStatement":
|
75 | case "LabeledStatement":
|
76 | stack.push(node.body);
|
77 | break;
|
78 | case "SwitchStatement":
|
79 | for (const cs of node.cases) {
|
80 | for (const consequent of cs.consequent) {
|
81 | stack.push(consequent);
|
82 | }
|
83 | }
|
84 | break;
|
85 | case "TryStatement":
|
86 | stack.push(node.block);
|
87 | if (node.handler) {
|
88 | stack.push(node.handler.body);
|
89 | }
|
90 | stack.push(node.finalizer);
|
91 | break;
|
92 | case "FunctionDeclaration":
|
93 | if (includeFunctionDeclarations) {
|
94 | collectDeclaration(declarations, node.id);
|
95 | }
|
96 | break;
|
97 | case "VariableDeclaration":
|
98 | if (node.kind === "var") {
|
99 | for (const decl of node.declarations) {
|
100 | collectDeclaration(declarations, decl.id);
|
101 | }
|
102 | }
|
103 | break;
|
104 | }
|
105 | }
|
106 | return Array.from(declarations);
|
107 | };
|
108 |
|
109 | class ConstPlugin {
|
110 | apply(compiler) {
|
111 | compiler.hooks.compilation.tap(
|
112 | "ConstPlugin",
|
113 | (compilation, { normalModuleFactory }) => {
|
114 | compilation.dependencyFactories.set(ConstDependency, new NullFactory());
|
115 | compilation.dependencyTemplates.set(
|
116 | ConstDependency,
|
117 | new ConstDependency.Template()
|
118 | );
|
119 |
|
120 | const handler = parser => {
|
121 | parser.hooks.statementIf.tap("ConstPlugin", statement => {
|
122 | const param = parser.evaluateExpression(statement.test);
|
123 | const bool = param.asBool();
|
124 | if (typeof bool === "boolean") {
|
125 | if (statement.test.type !== "Literal") {
|
126 | const dep = new ConstDependency(`${bool}`, param.range);
|
127 | dep.loc = statement.loc;
|
128 | parser.state.current.addDependency(dep);
|
129 | }
|
130 | const branchToRemove = bool
|
131 | ? statement.alternate
|
132 | : statement.consequent;
|
133 | if (branchToRemove) {
|
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 | let declarations;
|
165 | if (parser.scope.isStrict) {
|
166 |
|
167 |
|
168 | declarations = getHoistedDeclarations(branchToRemove, false);
|
169 | } else {
|
170 |
|
171 | declarations = getHoistedDeclarations(branchToRemove, true);
|
172 | }
|
173 | let replacement;
|
174 | if (declarations.length > 0) {
|
175 | replacement = `{ var ${declarations.join(", ")}; }`;
|
176 | } else {
|
177 | replacement = "{}";
|
178 | }
|
179 | const dep = new ConstDependency(
|
180 | replacement,
|
181 | branchToRemove.range
|
182 | );
|
183 | dep.loc = branchToRemove.loc;
|
184 | parser.state.current.addDependency(dep);
|
185 | }
|
186 | return bool;
|
187 | }
|
188 | });
|
189 | parser.hooks.expressionConditionalOperator.tap(
|
190 | "ConstPlugin",
|
191 | expression => {
|
192 | const param = parser.evaluateExpression(expression.test);
|
193 | const bool = param.asBool();
|
194 | if (typeof bool === "boolean") {
|
195 | if (expression.test.type !== "Literal") {
|
196 | const dep = new ConstDependency(` ${bool}`, param.range);
|
197 | dep.loc = expression.loc;
|
198 | parser.state.current.addDependency(dep);
|
199 | }
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | const branchToRemove = bool
|
212 | ? expression.alternate
|
213 | : expression.consequent;
|
214 | const dep = new ConstDependency(
|
215 | "undefined",
|
216 | branchToRemove.range
|
217 | );
|
218 | dep.loc = branchToRemove.loc;
|
219 | parser.state.current.addDependency(dep);
|
220 | return bool;
|
221 | }
|
222 | }
|
223 | );
|
224 | parser.hooks.expressionLogicalOperator.tap(
|
225 | "ConstPlugin",
|
226 | expression => {
|
227 | if (
|
228 | expression.operator === "&&" ||
|
229 | expression.operator === "||"
|
230 | ) {
|
231 | const param = parser.evaluateExpression(expression.left);
|
232 | const bool = param.asBool();
|
233 | if (typeof bool === "boolean") {
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 | const keepRight =
|
278 | (expression.operator === "&&" && bool) ||
|
279 | (expression.operator === "||" && !bool);
|
280 |
|
281 | if (param.isBoolean() || keepRight) {
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 | const dep = new ConstDependency(` ${bool}`, param.range);
|
291 | dep.loc = expression.loc;
|
292 | parser.state.current.addDependency(dep);
|
293 | } else {
|
294 | parser.walkExpression(expression.left);
|
295 | }
|
296 | if (!keepRight) {
|
297 | const dep = new ConstDependency(
|
298 | "false",
|
299 | expression.right.range
|
300 | );
|
301 | dep.loc = expression.loc;
|
302 | parser.state.current.addDependency(dep);
|
303 | }
|
304 | return keepRight;
|
305 | }
|
306 | }
|
307 | }
|
308 | );
|
309 | parser.hooks.evaluateIdentifier
|
310 | .for("__resourceQuery")
|
311 | .tap("ConstPlugin", expr => {
|
312 | if (!parser.state.module) return;
|
313 | return ParserHelpers.evaluateToString(
|
314 | getQuery(parser.state.module.resource)
|
315 | )(expr);
|
316 | });
|
317 | parser.hooks.expression
|
318 | .for("__resourceQuery")
|
319 | .tap("ConstPlugin", () => {
|
320 | if (!parser.state.module) return;
|
321 | parser.state.current.addVariable(
|
322 | "__resourceQuery",
|
323 | JSON.stringify(getQuery(parser.state.module.resource))
|
324 | );
|
325 | return true;
|
326 | });
|
327 | };
|
328 |
|
329 | normalModuleFactory.hooks.parser
|
330 | .for("javascript/auto")
|
331 | .tap("ConstPlugin", handler);
|
332 | normalModuleFactory.hooks.parser
|
333 | .for("javascript/dynamic")
|
334 | .tap("ConstPlugin", handler);
|
335 | normalModuleFactory.hooks.parser
|
336 | .for("javascript/esm")
|
337 | .tap("ConstPlugin", handler);
|
338 | }
|
339 | );
|
340 | }
|
341 | }
|
342 |
|
343 | module.exports = ConstPlugin;
|