AMDRequireDependenciesBlockParserPlugin.js
7.6 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
278
279
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const RuntimeGlobals = require("../RuntimeGlobals");
const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
const AMDRequireArrayDependency = require("./AMDRequireArrayDependency");
const AMDRequireContextDependency = require("./AMDRequireContextDependency");
const AMDRequireDependenciesBlock = require("./AMDRequireDependenciesBlock");
const AMDRequireDependency = require("./AMDRequireDependency");
const AMDRequireItemDependency = require("./AMDRequireItemDependency");
const ConstDependency = require("./ConstDependency");
const ContextDependencyHelpers = require("./ContextDependencyHelpers");
const LocalModuleDependency = require("./LocalModuleDependency");
const { getLocalModule } = require("./LocalModulesHelpers");
const UnsupportedDependency = require("./UnsupportedDependency");
const getFunctionExpression = require("./getFunctionExpression");
class AMDRequireDependenciesBlockParserPlugin {
constructor(options) {
this.options = options;
}
processFunctionArgument(parser, expression) {
let bindThis = true;
const fnData = getFunctionExpression(expression);
if (fnData) {
parser.inScope(
fnData.fn.params.filter(i => {
return !["require", "module", "exports"].includes(i.name);
}),
() => {
if (fnData.fn.body.type === "BlockStatement") {
parser.walkStatement(fnData.fn.body);
} else {
parser.walkExpression(fnData.fn.body);
}
}
);
parser.walkExpressions(fnData.expressions);
if (fnData.needThis === false) {
bindThis = false;
}
} else {
parser.walkExpression(expression);
}
return bindThis;
}
apply(parser) {
parser.hooks.call
.for("require")
.tap(
"AMDRequireDependenciesBlockParserPlugin",
this.processCallRequire.bind(this, parser)
);
}
processArray(parser, expr, param) {
if (param.isArray()) {
for (const p of param.items) {
const result = this.processItem(parser, expr, p);
if (result === undefined) {
this.processContext(parser, expr, p);
}
}
return true;
} else if (param.isConstArray()) {
const deps = [];
for (const request of param.array) {
let dep, localModule;
if (request === "require") {
dep = "__webpack_require__";
} else if (["exports", "module"].includes(request)) {
dep = request;
} else if ((localModule = getLocalModule(parser.state, request))) {
localModule.flagUsed();
dep = new LocalModuleDependency(localModule, undefined, false);
dep.loc = expr.loc;
parser.state.module.addPresentationalDependency(dep);
} else {
dep = this.newRequireItemDependency(request);
dep.loc = expr.loc;
dep.optional = !!parser.scope.inTry;
parser.state.current.addDependency(dep);
}
deps.push(dep);
}
const dep = this.newRequireArrayDependency(deps, param.range);
dep.loc = expr.loc;
dep.optional = !!parser.scope.inTry;
parser.state.module.addPresentationalDependency(dep);
return true;
}
}
processItem(parser, expr, param) {
if (param.isConditional()) {
for (const p of param.options) {
const result = this.processItem(parser, expr, p);
if (result === undefined) {
this.processContext(parser, expr, p);
}
}
return true;
} else if (param.isString()) {
let dep, localModule;
if (param.string === "require") {
dep = new ConstDependency("__webpack_require__", param.string, [
RuntimeGlobals.require
]);
} else if (param.string === "module") {
dep = new ConstDependency(
parser.state.module.buildInfo.moduleArgument,
param.range,
[RuntimeGlobals.module]
);
} else if (param.string === "exports") {
dep = new ConstDependency(
parser.state.module.buildInfo.exportsArgument,
param.range,
[RuntimeGlobals.exports]
);
} else if ((localModule = getLocalModule(parser.state, param.string))) {
localModule.flagUsed();
dep = new LocalModuleDependency(localModule, param.range, false);
} else {
dep = this.newRequireItemDependency(param.string, param.range);
dep.loc = expr.loc;
dep.optional = !!parser.scope.inTry;
parser.state.current.addDependency(dep);
return true;
}
dep.loc = expr.loc;
parser.state.module.addPresentationalDependency(dep);
return true;
}
}
processContext(parser, expr, param) {
const dep = ContextDependencyHelpers.create(
AMDRequireContextDependency,
param.range,
param,
expr,
this.options,
{
category: "amd"
},
parser
);
if (!dep) return;
dep.loc = expr.loc;
dep.optional = !!parser.scope.inTry;
parser.state.current.addDependency(dep);
return true;
}
processArrayForRequestString(param) {
if (param.isArray()) {
const result = param.items.map(item =>
this.processItemForRequestString(item)
);
if (result.every(Boolean)) return result.join(" ");
} else if (param.isConstArray()) {
return param.array.join(" ");
}
}
processItemForRequestString(param) {
if (param.isConditional()) {
const result = param.options.map(item =>
this.processItemForRequestString(item)
);
if (result.every(Boolean)) return result.join("|");
} else if (param.isString()) {
return param.string;
}
}
processCallRequire(parser, expr) {
let param;
let depBlock;
let dep;
let result;
const old = parser.state.current;
if (expr.arguments.length >= 1) {
param = parser.evaluateExpression(expr.arguments[0]);
depBlock = this.newRequireDependenciesBlock(
expr.loc,
this.processArrayForRequestString(param)
);
dep = this.newRequireDependency(
expr.range,
param.range,
expr.arguments.length > 1 ? expr.arguments[1].range : null,
expr.arguments.length > 2 ? expr.arguments[2].range : null
);
dep.loc = expr.loc;
depBlock.addDependency(dep);
parser.state.current = depBlock;
}
if (expr.arguments.length === 1) {
parser.inScope([], () => {
result = this.processArray(parser, expr, param);
});
parser.state.current = old;
if (!result) return;
parser.state.current.addBlock(depBlock);
return true;
}
if (expr.arguments.length === 2 || expr.arguments.length === 3) {
try {
parser.inScope([], () => {
result = this.processArray(parser, expr, param);
});
if (!result) {
const dep = new UnsupportedDependency("unsupported", expr.range);
old.addPresentationalDependency(dep);
if (parser.state.module) {
parser.state.module.addError(
new UnsupportedFeatureWarning(
"Cannot statically analyse 'require(…, …)' in line " +
expr.loc.start.line,
expr.loc
)
);
}
depBlock = null;
return true;
}
dep.functionBindThis = this.processFunctionArgument(
parser,
expr.arguments[1]
);
if (expr.arguments.length === 3) {
dep.errorCallbackBindThis = this.processFunctionArgument(
parser,
expr.arguments[2]
);
}
} finally {
parser.state.current = old;
if (depBlock) parser.state.current.addBlock(depBlock);
}
return true;
}
}
newRequireDependenciesBlock(loc, request) {
return new AMDRequireDependenciesBlock(loc, request);
}
newRequireDependency(
outerRange,
arrayRange,
functionRange,
errorCallbackRange
) {
return new AMDRequireDependency(
outerRange,
arrayRange,
functionRange,
errorCallbackRange
);
}
newRequireItemDependency(request, range) {
return new AMDRequireItemDependency(request, range);
}
newRequireArrayDependency(depsArray, range) {
return new AMDRequireArrayDependency(depsArray, range);
}
}
module.exports = AMDRequireDependenciesBlockParserPlugin;