source: trip-planner-front/node_modules/webpack/lib/dependencies/CommonJsImportsParserPlugin.js

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

initial commit

  • Property mode set to 100644
File size: 11.6 KB
RevLine 
[6a3a178]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const CommentCompilationWarning = require("../CommentCompilationWarning");
9const RuntimeGlobals = require("../RuntimeGlobals");
10const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
11const {
12 evaluateToIdentifier,
13 evaluateToString,
14 expressionIsUnsupported,
15 toConstantDependency
16} = require("../javascript/JavascriptParserHelpers");
17const CommonJsFullRequireDependency = require("./CommonJsFullRequireDependency");
18const CommonJsRequireContextDependency = require("./CommonJsRequireContextDependency");
19const CommonJsRequireDependency = require("./CommonJsRequireDependency");
20const ConstDependency = require("./ConstDependency");
21const ContextDependencyHelpers = require("./ContextDependencyHelpers");
22const LocalModuleDependency = require("./LocalModuleDependency");
23const { getLocalModule } = require("./LocalModulesHelpers");
24const RequireHeaderDependency = require("./RequireHeaderDependency");
25const RequireResolveContextDependency = require("./RequireResolveContextDependency");
26const RequireResolveDependency = require("./RequireResolveDependency");
27const RequireResolveHeaderDependency = require("./RequireResolveHeaderDependency");
28
29/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
30
31class CommonJsImportsParserPlugin {
32 /**
33 * @param {JavascriptParserOptions} options parser options
34 */
35 constructor(options) {
36 this.options = options;
37 }
38
39 apply(parser) {
40 const options = this.options;
41
42 // metadata //
43 const tapRequireExpression = (expression, getMembers) => {
44 parser.hooks.typeof
45 .for(expression)
46 .tap(
47 "CommonJsPlugin",
48 toConstantDependency(parser, JSON.stringify("function"))
49 );
50 parser.hooks.evaluateTypeof
51 .for(expression)
52 .tap("CommonJsPlugin", evaluateToString("function"));
53 parser.hooks.evaluateIdentifier
54 .for(expression)
55 .tap(
56 "CommonJsPlugin",
57 evaluateToIdentifier(expression, "require", getMembers, true)
58 );
59 };
60 tapRequireExpression("require", () => []);
61 tapRequireExpression("require.resolve", () => ["resolve"]);
62 tapRequireExpression("require.resolveWeak", () => ["resolveWeak"]);
63
64 // Weird stuff //
65 parser.hooks.assign.for("require").tap("CommonJsPlugin", expr => {
66 // to not leak to global "require", we need to define a local require here.
67 const dep = new ConstDependency("var require;", 0);
68 dep.loc = expr.loc;
69 parser.state.module.addPresentationalDependency(dep);
70 return true;
71 });
72
73 // Unsupported //
74 parser.hooks.expression
75 .for("require.main.require")
76 .tap(
77 "CommonJsPlugin",
78 expressionIsUnsupported(
79 parser,
80 "require.main.require is not supported by webpack."
81 )
82 );
83 parser.hooks.call
84 .for("require.main.require")
85 .tap(
86 "CommonJsPlugin",
87 expressionIsUnsupported(
88 parser,
89 "require.main.require is not supported by webpack."
90 )
91 );
92 parser.hooks.expression
93 .for("module.parent.require")
94 .tap(
95 "CommonJsPlugin",
96 expressionIsUnsupported(
97 parser,
98 "module.parent.require is not supported by webpack."
99 )
100 );
101 parser.hooks.call
102 .for("module.parent.require")
103 .tap(
104 "CommonJsPlugin",
105 expressionIsUnsupported(
106 parser,
107 "module.parent.require is not supported by webpack."
108 )
109 );
110
111 // renaming //
112 parser.hooks.canRename.for("require").tap("CommonJsPlugin", () => true);
113 parser.hooks.rename.for("require").tap("CommonJsPlugin", expr => {
114 // To avoid "not defined" error, replace the value with undefined
115 const dep = new ConstDependency("undefined", expr.range);
116 dep.loc = expr.loc;
117 parser.state.module.addPresentationalDependency(dep);
118 return false;
119 });
120
121 // inspection //
122 parser.hooks.expression
123 .for("require.cache")
124 .tap(
125 "CommonJsImportsParserPlugin",
126 toConstantDependency(parser, RuntimeGlobals.moduleCache, [
127 RuntimeGlobals.moduleCache,
128 RuntimeGlobals.moduleId,
129 RuntimeGlobals.moduleLoaded
130 ])
131 );
132
133 // require as expression //
134 parser.hooks.expression
135 .for("require")
136 .tap("CommonJsImportsParserPlugin", expr => {
137 const dep = new CommonJsRequireContextDependency(
138 {
139 request: options.unknownContextRequest,
140 recursive: options.unknownContextRecursive,
141 regExp: options.unknownContextRegExp,
142 mode: "sync"
143 },
144 expr.range
145 );
146 dep.critical =
147 options.unknownContextCritical &&
148 "require function is used in a way in which dependencies cannot be statically extracted";
149 dep.loc = expr.loc;
150 dep.optional = !!parser.scope.inTry;
151 parser.state.current.addDependency(dep);
152 return true;
153 });
154
155 // require //
156 const processRequireItem = (expr, param) => {
157 if (param.isString()) {
158 const dep = new CommonJsRequireDependency(param.string, param.range);
159 dep.loc = expr.loc;
160 dep.optional = !!parser.scope.inTry;
161 parser.state.current.addDependency(dep);
162 return true;
163 }
164 };
165 const processRequireContext = (expr, param) => {
166 const dep = ContextDependencyHelpers.create(
167 CommonJsRequireContextDependency,
168 expr.range,
169 param,
170 expr,
171 options,
172 {
173 category: "commonjs"
174 },
175 parser
176 );
177 if (!dep) return;
178 dep.loc = expr.loc;
179 dep.optional = !!parser.scope.inTry;
180 parser.state.current.addDependency(dep);
181 return true;
182 };
183 const createRequireHandler = callNew => expr => {
184 if (options.commonjsMagicComments) {
185 const { options: requireOptions, errors: commentErrors } =
186 parser.parseCommentOptions(expr.range);
187
188 if (commentErrors) {
189 for (const e of commentErrors) {
190 const { comment } = e;
191 parser.state.module.addWarning(
192 new CommentCompilationWarning(
193 `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
194 comment.loc
195 )
196 );
197 }
198 }
199 if (requireOptions) {
200 if (requireOptions.webpackIgnore !== undefined) {
201 if (typeof requireOptions.webpackIgnore !== "boolean") {
202 parser.state.module.addWarning(
203 new UnsupportedFeatureWarning(
204 `\`webpackIgnore\` expected a boolean, but received: ${requireOptions.webpackIgnore}.`,
205 expr.loc
206 )
207 );
208 } else {
209 // Do not instrument `require()` if `webpackIgnore` is `true`
210 if (requireOptions.webpackIgnore) {
211 return true;
212 }
213 }
214 }
215 }
216 }
217
218 if (expr.arguments.length !== 1) return;
219 let localModule;
220 const param = parser.evaluateExpression(expr.arguments[0]);
221 if (param.isConditional()) {
222 let isExpression = false;
223 for (const p of param.options) {
224 const result = processRequireItem(expr, p);
225 if (result === undefined) {
226 isExpression = true;
227 }
228 }
229 if (!isExpression) {
230 const dep = new RequireHeaderDependency(expr.callee.range);
231 dep.loc = expr.loc;
232 parser.state.module.addPresentationalDependency(dep);
233 return true;
234 }
235 }
236 if (
237 param.isString() &&
238 (localModule = getLocalModule(parser.state, param.string))
239 ) {
240 localModule.flagUsed();
241 const dep = new LocalModuleDependency(localModule, expr.range, callNew);
242 dep.loc = expr.loc;
243 parser.state.module.addPresentationalDependency(dep);
244 return true;
245 } else {
246 const result = processRequireItem(expr, param);
247 if (result === undefined) {
248 processRequireContext(expr, param);
249 } else {
250 const dep = new RequireHeaderDependency(expr.callee.range);
251 dep.loc = expr.loc;
252 parser.state.module.addPresentationalDependency(dep);
253 }
254 return true;
255 }
256 };
257 parser.hooks.call
258 .for("require")
259 .tap("CommonJsImportsParserPlugin", createRequireHandler(false));
260 parser.hooks.new
261 .for("require")
262 .tap("CommonJsImportsParserPlugin", createRequireHandler(true));
263 parser.hooks.call
264 .for("module.require")
265 .tap("CommonJsImportsParserPlugin", createRequireHandler(false));
266 parser.hooks.new
267 .for("module.require")
268 .tap("CommonJsImportsParserPlugin", createRequireHandler(true));
269
270 // require with property access //
271 const chainHandler = (expr, calleeMembers, callExpr, members) => {
272 if (callExpr.arguments.length !== 1) return;
273 const param = parser.evaluateExpression(callExpr.arguments[0]);
274 if (param.isString() && !getLocalModule(parser.state, param.string)) {
275 const dep = new CommonJsFullRequireDependency(
276 param.string,
277 expr.range,
278 members
279 );
280 dep.asiSafe = !parser.isAsiPosition(expr.range[0]);
281 dep.optional = !!parser.scope.inTry;
282 dep.loc = expr.loc;
283 parser.state.module.addDependency(dep);
284 return true;
285 }
286 };
287 const callChainHandler = (expr, calleeMembers, callExpr, members) => {
288 if (callExpr.arguments.length !== 1) return;
289 const param = parser.evaluateExpression(callExpr.arguments[0]);
290 if (param.isString() && !getLocalModule(parser.state, param.string)) {
291 const dep = new CommonJsFullRequireDependency(
292 param.string,
293 expr.callee.range,
294 members
295 );
296 dep.call = true;
297 dep.asiSafe = !parser.isAsiPosition(expr.range[0]);
298 dep.optional = !!parser.scope.inTry;
299 dep.loc = expr.callee.loc;
300 parser.state.module.addDependency(dep);
301 parser.walkExpressions(expr.arguments);
302 return true;
303 }
304 };
305 parser.hooks.memberChainOfCallMemberChain
306 .for("require")
307 .tap("CommonJsImportsParserPlugin", chainHandler);
308 parser.hooks.memberChainOfCallMemberChain
309 .for("module.require")
310 .tap("CommonJsImportsParserPlugin", chainHandler);
311 parser.hooks.callMemberChainOfCallMemberChain
312 .for("require")
313 .tap("CommonJsImportsParserPlugin", callChainHandler);
314 parser.hooks.callMemberChainOfCallMemberChain
315 .for("module.require")
316 .tap("CommonJsImportsParserPlugin", callChainHandler);
317
318 // require.resolve //
319 const processResolve = (expr, weak) => {
320 if (expr.arguments.length !== 1) return;
321 const param = parser.evaluateExpression(expr.arguments[0]);
322 if (param.isConditional()) {
323 for (const option of param.options) {
324 const result = processResolveItem(expr, option, weak);
325 if (result === undefined) {
326 processResolveContext(expr, option, weak);
327 }
328 }
329 const dep = new RequireResolveHeaderDependency(expr.callee.range);
330 dep.loc = expr.loc;
331 parser.state.module.addPresentationalDependency(dep);
332 return true;
333 } else {
334 const result = processResolveItem(expr, param, weak);
335 if (result === undefined) {
336 processResolveContext(expr, param, weak);
337 }
338 const dep = new RequireResolveHeaderDependency(expr.callee.range);
339 dep.loc = expr.loc;
340 parser.state.module.addPresentationalDependency(dep);
341 return true;
342 }
343 };
344 const processResolveItem = (expr, param, weak) => {
345 if (param.isString()) {
346 const dep = new RequireResolveDependency(param.string, param.range);
347 dep.loc = expr.loc;
348 dep.optional = !!parser.scope.inTry;
349 dep.weak = weak;
350 parser.state.current.addDependency(dep);
351 return true;
352 }
353 };
354 const processResolveContext = (expr, param, weak) => {
355 const dep = ContextDependencyHelpers.create(
356 RequireResolveContextDependency,
357 param.range,
358 param,
359 expr,
360 options,
361 {
362 category: "commonjs",
363 mode: weak ? "weak" : "sync"
364 },
365 parser
366 );
367 if (!dep) return;
368 dep.loc = expr.loc;
369 dep.optional = !!parser.scope.inTry;
370 parser.state.current.addDependency(dep);
371 return true;
372 };
373
374 parser.hooks.call
375 .for("require.resolve")
376 .tap("RequireResolveDependencyParserPlugin", expr => {
377 return processResolve(expr, false);
378 });
379 parser.hooks.call
380 .for("require.resolveWeak")
381 .tap("RequireResolveDependencyParserPlugin", expr => {
382 return processResolve(expr, true);
383 });
384 }
385}
386module.exports = CommonJsImportsParserPlugin;
Note: See TracBrowser for help on using the repository browser.