| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const RequireEnsureDependenciesBlock = require("./RequireEnsureDependenciesBlock");
|
|---|
| 9 | const RequireEnsureDependency = require("./RequireEnsureDependency");
|
|---|
| 10 | const RequireEnsureItemDependency = require("./RequireEnsureItemDependency");
|
|---|
| 11 | const getFunctionExpression = require("./getFunctionExpression");
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {import("estree").Expression} Expression */
|
|---|
| 14 | /** @typedef {import("estree").SpreadElement} SpreadElement */
|
|---|
| 15 | /** @typedef {import("../AsyncDependenciesBlock").GroupOptions} GroupOptions */
|
|---|
| 16 | /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
|---|
| 17 | /** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
|
|---|
| 18 | /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
|---|
| 19 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 20 | /** @typedef {import("./getFunctionExpression").FunctionExpressionResult} FunctionExpressionResult */
|
|---|
| 21 |
|
|---|
| 22 | const PLUGIN_NAME = "RequireEnsureDependenciesBlockParserPlugin";
|
|---|
| 23 |
|
|---|
| 24 | module.exports = class RequireEnsureDependenciesBlockParserPlugin {
|
|---|
| 25 | /**
|
|---|
| 26 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 27 | * @param {JavascriptParser} parser the parser
|
|---|
| 28 | * @returns {void}
|
|---|
| 29 | */
|
|---|
| 30 | apply(parser) {
|
|---|
| 31 | parser.hooks.call.for("require.ensure").tap(PLUGIN_NAME, (expr) => {
|
|---|
| 32 | /** @type {string | GroupOptions | null} */
|
|---|
| 33 | let chunkName = null;
|
|---|
| 34 | /** @type {undefined | Expression | SpreadElement} */
|
|---|
| 35 | let errorExpressionArg;
|
|---|
| 36 | /** @type {undefined | FunctionExpressionResult} */
|
|---|
| 37 | let errorExpression;
|
|---|
| 38 | switch (expr.arguments.length) {
|
|---|
| 39 | case 4: {
|
|---|
| 40 | const chunkNameExpr = parser.evaluateExpression(expr.arguments[3]);
|
|---|
| 41 | if (!chunkNameExpr.isString()) return;
|
|---|
| 42 | chunkName =
|
|---|
| 43 | /** @type {string} */
|
|---|
| 44 | (chunkNameExpr.string);
|
|---|
| 45 | }
|
|---|
| 46 | // falls through
|
|---|
| 47 | case 3: {
|
|---|
| 48 | errorExpressionArg = expr.arguments[2];
|
|---|
| 49 | errorExpression = getFunctionExpression(errorExpressionArg);
|
|---|
| 50 |
|
|---|
| 51 | if (!errorExpression && !chunkName) {
|
|---|
| 52 | const chunkNameExpr = parser.evaluateExpression(expr.arguments[2]);
|
|---|
| 53 | if (!chunkNameExpr.isString()) return;
|
|---|
| 54 | chunkName =
|
|---|
| 55 | /** @type {string} */
|
|---|
| 56 | (chunkNameExpr.string);
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | // falls through
|
|---|
| 60 | case 2: {
|
|---|
| 61 | const dependenciesExpr = parser.evaluateExpression(expr.arguments[0]);
|
|---|
| 62 | const dependenciesItems = /** @type {BasicEvaluatedExpression[]} */ (
|
|---|
| 63 | dependenciesExpr.isArray()
|
|---|
| 64 | ? dependenciesExpr.items
|
|---|
| 65 | : [dependenciesExpr]
|
|---|
| 66 | );
|
|---|
| 67 | const successExpressionArg = expr.arguments[1];
|
|---|
| 68 | const successExpression = getFunctionExpression(successExpressionArg);
|
|---|
| 69 |
|
|---|
| 70 | if (successExpression) {
|
|---|
| 71 | parser.walkExpressions(successExpression.expressions);
|
|---|
| 72 | }
|
|---|
| 73 | if (errorExpression) {
|
|---|
| 74 | parser.walkExpressions(errorExpression.expressions);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | const depBlock = new RequireEnsureDependenciesBlock(
|
|---|
| 78 | chunkName,
|
|---|
| 79 | /** @type {DependencyLocation} */
|
|---|
| 80 | (expr.loc)
|
|---|
| 81 | );
|
|---|
| 82 | const errorCallbackExists =
|
|---|
| 83 | expr.arguments.length === 4 ||
|
|---|
| 84 | (!chunkName && expr.arguments.length === 3);
|
|---|
| 85 | const dep = new RequireEnsureDependency(
|
|---|
| 86 | /** @type {Range} */ (expr.range),
|
|---|
| 87 | /** @type {Range} */ (expr.arguments[1].range),
|
|---|
| 88 | errorCallbackExists &&
|
|---|
| 89 | /** @type {Range} */ (expr.arguments[2].range)
|
|---|
| 90 | );
|
|---|
| 91 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|---|
| 92 | depBlock.addDependency(dep);
|
|---|
| 93 | const old = parser.state.current;
|
|---|
| 94 | parser.state.current = /** @type {EXPECTED_ANY} */ (depBlock);
|
|---|
| 95 | try {
|
|---|
| 96 | let failed = false;
|
|---|
| 97 | parser.inFunctionScope(true, [], () => {
|
|---|
| 98 | for (const ee of dependenciesItems) {
|
|---|
| 99 | if (ee.isString()) {
|
|---|
| 100 | const ensureDependency = new RequireEnsureItemDependency(
|
|---|
| 101 | /** @type {string} */ (ee.string)
|
|---|
| 102 | );
|
|---|
| 103 | ensureDependency.loc =
|
|---|
| 104 | /** @type {DependencyLocation} */
|
|---|
| 105 | (expr.loc);
|
|---|
| 106 | depBlock.addDependency(ensureDependency);
|
|---|
| 107 | } else {
|
|---|
| 108 | failed = true;
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | });
|
|---|
| 112 | if (failed) {
|
|---|
| 113 | return;
|
|---|
| 114 | }
|
|---|
| 115 | if (successExpression) {
|
|---|
| 116 | if (successExpression.fn.body.type === "BlockStatement") {
|
|---|
| 117 | // Opt-out of Dead Control Flow detection for this block
|
|---|
| 118 | const oldTerminated = parser.scope.terminated;
|
|---|
| 119 | parser.walkStatement(successExpression.fn.body);
|
|---|
| 120 | parser.scope.terminated = oldTerminated;
|
|---|
| 121 | } else {
|
|---|
| 122 | parser.walkExpression(successExpression.fn.body);
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | old.addBlock(depBlock);
|
|---|
| 126 | } finally {
|
|---|
| 127 | parser.state.current = old;
|
|---|
| 128 | }
|
|---|
| 129 | if (!successExpression) {
|
|---|
| 130 | parser.walkExpression(successExpressionArg);
|
|---|
| 131 | }
|
|---|
| 132 | if (errorExpression) {
|
|---|
| 133 | if (errorExpression.fn.body.type === "BlockStatement") {
|
|---|
| 134 | parser.walkStatement(errorExpression.fn.body);
|
|---|
| 135 | } else {
|
|---|
| 136 | parser.walkExpression(errorExpression.fn.body);
|
|---|
| 137 | }
|
|---|
| 138 | } else if (errorExpressionArg) {
|
|---|
| 139 | parser.walkExpression(errorExpressionArg);
|
|---|
| 140 | }
|
|---|
| 141 | return true;
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | });
|
|---|
| 145 | }
|
|---|
| 146 | };
|
|---|