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 AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
|
---|
9 | const CommentCompilationWarning = require("../CommentCompilationWarning");
|
---|
10 | const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
|
---|
11 | const { getImportAttributes } = require("../javascript/JavascriptParser");
|
---|
12 | const ContextDependencyHelpers = require("./ContextDependencyHelpers");
|
---|
13 | const ImportContextDependency = require("./ImportContextDependency");
|
---|
14 | const ImportDependency = require("./ImportDependency");
|
---|
15 | const ImportEagerDependency = require("./ImportEagerDependency");
|
---|
16 | const ImportWeakDependency = require("./ImportWeakDependency");
|
---|
17 |
|
---|
18 | /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
---|
19 | /** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
|
---|
20 | /** @typedef {import("../ContextModule").ContextMode} ContextMode */
|
---|
21 | /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
---|
22 | /** @typedef {import("../Module").BuildMeta} BuildMeta */
|
---|
23 | /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
---|
24 | /** @typedef {import("../javascript/JavascriptParser").ImportExpression} ImportExpression */
|
---|
25 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
26 |
|
---|
27 | class ImportParserPlugin {
|
---|
28 | /**
|
---|
29 | * @param {JavascriptParserOptions} options options
|
---|
30 | */
|
---|
31 | constructor(options) {
|
---|
32 | this.options = options;
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @param {JavascriptParser} parser the parser
|
---|
37 | * @returns {void}
|
---|
38 | */
|
---|
39 | apply(parser) {
|
---|
40 | /**
|
---|
41 | * @template T
|
---|
42 | * @param {Iterable<T>} enumerable enumerable
|
---|
43 | * @returns {T[][]} array of array
|
---|
44 | */
|
---|
45 | const exportsFromEnumerable = enumerable =>
|
---|
46 | Array.from(enumerable, e => [e]);
|
---|
47 | parser.hooks.importCall.tap("ImportParserPlugin", expr => {
|
---|
48 | const param = parser.evaluateExpression(expr.source);
|
---|
49 |
|
---|
50 | let chunkName = null;
|
---|
51 | let mode = /** @type {ContextMode} */ (this.options.dynamicImportMode);
|
---|
52 | let include = null;
|
---|
53 | let exclude = null;
|
---|
54 | /** @type {string[][] | null} */
|
---|
55 | let exports = null;
|
---|
56 | /** @type {RawChunkGroupOptions} */
|
---|
57 | const groupOptions = {};
|
---|
58 |
|
---|
59 | const {
|
---|
60 | dynamicImportPreload,
|
---|
61 | dynamicImportPrefetch,
|
---|
62 | dynamicImportFetchPriority
|
---|
63 | } = this.options;
|
---|
64 | if (dynamicImportPreload !== undefined && dynamicImportPreload !== false)
|
---|
65 | groupOptions.preloadOrder =
|
---|
66 | dynamicImportPreload === true ? 0 : dynamicImportPreload;
|
---|
67 | if (
|
---|
68 | dynamicImportPrefetch !== undefined &&
|
---|
69 | dynamicImportPrefetch !== false
|
---|
70 | )
|
---|
71 | groupOptions.prefetchOrder =
|
---|
72 | dynamicImportPrefetch === true ? 0 : dynamicImportPrefetch;
|
---|
73 | if (
|
---|
74 | dynamicImportFetchPriority !== undefined &&
|
---|
75 | dynamicImportFetchPriority !== false
|
---|
76 | )
|
---|
77 | groupOptions.fetchPriority = dynamicImportFetchPriority;
|
---|
78 |
|
---|
79 | const { options: importOptions, errors: commentErrors } =
|
---|
80 | parser.parseCommentOptions(/** @type {Range} */ (expr.range));
|
---|
81 |
|
---|
82 | if (commentErrors) {
|
---|
83 | for (const e of commentErrors) {
|
---|
84 | const { comment } = e;
|
---|
85 | parser.state.module.addWarning(
|
---|
86 | new CommentCompilationWarning(
|
---|
87 | `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
|
---|
88 | /** @type {DependencyLocation} */ (comment.loc)
|
---|
89 | )
|
---|
90 | );
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (importOptions) {
|
---|
95 | if (importOptions.webpackIgnore !== undefined) {
|
---|
96 | if (typeof importOptions.webpackIgnore !== "boolean") {
|
---|
97 | parser.state.module.addWarning(
|
---|
98 | new UnsupportedFeatureWarning(
|
---|
99 | `\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
|
---|
100 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
101 | )
|
---|
102 | );
|
---|
103 | } else if (importOptions.webpackIgnore) {
|
---|
104 | // Do not instrument `import()` if `webpackIgnore` is `true`
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | if (importOptions.webpackChunkName !== undefined) {
|
---|
109 | if (typeof importOptions.webpackChunkName !== "string") {
|
---|
110 | parser.state.module.addWarning(
|
---|
111 | new UnsupportedFeatureWarning(
|
---|
112 | `\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
|
---|
113 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
114 | )
|
---|
115 | );
|
---|
116 | } else {
|
---|
117 | chunkName = importOptions.webpackChunkName;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | if (importOptions.webpackMode !== undefined) {
|
---|
121 | if (typeof importOptions.webpackMode !== "string") {
|
---|
122 | parser.state.module.addWarning(
|
---|
123 | new UnsupportedFeatureWarning(
|
---|
124 | `\`webpackMode\` expected a string, but received: ${importOptions.webpackMode}.`,
|
---|
125 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
126 | )
|
---|
127 | );
|
---|
128 | } else {
|
---|
129 | mode = /** @type {ContextMode} */ (importOptions.webpackMode);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | if (importOptions.webpackPrefetch !== undefined) {
|
---|
133 | if (importOptions.webpackPrefetch === true) {
|
---|
134 | groupOptions.prefetchOrder = 0;
|
---|
135 | } else if (typeof importOptions.webpackPrefetch === "number") {
|
---|
136 | groupOptions.prefetchOrder = importOptions.webpackPrefetch;
|
---|
137 | } else {
|
---|
138 | parser.state.module.addWarning(
|
---|
139 | new UnsupportedFeatureWarning(
|
---|
140 | `\`webpackPrefetch\` expected true or a number, but received: ${importOptions.webpackPrefetch}.`,
|
---|
141 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
142 | )
|
---|
143 | );
|
---|
144 | }
|
---|
145 | }
|
---|
146 | if (importOptions.webpackPreload !== undefined) {
|
---|
147 | if (importOptions.webpackPreload === true) {
|
---|
148 | groupOptions.preloadOrder = 0;
|
---|
149 | } else if (typeof importOptions.webpackPreload === "number") {
|
---|
150 | groupOptions.preloadOrder = importOptions.webpackPreload;
|
---|
151 | } else {
|
---|
152 | parser.state.module.addWarning(
|
---|
153 | new UnsupportedFeatureWarning(
|
---|
154 | `\`webpackPreload\` expected true or a number, but received: ${importOptions.webpackPreload}.`,
|
---|
155 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
156 | )
|
---|
157 | );
|
---|
158 | }
|
---|
159 | }
|
---|
160 | if (importOptions.webpackFetchPriority !== undefined) {
|
---|
161 | if (
|
---|
162 | typeof importOptions.webpackFetchPriority === "string" &&
|
---|
163 | ["high", "low", "auto"].includes(importOptions.webpackFetchPriority)
|
---|
164 | ) {
|
---|
165 | groupOptions.fetchPriority =
|
---|
166 | /** @type {"low" | "high" | "auto"} */
|
---|
167 | (importOptions.webpackFetchPriority);
|
---|
168 | } else {
|
---|
169 | parser.state.module.addWarning(
|
---|
170 | new UnsupportedFeatureWarning(
|
---|
171 | `\`webpackFetchPriority\` expected true or "low", "high" or "auto", but received: ${importOptions.webpackFetchPriority}.`,
|
---|
172 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
173 | )
|
---|
174 | );
|
---|
175 | }
|
---|
176 | }
|
---|
177 | if (importOptions.webpackInclude !== undefined) {
|
---|
178 | if (
|
---|
179 | !importOptions.webpackInclude ||
|
---|
180 | !(importOptions.webpackInclude instanceof RegExp)
|
---|
181 | ) {
|
---|
182 | parser.state.module.addWarning(
|
---|
183 | new UnsupportedFeatureWarning(
|
---|
184 | `\`webpackInclude\` expected a regular expression, but received: ${importOptions.webpackInclude}.`,
|
---|
185 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
186 | )
|
---|
187 | );
|
---|
188 | } else {
|
---|
189 | include = importOptions.webpackInclude;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | if (importOptions.webpackExclude !== undefined) {
|
---|
193 | if (
|
---|
194 | !importOptions.webpackExclude ||
|
---|
195 | !(importOptions.webpackExclude instanceof RegExp)
|
---|
196 | ) {
|
---|
197 | parser.state.module.addWarning(
|
---|
198 | new UnsupportedFeatureWarning(
|
---|
199 | `\`webpackExclude\` expected a regular expression, but received: ${importOptions.webpackExclude}.`,
|
---|
200 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
201 | )
|
---|
202 | );
|
---|
203 | } else {
|
---|
204 | exclude = importOptions.webpackExclude;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | if (importOptions.webpackExports !== undefined) {
|
---|
208 | if (
|
---|
209 | !(
|
---|
210 | typeof importOptions.webpackExports === "string" ||
|
---|
211 | (Array.isArray(importOptions.webpackExports) &&
|
---|
212 | /** @type {string[]} */ (importOptions.webpackExports).every(
|
---|
213 | item => typeof item === "string"
|
---|
214 | ))
|
---|
215 | )
|
---|
216 | ) {
|
---|
217 | parser.state.module.addWarning(
|
---|
218 | new UnsupportedFeatureWarning(
|
---|
219 | `\`webpackExports\` expected a string or an array of strings, but received: ${importOptions.webpackExports}.`,
|
---|
220 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
221 | )
|
---|
222 | );
|
---|
223 | } else if (typeof importOptions.webpackExports === "string") {
|
---|
224 | exports = [[importOptions.webpackExports]];
|
---|
225 | } else {
|
---|
226 | exports = exportsFromEnumerable(importOptions.webpackExports);
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | if (
|
---|
232 | mode !== "lazy" &&
|
---|
233 | mode !== "lazy-once" &&
|
---|
234 | mode !== "eager" &&
|
---|
235 | mode !== "weak"
|
---|
236 | ) {
|
---|
237 | parser.state.module.addWarning(
|
---|
238 | new UnsupportedFeatureWarning(
|
---|
239 | `\`webpackMode\` expected 'lazy', 'lazy-once', 'eager' or 'weak', but received: ${mode}.`,
|
---|
240 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
241 | )
|
---|
242 | );
|
---|
243 | mode = "lazy";
|
---|
244 | }
|
---|
245 |
|
---|
246 | const referencedPropertiesInDestructuring =
|
---|
247 | parser.destructuringAssignmentPropertiesFor(expr);
|
---|
248 | if (referencedPropertiesInDestructuring) {
|
---|
249 | if (exports) {
|
---|
250 | parser.state.module.addWarning(
|
---|
251 | new UnsupportedFeatureWarning(
|
---|
252 | "`webpackExports` could not be used with destructuring assignment.",
|
---|
253 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
254 | )
|
---|
255 | );
|
---|
256 | }
|
---|
257 | exports = exportsFromEnumerable(
|
---|
258 | [...referencedPropertiesInDestructuring].map(({ id }) => id)
|
---|
259 | );
|
---|
260 | }
|
---|
261 |
|
---|
262 | if (param.isString()) {
|
---|
263 | const attributes = getImportAttributes(expr);
|
---|
264 |
|
---|
265 | if (mode === "eager") {
|
---|
266 | const dep = new ImportEagerDependency(
|
---|
267 | /** @type {string} */ (param.string),
|
---|
268 | /** @type {Range} */ (expr.range),
|
---|
269 | exports,
|
---|
270 | attributes
|
---|
271 | );
|
---|
272 | parser.state.current.addDependency(dep);
|
---|
273 | } else if (mode === "weak") {
|
---|
274 | const dep = new ImportWeakDependency(
|
---|
275 | /** @type {string} */ (param.string),
|
---|
276 | /** @type {Range} */ (expr.range),
|
---|
277 | exports,
|
---|
278 | attributes
|
---|
279 | );
|
---|
280 | parser.state.current.addDependency(dep);
|
---|
281 | } else {
|
---|
282 | const depBlock = new AsyncDependenciesBlock(
|
---|
283 | {
|
---|
284 | ...groupOptions,
|
---|
285 | name: chunkName
|
---|
286 | },
|
---|
287 | /** @type {DependencyLocation} */ (expr.loc),
|
---|
288 | param.string
|
---|
289 | );
|
---|
290 | const dep = new ImportDependency(
|
---|
291 | /** @type {string} */ (param.string),
|
---|
292 | /** @type {Range} */ (expr.range),
|
---|
293 | exports,
|
---|
294 | attributes
|
---|
295 | );
|
---|
296 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
297 | dep.optional = Boolean(parser.scope.inTry);
|
---|
298 | depBlock.addDependency(dep);
|
---|
299 | parser.state.current.addBlock(depBlock);
|
---|
300 | }
|
---|
301 | return true;
|
---|
302 | }
|
---|
303 | if (mode === "weak") {
|
---|
304 | mode = "async-weak";
|
---|
305 | }
|
---|
306 | const dep = ContextDependencyHelpers.create(
|
---|
307 | ImportContextDependency,
|
---|
308 | /** @type {Range} */ (expr.range),
|
---|
309 | param,
|
---|
310 | expr,
|
---|
311 | this.options,
|
---|
312 | {
|
---|
313 | chunkName,
|
---|
314 | groupOptions,
|
---|
315 | include,
|
---|
316 | exclude,
|
---|
317 | mode,
|
---|
318 | namespaceObject: /** @type {BuildMeta} */ (
|
---|
319 | parser.state.module.buildMeta
|
---|
320 | ).strictHarmonyModule
|
---|
321 | ? "strict"
|
---|
322 | : true,
|
---|
323 | typePrefix: "import()",
|
---|
324 | category: "esm",
|
---|
325 | referencedExports: exports,
|
---|
326 | attributes: getImportAttributes(expr)
|
---|
327 | },
|
---|
328 | parser
|
---|
329 | );
|
---|
330 | if (!dep) return;
|
---|
331 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
332 | dep.optional = Boolean(parser.scope.inTry);
|
---|
333 | parser.state.current.addDependency(dep);
|
---|
334 | return true;
|
---|
335 | });
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | module.exports = ImportParserPlugin;
|
---|