1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const CachedConstDependency = require("./dependencies/CachedConstDependency");
|
9 | const ConstDependency = require("./dependencies/ConstDependency");
|
10 | const { evaluateToString } = require("./javascript/JavascriptParserHelpers");
|
11 | const { parseResource } = require("./util/identifier");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const collectDeclaration = (declarations, pattern) => {
|
18 | const stack = [pattern];
|
19 | while (stack.length > 0) {
|
20 | const node = stack.pop();
|
21 | switch (node.type) {
|
22 | case "Identifier":
|
23 | declarations.add(node.name);
|
24 | break;
|
25 | case "ArrayPattern":
|
26 | for (const element of node.elements) {
|
27 | if (element) {
|
28 | stack.push(element);
|
29 | }
|
30 | }
|
31 | break;
|
32 | case "AssignmentPattern":
|
33 | stack.push(node.left);
|
34 | break;
|
35 | case "ObjectPattern":
|
36 | for (const property of node.properties) {
|
37 | stack.push(property.value);
|
38 | }
|
39 | break;
|
40 | case "RestElement":
|
41 | stack.push(node.argument);
|
42 | break;
|
43 | }
|
44 | }
|
45 | };
|
46 |
|
47 | const getHoistedDeclarations = (branch, includeFunctionDeclarations) => {
|
48 | const declarations = new Set();
|
49 | const stack = [branch];
|
50 | while (stack.length > 0) {
|
51 | const node = stack.pop();
|
52 |
|
53 | if (!node) continue;
|
54 | switch (node.type) {
|
55 |
|
56 |
|
57 | case "BlockStatement":
|
58 | for (const stmt of node.body) {
|
59 | stack.push(stmt);
|
60 | }
|
61 | break;
|
62 | case "IfStatement":
|
63 | stack.push(node.consequent);
|
64 | stack.push(node.alternate);
|
65 | break;
|
66 | case "ForStatement":
|
67 | stack.push(node.init);
|
68 | stack.push(node.body);
|
69 | break;
|
70 | case "ForInStatement":
|
71 | case "ForOfStatement":
|
72 | stack.push(node.left);
|
73 | stack.push(node.body);
|
74 | break;
|
75 | case "DoWhileStatement":
|
76 | case "WhileStatement":
|
77 | case "LabeledStatement":
|
78 | stack.push(node.body);
|
79 | break;
|
80 | case "SwitchStatement":
|
81 | for (const cs of node.cases) {
|
82 | for (const consequent of cs.consequent) {
|
83 | stack.push(consequent);
|
84 | }
|
85 | }
|
86 | break;
|
87 | case "TryStatement":
|
88 | stack.push(node.block);
|
89 | if (node.handler) {
|
90 | stack.push(node.handler.body);
|
91 | }
|
92 | stack.push(node.finalizer);
|
93 | break;
|
94 | case "FunctionDeclaration":
|
95 | if (includeFunctionDeclarations) {
|
96 | collectDeclaration(declarations, node.id);
|
97 | }
|
98 | break;
|
99 | case "VariableDeclaration":
|
100 | if (node.kind === "var") {
|
101 | for (const decl of node.declarations) {
|
102 | collectDeclaration(declarations, decl.id);
|
103 | }
|
104 | }
|
105 | break;
|
106 | }
|
107 | }
|
108 | return Array.from(declarations);
|
109 | };
|
110 |
|
111 | class ConstPlugin {
|
112 | |
113 |
|
114 |
|
115 |
|
116 |
|
117 | apply(compiler) {
|
118 | const cachedParseResource = parseResource.bindCache(compiler.root);
|
119 | compiler.hooks.compilation.tap(
|
120 | "ConstPlugin",
|
121 | (compilation, { normalModuleFactory }) => {
|
122 | compilation.dependencyTemplates.set(
|
123 | ConstDependency,
|
124 | new ConstDependency.Template()
|
125 | );
|
126 |
|
127 | compilation.dependencyTemplates.set(
|
128 | CachedConstDependency,
|
129 | new CachedConstDependency.Template()
|
130 | );
|
131 |
|
132 | const handler = parser => {
|
133 | parser.hooks.statementIf.tap("ConstPlugin", statement => {
|
134 | if (parser.scope.isAsmJs) return;
|
135 | const param = parser.evaluateExpression(statement.test);
|
136 | const bool = param.asBool();
|
137 | if (typeof bool === "boolean") {
|
138 | if (!param.couldHaveSideEffects()) {
|
139 | const dep = new ConstDependency(`${bool}`, param.range);
|
140 | dep.loc = statement.loc;
|
141 | parser.state.module.addPresentationalDependency(dep);
|
142 | } else {
|
143 | parser.walkExpression(statement.test);
|
144 | }
|
145 | const branchToRemove = bool
|
146 | ? statement.alternate
|
147 | : statement.consequent;
|
148 | if (branchToRemove) {
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | let declarations;
|
180 | if (parser.scope.isStrict) {
|
181 |
|
182 |
|
183 | declarations = getHoistedDeclarations(branchToRemove, false);
|
184 | } else {
|
185 |
|
186 | declarations = getHoistedDeclarations(branchToRemove, true);
|
187 | }
|
188 | let replacement;
|
189 | if (declarations.length > 0) {
|
190 | replacement = `{ var ${declarations.join(", ")}; }`;
|
191 | } else {
|
192 | replacement = "{}";
|
193 | }
|
194 | const dep = new ConstDependency(
|
195 | replacement,
|
196 | branchToRemove.range
|
197 | );
|
198 | dep.loc = branchToRemove.loc;
|
199 | parser.state.module.addPresentationalDependency(dep);
|
200 | }
|
201 | return bool;
|
202 | }
|
203 | });
|
204 | parser.hooks.expressionConditionalOperator.tap(
|
205 | "ConstPlugin",
|
206 | expression => {
|
207 | if (parser.scope.isAsmJs) return;
|
208 | const param = parser.evaluateExpression(expression.test);
|
209 | const bool = param.asBool();
|
210 | if (typeof bool === "boolean") {
|
211 | if (!param.couldHaveSideEffects()) {
|
212 | const dep = new ConstDependency(` ${bool}`, param.range);
|
213 | dep.loc = expression.loc;
|
214 | parser.state.module.addPresentationalDependency(dep);
|
215 | } else {
|
216 | parser.walkExpression(expression.test);
|
217 | }
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | const branchToRemove = bool
|
230 | ? expression.alternate
|
231 | : expression.consequent;
|
232 | const dep = new ConstDependency("0", branchToRemove.range);
|
233 | dep.loc = branchToRemove.loc;
|
234 | parser.state.module.addPresentationalDependency(dep);
|
235 | return bool;
|
236 | }
|
237 | }
|
238 | );
|
239 | parser.hooks.expressionLogicalOperator.tap(
|
240 | "ConstPlugin",
|
241 | expression => {
|
242 | if (parser.scope.isAsmJs) return;
|
243 | if (
|
244 | expression.operator === "&&" ||
|
245 | expression.operator === "||"
|
246 | ) {
|
247 | const param = parser.evaluateExpression(expression.left);
|
248 | const bool = param.asBool();
|
249 | if (typeof bool === "boolean") {
|
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 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | const keepRight =
|
294 | (expression.operator === "&&" && bool) ||
|
295 | (expression.operator === "||" && !bool);
|
296 |
|
297 | if (
|
298 | !param.couldHaveSideEffects() &&
|
299 | (param.isBoolean() || keepRight)
|
300 | ) {
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 | const dep = new ConstDependency(` ${bool}`, param.range);
|
310 | dep.loc = expression.loc;
|
311 | parser.state.module.addPresentationalDependency(dep);
|
312 | } else {
|
313 | parser.walkExpression(expression.left);
|
314 | }
|
315 | if (!keepRight) {
|
316 | const dep = new ConstDependency(
|
317 | "0",
|
318 | expression.right.range
|
319 | );
|
320 | dep.loc = expression.loc;
|
321 | parser.state.module.addPresentationalDependency(dep);
|
322 | }
|
323 | return keepRight;
|
324 | }
|
325 | } else if (expression.operator === "??") {
|
326 | const param = parser.evaluateExpression(expression.left);
|
327 | const keepRight = param && param.asNullish();
|
328 | if (typeof keepRight === "boolean") {
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 | if (!param.couldHaveSideEffects() && keepRight) {
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 | const dep = new ConstDependency(" null", param.range);
|
360 | dep.loc = expression.loc;
|
361 | parser.state.module.addPresentationalDependency(dep);
|
362 | } else {
|
363 | const dep = new ConstDependency(
|
364 | "0",
|
365 | expression.right.range
|
366 | );
|
367 | dep.loc = expression.loc;
|
368 | parser.state.module.addPresentationalDependency(dep);
|
369 | parser.walkExpression(expression.left);
|
370 | }
|
371 |
|
372 | return keepRight;
|
373 | }
|
374 | }
|
375 | }
|
376 | );
|
377 | parser.hooks.optionalChaining.tap("ConstPlugin", expr => {
|
378 |
|
379 | const optionalExpressionsStack = [];
|
380 |
|
381 | let next = expr.expression;
|
382 |
|
383 | while (
|
384 | next.type === "MemberExpression" ||
|
385 | next.type === "CallExpression"
|
386 | ) {
|
387 | if (next.type === "MemberExpression") {
|
388 | if (next.optional) {
|
389 |
|
390 | optionalExpressionsStack.push(
|
391 | (next.object)
|
392 | );
|
393 | }
|
394 | next = next.object;
|
395 | } else {
|
396 | if (next.optional) {
|
397 |
|
398 | optionalExpressionsStack.push(
|
399 | (next.callee)
|
400 | );
|
401 | }
|
402 | next = next.callee;
|
403 | }
|
404 | }
|
405 |
|
406 | while (optionalExpressionsStack.length) {
|
407 | const expression = optionalExpressionsStack.pop();
|
408 | const evaluated = parser.evaluateExpression(expression);
|
409 |
|
410 | if (evaluated && evaluated.asNullish()) {
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 | const dep = new ConstDependency(" undefined", expr.range);
|
424 | dep.loc = expr.loc;
|
425 | parser.state.module.addPresentationalDependency(dep);
|
426 | return true;
|
427 | }
|
428 | }
|
429 | });
|
430 | parser.hooks.evaluateIdentifier
|
431 | .for("__resourceQuery")
|
432 | .tap("ConstPlugin", expr => {
|
433 | if (parser.scope.isAsmJs) return;
|
434 | if (!parser.state.module) return;
|
435 | return evaluateToString(
|
436 | cachedParseResource(parser.state.module.resource).query
|
437 | )(expr);
|
438 | });
|
439 | parser.hooks.expression
|
440 | .for("__resourceQuery")
|
441 | .tap("ConstPlugin", expr => {
|
442 | if (parser.scope.isAsmJs) return;
|
443 | if (!parser.state.module) return;
|
444 | const dep = new CachedConstDependency(
|
445 | JSON.stringify(
|
446 | cachedParseResource(parser.state.module.resource).query
|
447 | ),
|
448 | expr.range,
|
449 | "__resourceQuery"
|
450 | );
|
451 | dep.loc = expr.loc;
|
452 | parser.state.module.addPresentationalDependency(dep);
|
453 | return true;
|
454 | });
|
455 |
|
456 | parser.hooks.evaluateIdentifier
|
457 | .for("__resourceFragment")
|
458 | .tap("ConstPlugin", expr => {
|
459 | if (parser.scope.isAsmJs) return;
|
460 | if (!parser.state.module) return;
|
461 | return evaluateToString(
|
462 | cachedParseResource(parser.state.module.resource).fragment
|
463 | )(expr);
|
464 | });
|
465 | parser.hooks.expression
|
466 | .for("__resourceFragment")
|
467 | .tap("ConstPlugin", expr => {
|
468 | if (parser.scope.isAsmJs) return;
|
469 | if (!parser.state.module) return;
|
470 | const dep = new CachedConstDependency(
|
471 | JSON.stringify(
|
472 | cachedParseResource(parser.state.module.resource).fragment
|
473 | ),
|
474 | expr.range,
|
475 | "__resourceFragment"
|
476 | );
|
477 | dep.loc = expr.loc;
|
478 | parser.state.module.addPresentationalDependency(dep);
|
479 | return true;
|
480 | });
|
481 | };
|
482 |
|
483 | normalModuleFactory.hooks.parser
|
484 | .for("javascript/auto")
|
485 | .tap("ConstPlugin", handler);
|
486 | normalModuleFactory.hooks.parser
|
487 | .for("javascript/dynamic")
|
488 | .tap("ConstPlugin", handler);
|
489 | normalModuleFactory.hooks.parser
|
490 | .for("javascript/esm")
|
491 | .tap("ConstPlugin", handler);
|
492 | }
|
493 | );
|
494 | }
|
495 | }
|
496 |
|
497 | module.exports = ConstPlugin;
|