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("../ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */
|
---|
14 | /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
---|
15 | /** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
|
---|
16 | /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
---|
17 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
18 |
|
---|
19 | module.exports = class RequireEnsureDependenciesBlockParserPlugin {
|
---|
20 | /**
|
---|
21 | * @param {JavascriptParser} parser the parser
|
---|
22 | * @returns {void}
|
---|
23 | */
|
---|
24 | apply(parser) {
|
---|
25 | parser.hooks.call
|
---|
26 | .for("require.ensure")
|
---|
27 | .tap("RequireEnsureDependenciesBlockParserPlugin", expr => {
|
---|
28 | let chunkName = null;
|
---|
29 | let errorExpressionArg = null;
|
---|
30 | let errorExpression = null;
|
---|
31 | switch (expr.arguments.length) {
|
---|
32 | case 4: {
|
---|
33 | const chunkNameExpr = parser.evaluateExpression(expr.arguments[3]);
|
---|
34 | if (!chunkNameExpr.isString()) return;
|
---|
35 | chunkName = chunkNameExpr.string;
|
---|
36 | }
|
---|
37 | // falls through
|
---|
38 | case 3: {
|
---|
39 | errorExpressionArg = expr.arguments[2];
|
---|
40 | errorExpression = getFunctionExpression(errorExpressionArg);
|
---|
41 |
|
---|
42 | if (!errorExpression && !chunkName) {
|
---|
43 | const chunkNameExpr = parser.evaluateExpression(
|
---|
44 | expr.arguments[2]
|
---|
45 | );
|
---|
46 | if (!chunkNameExpr.isString()) return;
|
---|
47 | chunkName = chunkNameExpr.string;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | // falls through
|
---|
51 | case 2: {
|
---|
52 | const dependenciesExpr = parser.evaluateExpression(
|
---|
53 | expr.arguments[0]
|
---|
54 | );
|
---|
55 | const dependenciesItems =
|
---|
56 | /** @type {BasicEvaluatedExpression[]} */ (
|
---|
57 | dependenciesExpr.isArray()
|
---|
58 | ? dependenciesExpr.items
|
---|
59 | : [dependenciesExpr]
|
---|
60 | );
|
---|
61 | const successExpressionArg = expr.arguments[1];
|
---|
62 | const successExpression =
|
---|
63 | getFunctionExpression(successExpressionArg);
|
---|
64 |
|
---|
65 | if (successExpression) {
|
---|
66 | parser.walkExpressions(successExpression.expressions);
|
---|
67 | }
|
---|
68 | if (errorExpression) {
|
---|
69 | parser.walkExpressions(errorExpression.expressions);
|
---|
70 | }
|
---|
71 |
|
---|
72 | const depBlock = new RequireEnsureDependenciesBlock(
|
---|
73 | /** @type {ChunkGroupOptions & { entryOptions?: TODO }} */
|
---|
74 | (chunkName),
|
---|
75 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
76 | );
|
---|
77 | const errorCallbackExists =
|
---|
78 | expr.arguments.length === 4 ||
|
---|
79 | (!chunkName && expr.arguments.length === 3);
|
---|
80 | const dep = new RequireEnsureDependency(
|
---|
81 | /** @type {Range} */ (expr.range),
|
---|
82 | /** @type {Range} */ (expr.arguments[1].range),
|
---|
83 | errorCallbackExists &&
|
---|
84 | /** @type {Range} */ (expr.arguments[2].range)
|
---|
85 | );
|
---|
86 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
87 | depBlock.addDependency(dep);
|
---|
88 | const old = parser.state.current;
|
---|
89 | parser.state.current = /** @type {TODO} */ (depBlock);
|
---|
90 | try {
|
---|
91 | let failed = false;
|
---|
92 | parser.inScope([], () => {
|
---|
93 | for (const ee of dependenciesItems) {
|
---|
94 | if (ee.isString()) {
|
---|
95 | const ensureDependency = new RequireEnsureItemDependency(
|
---|
96 | /** @type {string} */ (ee.string)
|
---|
97 | );
|
---|
98 | ensureDependency.loc =
|
---|
99 | /** @type {DependencyLocation} */
|
---|
100 | (expr.loc);
|
---|
101 | depBlock.addDependency(ensureDependency);
|
---|
102 | } else {
|
---|
103 | failed = true;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | });
|
---|
107 | if (failed) {
|
---|
108 | return;
|
---|
109 | }
|
---|
110 | if (successExpression) {
|
---|
111 | if (successExpression.fn.body.type === "BlockStatement") {
|
---|
112 | parser.walkStatement(successExpression.fn.body);
|
---|
113 | } else {
|
---|
114 | parser.walkExpression(successExpression.fn.body);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | old.addBlock(depBlock);
|
---|
118 | } finally {
|
---|
119 | parser.state.current = old;
|
---|
120 | }
|
---|
121 | if (!successExpression) {
|
---|
122 | parser.walkExpression(successExpressionArg);
|
---|
123 | }
|
---|
124 | if (errorExpression) {
|
---|
125 | if (errorExpression.fn.body.type === "BlockStatement") {
|
---|
126 | parser.walkStatement(errorExpression.fn.body);
|
---|
127 | } else {
|
---|
128 | parser.walkExpression(errorExpression.fn.body);
|
---|
129 | }
|
---|
130 | } else if (errorExpressionArg) {
|
---|
131 | parser.walkExpression(errorExpressionArg);
|
---|
132 | }
|
---|
133 | return true;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | });
|
---|
137 | }
|
---|
138 | };
|
---|