source: trip-planner-front/node_modules/webpack/lib/dependencies/AMDDefineDependencyParserPlugin.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 9.9 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("../RuntimeGlobals");
9const AMDDefineDependency = require("./AMDDefineDependency");
10const AMDRequireArrayDependency = require("./AMDRequireArrayDependency");
11const AMDRequireContextDependency = require("./AMDRequireContextDependency");
12const AMDRequireItemDependency = require("./AMDRequireItemDependency");
13const ConstDependency = require("./ConstDependency");
14const ContextDependencyHelpers = require("./ContextDependencyHelpers");
15const DynamicExports = require("./DynamicExports");
16const LocalModuleDependency = require("./LocalModuleDependency");
17const { addLocalModule, getLocalModule } = require("./LocalModulesHelpers");
18
19const isBoundFunctionExpression = expr => {
20 if (expr.type !== "CallExpression") return false;
21 if (expr.callee.type !== "MemberExpression") return false;
22 if (expr.callee.computed) return false;
23 if (expr.callee.object.type !== "FunctionExpression") return false;
24 if (expr.callee.property.type !== "Identifier") return false;
25 if (expr.callee.property.name !== "bind") return false;
26 return true;
27};
28
29const isUnboundFunctionExpression = expr => {
30 if (expr.type === "FunctionExpression") return true;
31 if (expr.type === "ArrowFunctionExpression") return true;
32 return false;
33};
34
35const isCallable = expr => {
36 if (isUnboundFunctionExpression(expr)) return true;
37 if (isBoundFunctionExpression(expr)) return true;
38 return false;
39};
40
41class AMDDefineDependencyParserPlugin {
42 constructor(options) {
43 this.options = options;
44 }
45
46 apply(parser) {
47 parser.hooks.call
48 .for("define")
49 .tap(
50 "AMDDefineDependencyParserPlugin",
51 this.processCallDefine.bind(this, parser)
52 );
53 }
54
55 processArray(parser, expr, param, identifiers, namedModule) {
56 if (param.isArray()) {
57 param.items.forEach((param, idx) => {
58 if (
59 param.isString() &&
60 ["require", "module", "exports"].includes(param.string)
61 )
62 identifiers[idx] = param.string;
63 const result = this.processItem(parser, expr, param, namedModule);
64 if (result === undefined) {
65 this.processContext(parser, expr, param);
66 }
67 });
68 return true;
69 } else if (param.isConstArray()) {
70 const deps = [];
71 param.array.forEach((request, idx) => {
72 let dep;
73 let localModule;
74 if (request === "require") {
75 identifiers[idx] = request;
76 dep = "__webpack_require__";
77 } else if (["exports", "module"].includes(request)) {
78 identifiers[idx] = request;
79 dep = request;
80 } else if ((localModule = getLocalModule(parser.state, request))) {
81 localModule.flagUsed();
82 dep = new LocalModuleDependency(localModule, undefined, false);
83 dep.loc = expr.loc;
84 parser.state.module.addPresentationalDependency(dep);
85 } else {
86 dep = this.newRequireItemDependency(request);
87 dep.loc = expr.loc;
88 dep.optional = !!parser.scope.inTry;
89 parser.state.current.addDependency(dep);
90 }
91 deps.push(dep);
92 });
93 const dep = this.newRequireArrayDependency(deps, param.range);
94 dep.loc = expr.loc;
95 dep.optional = !!parser.scope.inTry;
96 parser.state.module.addPresentationalDependency(dep);
97 return true;
98 }
99 }
100 processItem(parser, expr, param, namedModule) {
101 if (param.isConditional()) {
102 param.options.forEach(param => {
103 const result = this.processItem(parser, expr, param);
104 if (result === undefined) {
105 this.processContext(parser, expr, param);
106 }
107 });
108 return true;
109 } else if (param.isString()) {
110 let dep, localModule;
111 if (param.string === "require") {
112 dep = new ConstDependency("__webpack_require__", param.range, [
113 RuntimeGlobals.require
114 ]);
115 } else if (param.string === "exports") {
116 dep = new ConstDependency("exports", param.range, [
117 RuntimeGlobals.exports
118 ]);
119 } else if (param.string === "module") {
120 dep = new ConstDependency("module", param.range, [
121 RuntimeGlobals.module
122 ]);
123 } else if (
124 (localModule = getLocalModule(parser.state, param.string, namedModule))
125 ) {
126 localModule.flagUsed();
127 dep = new LocalModuleDependency(localModule, param.range, false);
128 } else {
129 dep = this.newRequireItemDependency(param.string, param.range);
130 dep.optional = !!parser.scope.inTry;
131 parser.state.current.addDependency(dep);
132 return true;
133 }
134 dep.loc = expr.loc;
135 parser.state.module.addPresentationalDependency(dep);
136 return true;
137 }
138 }
139 processContext(parser, expr, param) {
140 const dep = ContextDependencyHelpers.create(
141 AMDRequireContextDependency,
142 param.range,
143 param,
144 expr,
145 this.options,
146 {
147 category: "amd"
148 },
149 parser
150 );
151 if (!dep) return;
152 dep.loc = expr.loc;
153 dep.optional = !!parser.scope.inTry;
154 parser.state.current.addDependency(dep);
155 return true;
156 }
157
158 processCallDefine(parser, expr) {
159 let array, fn, obj, namedModule;
160 switch (expr.arguments.length) {
161 case 1:
162 if (isCallable(expr.arguments[0])) {
163 // define(f() {…})
164 fn = expr.arguments[0];
165 } else if (expr.arguments[0].type === "ObjectExpression") {
166 // define({…})
167 obj = expr.arguments[0];
168 } else {
169 // define(expr)
170 // unclear if function or object
171 obj = fn = expr.arguments[0];
172 }
173 break;
174 case 2:
175 if (expr.arguments[0].type === "Literal") {
176 namedModule = expr.arguments[0].value;
177 // define("…", …)
178 if (isCallable(expr.arguments[1])) {
179 // define("…", f() {…})
180 fn = expr.arguments[1];
181 } else if (expr.arguments[1].type === "ObjectExpression") {
182 // define("…", {…})
183 obj = expr.arguments[1];
184 } else {
185 // define("…", expr)
186 // unclear if function or object
187 obj = fn = expr.arguments[1];
188 }
189 } else {
190 array = expr.arguments[0];
191 if (isCallable(expr.arguments[1])) {
192 // define([…], f() {})
193 fn = expr.arguments[1];
194 } else if (expr.arguments[1].type === "ObjectExpression") {
195 // define([…], {…})
196 obj = expr.arguments[1];
197 } else {
198 // define([…], expr)
199 // unclear if function or object
200 obj = fn = expr.arguments[1];
201 }
202 }
203 break;
204 case 3:
205 // define("…", […], f() {…})
206 namedModule = expr.arguments[0].value;
207 array = expr.arguments[1];
208 if (isCallable(expr.arguments[2])) {
209 // define("…", […], f() {})
210 fn = expr.arguments[2];
211 } else if (expr.arguments[2].type === "ObjectExpression") {
212 // define("…", […], {…})
213 obj = expr.arguments[2];
214 } else {
215 // define("…", […], expr)
216 // unclear if function or object
217 obj = fn = expr.arguments[2];
218 }
219 break;
220 default:
221 return;
222 }
223 DynamicExports.bailout(parser.state);
224 let fnParams = null;
225 let fnParamsOffset = 0;
226 if (fn) {
227 if (isUnboundFunctionExpression(fn)) {
228 fnParams = fn.params;
229 } else if (isBoundFunctionExpression(fn)) {
230 fnParams = fn.callee.object.params;
231 fnParamsOffset = fn.arguments.length - 1;
232 if (fnParamsOffset < 0) {
233 fnParamsOffset = 0;
234 }
235 }
236 }
237 let fnRenames = new Map();
238 if (array) {
239 const identifiers = {};
240 const param = parser.evaluateExpression(array);
241 const result = this.processArray(
242 parser,
243 expr,
244 param,
245 identifiers,
246 namedModule
247 );
248 if (!result) return;
249 if (fnParams) {
250 fnParams = fnParams.slice(fnParamsOffset).filter((param, idx) => {
251 if (identifiers[idx]) {
252 fnRenames.set(param.name, parser.getVariableInfo(identifiers[idx]));
253 return false;
254 }
255 return true;
256 });
257 }
258 } else {
259 const identifiers = ["require", "exports", "module"];
260 if (fnParams) {
261 fnParams = fnParams.slice(fnParamsOffset).filter((param, idx) => {
262 if (identifiers[idx]) {
263 fnRenames.set(param.name, parser.getVariableInfo(identifiers[idx]));
264 return false;
265 }
266 return true;
267 });
268 }
269 }
270 let inTry;
271 if (fn && isUnboundFunctionExpression(fn)) {
272 inTry = parser.scope.inTry;
273 parser.inScope(fnParams, () => {
274 for (const [name, varInfo] of fnRenames) {
275 parser.setVariable(name, varInfo);
276 }
277 parser.scope.inTry = inTry;
278 if (fn.body.type === "BlockStatement") {
279 parser.detectMode(fn.body.body);
280 const prev = parser.prevStatement;
281 parser.preWalkStatement(fn.body);
282 parser.prevStatement = prev;
283 parser.walkStatement(fn.body);
284 } else {
285 parser.walkExpression(fn.body);
286 }
287 });
288 } else if (fn && isBoundFunctionExpression(fn)) {
289 inTry = parser.scope.inTry;
290 parser.inScope(
291 fn.callee.object.params.filter(
292 i => !["require", "module", "exports"].includes(i.name)
293 ),
294 () => {
295 for (const [name, varInfo] of fnRenames) {
296 parser.setVariable(name, varInfo);
297 }
298 parser.scope.inTry = inTry;
299 if (fn.callee.object.body.type === "BlockStatement") {
300 parser.detectMode(fn.callee.object.body.body);
301 const prev = parser.prevStatement;
302 parser.preWalkStatement(fn.callee.object.body);
303 parser.prevStatement = prev;
304 parser.walkStatement(fn.callee.object.body);
305 } else {
306 parser.walkExpression(fn.callee.object.body);
307 }
308 }
309 );
310 if (fn.arguments) {
311 parser.walkExpressions(fn.arguments);
312 }
313 } else if (fn || obj) {
314 parser.walkExpression(fn || obj);
315 }
316
317 const dep = this.newDefineDependency(
318 expr.range,
319 array ? array.range : null,
320 fn ? fn.range : null,
321 obj ? obj.range : null,
322 namedModule ? namedModule : null
323 );
324 dep.loc = expr.loc;
325 if (namedModule) {
326 dep.localModule = addLocalModule(parser.state, namedModule);
327 }
328 parser.state.module.addPresentationalDependency(dep);
329 return true;
330 }
331
332 newDefineDependency(
333 range,
334 arrayRange,
335 functionRange,
336 objectRange,
337 namedModule
338 ) {
339 return new AMDDefineDependency(
340 range,
341 arrayRange,
342 functionRange,
343 objectRange,
344 namedModule
345 );
346 }
347 newRequireArrayDependency(depsArray, range) {
348 return new AMDRequireArrayDependency(depsArray, range);
349 }
350 newRequireItemDependency(request, range) {
351 return new AMDRequireItemDependency(request, range);
352 }
353}
354module.exports = AMDDefineDependencyParserPlugin;
Note: See TracBrowser for help on using the repository browser.