source: trip-planner-front/node_modules/webpack/lib/dependencies/ImportParserPlugin.js@ e29cc2e

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

initial commit

  • Property mode set to 100644
File size: 7.7 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 AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
9const CommentCompilationWarning = require("../CommentCompilationWarning");
10const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
11const ContextDependencyHelpers = require("./ContextDependencyHelpers");
12const ImportContextDependency = require("./ImportContextDependency");
13const ImportDependency = require("./ImportDependency");
14const ImportEagerDependency = require("./ImportEagerDependency");
15const ImportWeakDependency = require("./ImportWeakDependency");
16
17/** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
18/** @typedef {import("../ContextModule").ContextMode} ContextMode */
19
20class ImportParserPlugin {
21 constructor(options) {
22 this.options = options;
23 }
24
25 apply(parser) {
26 parser.hooks.importCall.tap("ImportParserPlugin", expr => {
27 const param = parser.evaluateExpression(expr.source);
28
29 let chunkName = null;
30 /** @type {ContextMode} */
31 let mode = "lazy";
32 let include = null;
33 let exclude = null;
34 /** @type {string[][] | null} */
35 let exports = null;
36 /** @type {RawChunkGroupOptions} */
37 const groupOptions = {};
38
39 const { options: importOptions, errors: commentErrors } =
40 parser.parseCommentOptions(expr.range);
41
42 if (commentErrors) {
43 for (const e of commentErrors) {
44 const { comment } = e;
45 parser.state.module.addWarning(
46 new CommentCompilationWarning(
47 `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
48 comment.loc
49 )
50 );
51 }
52 }
53
54 if (importOptions) {
55 if (importOptions.webpackIgnore !== undefined) {
56 if (typeof importOptions.webpackIgnore !== "boolean") {
57 parser.state.module.addWarning(
58 new UnsupportedFeatureWarning(
59 `\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
60 expr.loc
61 )
62 );
63 } else {
64 // Do not instrument `import()` if `webpackIgnore` is `true`
65 if (importOptions.webpackIgnore) {
66 return false;
67 }
68 }
69 }
70 if (importOptions.webpackChunkName !== undefined) {
71 if (typeof importOptions.webpackChunkName !== "string") {
72 parser.state.module.addWarning(
73 new UnsupportedFeatureWarning(
74 `\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
75 expr.loc
76 )
77 );
78 } else {
79 chunkName = importOptions.webpackChunkName;
80 }
81 }
82 if (importOptions.webpackMode !== undefined) {
83 if (typeof importOptions.webpackMode !== "string") {
84 parser.state.module.addWarning(
85 new UnsupportedFeatureWarning(
86 `\`webpackMode\` expected a string, but received: ${importOptions.webpackMode}.`,
87 expr.loc
88 )
89 );
90 } else {
91 mode = importOptions.webpackMode;
92 }
93 }
94 if (importOptions.webpackPrefetch !== undefined) {
95 if (importOptions.webpackPrefetch === true) {
96 groupOptions.prefetchOrder = 0;
97 } else if (typeof importOptions.webpackPrefetch === "number") {
98 groupOptions.prefetchOrder = importOptions.webpackPrefetch;
99 } else {
100 parser.state.module.addWarning(
101 new UnsupportedFeatureWarning(
102 `\`webpackPrefetch\` expected true or a number, but received: ${importOptions.webpackPrefetch}.`,
103 expr.loc
104 )
105 );
106 }
107 }
108 if (importOptions.webpackPreload !== undefined) {
109 if (importOptions.webpackPreload === true) {
110 groupOptions.preloadOrder = 0;
111 } else if (typeof importOptions.webpackPreload === "number") {
112 groupOptions.preloadOrder = importOptions.webpackPreload;
113 } else {
114 parser.state.module.addWarning(
115 new UnsupportedFeatureWarning(
116 `\`webpackPreload\` expected true or a number, but received: ${importOptions.webpackPreload}.`,
117 expr.loc
118 )
119 );
120 }
121 }
122 if (importOptions.webpackInclude !== undefined) {
123 if (
124 !importOptions.webpackInclude ||
125 importOptions.webpackInclude.constructor.name !== "RegExp"
126 ) {
127 parser.state.module.addWarning(
128 new UnsupportedFeatureWarning(
129 `\`webpackInclude\` expected a regular expression, but received: ${importOptions.webpackInclude}.`,
130 expr.loc
131 )
132 );
133 } else {
134 include = new RegExp(importOptions.webpackInclude);
135 }
136 }
137 if (importOptions.webpackExclude !== undefined) {
138 if (
139 !importOptions.webpackExclude ||
140 importOptions.webpackExclude.constructor.name !== "RegExp"
141 ) {
142 parser.state.module.addWarning(
143 new UnsupportedFeatureWarning(
144 `\`webpackExclude\` expected a regular expression, but received: ${importOptions.webpackExclude}.`,
145 expr.loc
146 )
147 );
148 } else {
149 exclude = new RegExp(importOptions.webpackExclude);
150 }
151 }
152 if (importOptions.webpackExports !== undefined) {
153 if (
154 !(
155 typeof importOptions.webpackExports === "string" ||
156 (Array.isArray(importOptions.webpackExports) &&
157 importOptions.webpackExports.every(
158 item => typeof item === "string"
159 ))
160 )
161 ) {
162 parser.state.module.addWarning(
163 new UnsupportedFeatureWarning(
164 `\`webpackExports\` expected a string or an array of strings, but received: ${importOptions.webpackExports}.`,
165 expr.loc
166 )
167 );
168 } else {
169 if (typeof importOptions.webpackExports === "string") {
170 exports = [[importOptions.webpackExports]];
171 } else {
172 exports = Array.from(importOptions.webpackExports, e => [e]);
173 }
174 }
175 }
176 }
177
178 if (param.isString()) {
179 if (mode !== "lazy" && mode !== "eager" && mode !== "weak") {
180 parser.state.module.addWarning(
181 new UnsupportedFeatureWarning(
182 `\`webpackMode\` expected 'lazy', 'eager' or 'weak', but received: ${mode}.`,
183 expr.loc
184 )
185 );
186 }
187
188 if (mode === "eager") {
189 const dep = new ImportEagerDependency(
190 param.string,
191 expr.range,
192 exports
193 );
194 parser.state.current.addDependency(dep);
195 } else if (mode === "weak") {
196 const dep = new ImportWeakDependency(
197 param.string,
198 expr.range,
199 exports
200 );
201 parser.state.current.addDependency(dep);
202 } else {
203 const depBlock = new AsyncDependenciesBlock(
204 {
205 ...groupOptions,
206 name: chunkName
207 },
208 expr.loc,
209 param.string
210 );
211 const dep = new ImportDependency(param.string, expr.range, exports);
212 dep.loc = expr.loc;
213 depBlock.addDependency(dep);
214 parser.state.current.addBlock(depBlock);
215 }
216 return true;
217 } else {
218 if (
219 mode !== "lazy" &&
220 mode !== "lazy-once" &&
221 mode !== "eager" &&
222 mode !== "weak"
223 ) {
224 parser.state.module.addWarning(
225 new UnsupportedFeatureWarning(
226 `\`webpackMode\` expected 'lazy', 'lazy-once', 'eager' or 'weak', but received: ${mode}.`,
227 expr.loc
228 )
229 );
230 mode = "lazy";
231 }
232
233 if (mode === "weak") {
234 mode = "async-weak";
235 }
236 const dep = ContextDependencyHelpers.create(
237 ImportContextDependency,
238 expr.range,
239 param,
240 expr,
241 this.options,
242 {
243 chunkName,
244 groupOptions,
245 include,
246 exclude,
247 mode,
248 namespaceObject: parser.state.module.buildMeta.strictHarmonyModule
249 ? "strict"
250 : true,
251 typePrefix: "import()",
252 category: "esm",
253 referencedExports: exports
254 },
255 parser
256 );
257 if (!dep) return;
258 dep.loc = expr.loc;
259 dep.optional = !!parser.scope.inTry;
260 parser.state.current.addDependency(dep);
261 return true;
262 }
263 });
264 }
265}
266
267module.exports = ImportParserPlugin;
Note: See TracBrowser for help on using the repository browser.