2016-12-03 18:36:10 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-01-12 01:58:05 +08:00
|
|
|
"use strict";
|
2016-12-03 18:36:10 +08:00
|
|
|
|
2020-06-10 19:31:01 +08:00
|
|
|
const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
|
2018-07-30 23:08:51 +08:00
|
|
|
const CommentCompilationWarning = require("../CommentCompilationWarning");
|
|
|
|
const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
|
|
|
|
const ContextDependencyHelpers = require("./ContextDependencyHelpers");
|
2017-10-16 18:52:04 +08:00
|
|
|
const ImportContextDependency = require("./ImportContextDependency");
|
2018-10-18 05:09:21 +08:00
|
|
|
const ImportDependency = require("./ImportDependency");
|
2017-05-05 00:37:25 +08:00
|
|
|
const ImportEagerDependency = require("./ImportEagerDependency");
|
2018-07-30 23:08:51 +08:00
|
|
|
const ImportWeakDependency = require("./ImportWeakDependency");
|
2016-12-03 18:36:10 +08:00
|
|
|
|
2022-03-09 15:27:02 +08:00
|
|
|
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
2019-06-14 17:44:54 +08:00
|
|
|
/** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
|
|
|
|
/** @typedef {import("../ContextModule").ContextMode} ContextMode */
|
|
|
|
|
2017-01-12 01:58:05 +08:00
|
|
|
class ImportParserPlugin {
|
2022-03-09 15:27:02 +08:00
|
|
|
/**
|
|
|
|
* @param {JavascriptParserOptions} options options
|
|
|
|
*/
|
2017-01-12 01:58:05 +08:00
|
|
|
constructor(options) {
|
|
|
|
this.options = options;
|
|
|
|
}
|
2016-12-03 18:36:10 +08:00
|
|
|
|
2017-01-12 01:58:05 +08:00
|
|
|
apply(parser) {
|
2018-01-16 04:26:47 +08:00
|
|
|
parser.hooks.importCall.tap("ImportParserPlugin", expr => {
|
2019-08-14 20:42:07 +08:00
|
|
|
const param = parser.evaluateExpression(expr.source);
|
2017-04-10 17:35:32 +08:00
|
|
|
|
2017-03-26 17:07:52 +08:00
|
|
|
let chunkName = null;
|
2019-06-14 17:44:54 +08:00
|
|
|
/** @type {ContextMode} */
|
2022-03-15 21:42:52 +08:00
|
|
|
let mode = this.options.dynamicImportMode;
|
2017-10-14 05:31:15 +08:00
|
|
|
let include = null;
|
2017-10-13 05:35:23 +08:00
|
|
|
let exclude = null;
|
2020-06-10 19:31:01 +08:00
|
|
|
/** @type {string[][] | null} */
|
2020-05-28 02:34:55 +08:00
|
|
|
let exports = null;
|
2019-06-14 17:44:54 +08:00
|
|
|
/** @type {RawChunkGroupOptions} */
|
2018-04-16 16:27:22 +08:00
|
|
|
const groupOptions = {};
|
2017-03-26 17:07:52 +08:00
|
|
|
|
2022-03-15 21:42:52 +08:00
|
|
|
const { dynamicImportPreload, dynamicImportPrefetch } = this.options;
|
|
|
|
if (dynamicImportPreload !== undefined && dynamicImportPreload !== false)
|
|
|
|
groupOptions.preloadOrder =
|
|
|
|
dynamicImportPreload === true ? 0 : dynamicImportPreload;
|
|
|
|
if (
|
|
|
|
dynamicImportPrefetch !== undefined &&
|
|
|
|
dynamicImportPrefetch !== false
|
|
|
|
)
|
|
|
|
groupOptions.prefetchOrder =
|
|
|
|
dynamicImportPrefetch === true ? 0 : dynamicImportPrefetch;
|
2022-03-09 15:27:02 +08:00
|
|
|
|
2021-05-11 15:31:46 +08:00
|
|
|
const { options: importOptions, errors: commentErrors } =
|
|
|
|
parser.parseCommentOptions(expr.range);
|
2018-05-29 19:17:36 +08:00
|
|
|
|
|
|
|
if (commentErrors) {
|
2022-11-22 12:03:27 +08:00
|
|
|
for (const e of commentErrors) {
|
|
|
|
const { comment } = e;
|
2019-11-08 19:43:05 +08:00
|
|
|
parser.state.module.addWarning(
|
2018-05-30 09:24:36 +08:00
|
|
|
new CommentCompilationWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
|
2018-05-29 19:17:36 +08:00
|
|
|
comment.loc
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2018-05-29 15:29:23 +08:00
|
|
|
}
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (importOptions) {
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackIgnore !== undefined) {
|
2018-04-12 23:33:22 +08:00
|
|
|
if (typeof importOptions.webpackIgnore !== "boolean") {
|
2019-11-08 19:43:05 +08:00
|
|
|
parser.state.module.addWarning(
|
2018-04-12 23:33:22 +08:00
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-04-12 23:33:22 +08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
2018-09-22 05:24:18 +08:00
|
|
|
// Do not instrument `import()` if `webpackIgnore` is `true`
|
2018-04-12 23:33:22 +08:00
|
|
|
if (importOptions.webpackIgnore) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackChunkName !== undefined) {
|
2018-04-12 23:33:22 +08:00
|
|
|
if (typeof importOptions.webpackChunkName !== "string") {
|
2019-11-08 19:43:05 +08:00
|
|
|
parser.state.module.addWarning(
|
2018-02-25 09:00:20 +08:00
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
);
|
2018-04-12 23:33:22 +08:00
|
|
|
} else {
|
|
|
|
chunkName = importOptions.webpackChunkName;
|
|
|
|
}
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackMode !== undefined) {
|
2018-04-12 23:33:22 +08:00
|
|
|
if (typeof importOptions.webpackMode !== "string") {
|
2019-11-08 19:43:05 +08:00
|
|
|
parser.state.module.addWarning(
|
2018-02-25 09:00:20 +08:00
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackMode\` expected a string, but received: ${importOptions.webpackMode}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
);
|
2018-04-12 23:33:22 +08:00
|
|
|
} else {
|
|
|
|
mode = importOptions.webpackMode;
|
|
|
|
}
|
2017-03-26 17:07:52 +08:00
|
|
|
}
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackPrefetch !== undefined) {
|
2018-04-16 23:43:13 +08:00
|
|
|
if (importOptions.webpackPrefetch === true) {
|
2018-04-17 00:00:34 +08:00
|
|
|
groupOptions.prefetchOrder = 0;
|
2018-04-16 23:43:13 +08:00
|
|
|
} else if (typeof importOptions.webpackPrefetch === "number") {
|
2018-04-17 00:00:34 +08:00
|
|
|
groupOptions.prefetchOrder = importOptions.webpackPrefetch;
|
2018-04-16 23:43:13 +08:00
|
|
|
} else {
|
2019-11-08 19:43:05 +08:00
|
|
|
parser.state.module.addWarning(
|
2018-04-16 16:27:22 +08:00
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackPrefetch\` expected true or a number, but received: ${importOptions.webpackPrefetch}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-04-16 16:27:22 +08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackPreload !== undefined) {
|
2018-04-16 23:43:13 +08:00
|
|
|
if (importOptions.webpackPreload === true) {
|
2018-04-17 00:00:34 +08:00
|
|
|
groupOptions.preloadOrder = 0;
|
2018-04-16 23:43:13 +08:00
|
|
|
} else if (typeof importOptions.webpackPreload === "number") {
|
2018-04-17 00:00:34 +08:00
|
|
|
groupOptions.preloadOrder = importOptions.webpackPreload;
|
2018-04-16 23:43:13 +08:00
|
|
|
} else {
|
2019-11-08 19:43:05 +08:00
|
|
|
parser.state.module.addWarning(
|
2018-04-16 16:27:22 +08:00
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackPreload\` expected true or a number, but received: ${importOptions.webpackPreload}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-04-16 16:27:22 +08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2017-03-26 17:07:52 +08:00
|
|
|
}
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackInclude !== undefined) {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
|
|
|
!importOptions.webpackInclude ||
|
2022-11-22 11:37:16 +08:00
|
|
|
!(importOptions.webpackInclude instanceof RegExp)
|
2018-02-25 09:00:20 +08:00
|
|
|
) {
|
2019-11-08 19:43:05 +08:00
|
|
|
parser.state.module.addWarning(
|
2018-02-25 09:00:20 +08:00
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackInclude\` expected a regular expression, but received: ${importOptions.webpackInclude}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
);
|
2017-10-12 05:30:54 +08:00
|
|
|
} else {
|
2022-11-22 11:37:16 +08:00
|
|
|
include = importOptions.webpackInclude;
|
2017-10-12 05:30:54 +08:00
|
|
|
}
|
|
|
|
}
|
2018-08-21 08:26:50 +08:00
|
|
|
if (importOptions.webpackExclude !== undefined) {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
|
|
|
!importOptions.webpackExclude ||
|
2022-11-22 11:37:16 +08:00
|
|
|
!(importOptions.webpackExclude instanceof RegExp)
|
2018-02-25 09:00:20 +08:00
|
|
|
) {
|
2019-11-08 19:43:05 +08:00
|
|
|
parser.state.module.addWarning(
|
2018-02-25 09:00:20 +08:00
|
|
|
new UnsupportedFeatureWarning(
|
2019-06-09 17:23:42 +08:00
|
|
|
`\`webpackExclude\` expected a regular expression, but received: ${importOptions.webpackExclude}.`,
|
2018-06-04 16:10:23 +08:00
|
|
|
expr.loc
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
);
|
2017-10-13 05:35:23 +08:00
|
|
|
} else {
|
2022-11-22 11:37:16 +08:00
|
|
|
exclude = importOptions.webpackExclude;
|
2017-10-13 05:35:23 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 02:34:55 +08:00
|
|
|
if (importOptions.webpackExports !== undefined) {
|
|
|
|
if (
|
|
|
|
!(
|
|
|
|
typeof importOptions.webpackExports === "string" ||
|
|
|
|
(Array.isArray(importOptions.webpackExports) &&
|
|
|
|
importOptions.webpackExports.every(
|
|
|
|
item => typeof item === "string"
|
|
|
|
))
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
parser.state.module.addWarning(
|
|
|
|
new UnsupportedFeatureWarning(
|
|
|
|
`\`webpackExports\` expected a string or an array of strings, but received: ${importOptions.webpackExports}.`,
|
|
|
|
expr.loc
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if (typeof importOptions.webpackExports === "string") {
|
2020-06-10 19:31:01 +08:00
|
|
|
exports = [[importOptions.webpackExports]];
|
2020-05-28 02:34:55 +08:00
|
|
|
} else {
|
2020-06-10 19:31:01 +08:00
|
|
|
exports = Array.from(importOptions.webpackExports, e => [e]);
|
2020-05-28 02:34:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-26 17:07:52 +08:00
|
|
|
}
|
|
|
|
|
2022-03-15 23:34:05 +08:00
|
|
|
if (
|
|
|
|
mode !== "lazy" &&
|
|
|
|
mode !== "lazy-once" &&
|
|
|
|
mode !== "eager" &&
|
|
|
|
mode !== "weak"
|
|
|
|
) {
|
|
|
|
parser.state.module.addWarning(
|
|
|
|
new UnsupportedFeatureWarning(
|
|
|
|
`\`webpackMode\` expected 'lazy', 'lazy-once', 'eager' or 'weak', but received: ${mode}.`,
|
|
|
|
expr.loc
|
|
|
|
)
|
|
|
|
);
|
|
|
|
mode = "lazy";
|
|
|
|
}
|
2017-05-05 00:37:25 +08:00
|
|
|
|
2022-03-15 23:34:05 +08:00
|
|
|
if (param.isString()) {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (mode === "eager") {
|
2020-05-28 02:34:55 +08:00
|
|
|
const dep = new ImportEagerDependency(
|
|
|
|
param.string,
|
|
|
|
expr.range,
|
|
|
|
exports
|
|
|
|
);
|
2017-05-05 00:37:25 +08:00
|
|
|
parser.state.current.addDependency(dep);
|
2018-02-25 09:00:20 +08:00
|
|
|
} else if (mode === "weak") {
|
2020-05-28 02:34:55 +08:00
|
|
|
const dep = new ImportWeakDependency(
|
|
|
|
param.string,
|
|
|
|
expr.range,
|
|
|
|
exports
|
|
|
|
);
|
2017-07-26 20:49:37 +08:00
|
|
|
parser.state.current.addDependency(dep);
|
2017-05-05 00:37:25 +08:00
|
|
|
} else {
|
2020-06-10 19:31:01 +08:00
|
|
|
const depBlock = new AsyncDependenciesBlock(
|
2019-06-19 19:16:05 +08:00
|
|
|
{
|
|
|
|
...groupOptions,
|
2018-04-16 16:27:22 +08:00
|
|
|
name: chunkName
|
2019-06-19 19:16:05 +08:00
|
|
|
},
|
2018-02-25 09:00:20 +08:00
|
|
|
expr.loc,
|
2020-06-10 19:31:01 +08:00
|
|
|
param.string
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2020-06-10 19:31:01 +08:00
|
|
|
const dep = new ImportDependency(param.string, expr.range, exports);
|
2018-10-18 05:09:21 +08:00
|
|
|
dep.loc = expr.loc;
|
|
|
|
depBlock.addDependency(dep);
|
2017-05-05 00:37:25 +08:00
|
|
|
parser.state.current.addBlock(depBlock);
|
|
|
|
}
|
2017-01-12 01:58:05 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (mode === "weak") {
|
2017-10-16 20:49:51 +08:00
|
|
|
mode = "async-weak";
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
const dep = ContextDependencyHelpers.create(
|
|
|
|
ImportContextDependency,
|
|
|
|
expr.range,
|
|
|
|
param,
|
|
|
|
expr,
|
|
|
|
this.options,
|
|
|
|
{
|
|
|
|
chunkName,
|
2018-04-16 16:27:22 +08:00
|
|
|
groupOptions,
|
2018-02-25 09:00:20 +08:00
|
|
|
include,
|
|
|
|
exclude,
|
|
|
|
mode,
|
|
|
|
namespaceObject: parser.state.module.buildMeta.strictHarmonyModule
|
|
|
|
? "strict"
|
2020-06-10 19:31:01 +08:00
|
|
|
: true,
|
2021-05-31 19:44:09 +08:00
|
|
|
typePrefix: "import()",
|
2020-06-18 05:03:02 +08:00
|
|
|
category: "esm",
|
2020-06-10 19:31:01 +08:00
|
|
|
referencedExports: exports
|
2018-10-30 19:39:43 +08:00
|
|
|
},
|
|
|
|
parser
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
|
|
|
if (!dep) return;
|
2017-01-12 01:58:05 +08:00
|
|
|
dep.loc = expr.loc;
|
|
|
|
dep.optional = !!parser.scope.inTry;
|
|
|
|
parser.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-16 04:26:47 +08:00
|
|
|
});
|
2017-01-12 01:58:05 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-29 15:29:23 +08:00
|
|
|
|
2017-01-12 01:58:05 +08:00
|
|
|
module.exports = ImportParserPlugin;
|