webpack/lib/dependencies/ImportParserPlugin.js

377 lines
12 KiB
JavaScript
Raw Normal View History

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
"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");
2024-10-31 22:43:31 +08:00
const { getImportAttributes } = require("../javascript/JavascriptParser");
2018-07-30 23:08:51 +08:00
const ContextDependencyHelpers = require("./ContextDependencyHelpers");
2017-10-16 18:52:04 +08:00
const ImportContextDependency = require("./ImportContextDependency");
const ImportDependency = require("./ImportDependency");
const ImportEagerDependency = require("./ImportEagerDependency");
2018-07-30 23:08:51 +08:00
const ImportWeakDependency = require("./ImportWeakDependency");
2016-12-03 18:36:10 +08:00
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
/** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
/** @typedef {import("../ContextModule").ContextMode} ContextMode */
2023-06-17 03:44:20 +08:00
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
2023-05-22 06:28:57 +08:00
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
2024-11-01 00:31:45 +08:00
/** @typedef {import("../javascript/JavascriptParser").ImportExpression} ImportExpression */
2023-06-17 03:44:20 +08:00
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
const PLUGIN_NAME = "ImportParserPlugin";
class ImportParserPlugin {
/**
* @param {JavascriptParserOptions} options options
*/
constructor(options) {
this.options = options;
}
2016-12-03 18:36:10 +08:00
2023-05-22 06:28:57 +08:00
/**
* @param {JavascriptParser} parser the parser
* @returns {void}
*/
apply(parser) {
2023-06-17 03:44:20 +08:00
/**
* @template T
* @param {Iterable<T>} enumerable enumerable
* @returns {T[][]} array of array
*/
const exportsFromEnumerable = (enumerable) =>
Array.from(enumerable, (e) => [e]);
parser.hooks.collectDestructuringAssignmentProperties.tap(
PLUGIN_NAME,
(expr) => {
if (expr.type === "ImportExpression") return true;
}
);
parser.hooks.importCall.tap(PLUGIN_NAME, (expr) => {
2019-08-14 20:42:07 +08:00
const param = parser.evaluateExpression(expr.source);
let chunkName = null;
2023-06-17 03:44:20 +08:00
let mode = /** @type {ContextMode} */ (this.options.dynamicImportMode);
let include = null;
let exclude = null;
2020-06-10 19:31:01 +08:00
/** @type {string[][] | null} */
let exports = null;
/** @type {RawChunkGroupOptions} */
const groupOptions = {};
const {
dynamicImportPreload,
dynamicImportPrefetch,
dynamicImportFetchPriority
} = this.options;
if (
dynamicImportPreload !== undefined &&
dynamicImportPreload !== false
) {
2022-03-15 21:42:52 +08:00
groupOptions.preloadOrder =
dynamicImportPreload === true ? 0 : dynamicImportPreload;
}
2023-04-23 04:42:55 +08:00
if (
dynamicImportPrefetch !== undefined &&
dynamicImportPrefetch !== false
) {
2023-04-23 04:42:55 +08:00
groupOptions.prefetchOrder =
dynamicImportPrefetch === true ? 0 : dynamicImportPrefetch;
}
if (
dynamicImportFetchPriority !== undefined &&
dynamicImportFetchPriority !== false
) {
2023-05-24 09:13:01 +08:00
groupOptions.fetchPriority = dynamicImportFetchPriority;
}
2021-05-11 15:31:46 +08:00
const { options: importOptions, errors: commentErrors } =
2023-06-17 03:44:20 +08:00
parser.parseCommentOptions(/** @type {Range} */ (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;
parser.state.module.addWarning(
new CommentCompilationWarning(
2019-06-09 17:23:42 +08:00
`Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
2024-10-16 22:42:26 +08:00
/** @type {DependencyLocation} */ (comment.loc)
2018-05-29 19:17:36 +08:00
)
);
}
}
let phase = expr.phase;
if (!phase && importOptions && importOptions.webpackDefer !== undefined) {
if (typeof importOptions.webpackDefer !== "boolean") {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackDefer\` expected a boolean, but received: ${importOptions.webpackDefer}.`,
/** @type {DependencyLocation} */ (expr.loc)
)
);
} else if (importOptions.webpackDefer) {
phase = "defer";
}
}
if (phase === "defer") {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
"import.defer() is not implemented yet.",
/** @type {DependencyLocation} */ (expr.loc)
)
);
}
2018-02-25 09:00:20 +08:00
if (importOptions) {
2018-08-21 08:26:50 +08:00
if (importOptions.webpackIgnore !== undefined) {
if (typeof importOptions.webpackIgnore !== "boolean") {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
2019-06-09 17:23:42 +08:00
`\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
2023-06-17 03:44:20 +08:00
/** @type {DependencyLocation} */ (expr.loc)
)
);
2024-07-31 04:54:55 +08:00
} else if (importOptions.webpackIgnore) {
2018-09-22 05:24:18 +08:00
// Do not instrument `import()` if `webpackIgnore` is `true`
2024-07-31 04:54:55 +08:00
return false;
}
}
2018-08-21 08:26:50 +08:00
if (importOptions.webpackChunkName !== undefined) {
if (typeof importOptions.webpackChunkName !== "string") {
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}.`,
2023-06-17 03:44:20 +08:00
/** @type {DependencyLocation} */ (expr.loc)
2018-02-25 09:00:20 +08:00
)
);
} else {
chunkName = importOptions.webpackChunkName;
}
}
2018-08-21 08:26:50 +08:00
if (importOptions.webpackMode !== undefined) {
if (typeof importOptions.webpackMode !== "string") {
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}.`,
2023-06-17 03:44:20 +08:00
/** @type {DependencyLocation} */ (expr.loc)
2018-02-25 09:00:20 +08:00
)
);
} else {
2024-10-01 03:05:27 +08:00
mode = /** @type {ContextMode} */ (importOptions.webpackMode);
}
}
2018-08-21 08:26:50 +08:00
if (importOptions.webpackPrefetch !== undefined) {
if (importOptions.webpackPrefetch === true) {
2018-04-17 00:00:34 +08:00
groupOptions.prefetchOrder = 0;
} else if (typeof importOptions.webpackPrefetch === "number") {
2018-04-17 00:00:34 +08:00
groupOptions.prefetchOrder = importOptions.webpackPrefetch;
} else {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
2019-06-09 17:23:42 +08:00
`\`webpackPrefetch\` expected true or a number, but received: ${importOptions.webpackPrefetch}.`,
2023-06-17 03:44:20 +08:00
/** @type {DependencyLocation} */ (expr.loc)
)
);
}
}
2018-08-21 08:26:50 +08:00
if (importOptions.webpackPreload !== undefined) {
if (importOptions.webpackPreload === true) {
2018-04-17 00:00:34 +08:00
groupOptions.preloadOrder = 0;
} else if (typeof importOptions.webpackPreload === "number") {
2018-04-17 00:00:34 +08:00
groupOptions.preloadOrder = importOptions.webpackPreload;
} else {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
2019-06-09 17:23:42 +08:00
`\`webpackPreload\` expected true or a number, but received: ${importOptions.webpackPreload}.`,
2023-06-17 03:44:20 +08:00
/** @type {DependencyLocation} */ (expr.loc)
)
);
}
}
if (importOptions.webpackFetchPriority !== undefined) {
2023-05-24 09:13:01 +08:00
if (
2023-04-23 05:32:14 +08:00
typeof importOptions.webpackFetchPriority === "string" &&
["high", "low", "auto"].includes(importOptions.webpackFetchPriority)
) {
2024-10-01 03:05:27 +08:00
groupOptions.fetchPriority =
/** @type {"low" | "high" | "auto"} */
(importOptions.webpackFetchPriority);
2023-04-23 05:32:14 +08:00
} else {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
2023-04-23 05:32:14 +08:00
`\`webpackFetchPriority\` expected true or "low", "high" or "auto", but received: ${importOptions.webpackFetchPriority}.`,
2023-06-17 03:44:20 +08:00
/** @type {DependencyLocation} */ (expr.loc)
)
);
}
}
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
) {
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}.`,
2023-06-17 03:44:20 +08:00
/** @type {DependencyLocation} */ (expr.loc)
2018-02-25 09:00:20 +08:00
)
);
} else {
2022-11-22 11:37:16 +08:00
include = importOptions.webpackInclude;
}
}
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
) {
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}.`,
2023-06-17 03:44:20 +08:00
/** @type {DependencyLocation} */ (expr.loc)
2018-02-25 09:00:20 +08:00
)
);
} else {
2022-11-22 11:37:16 +08:00
exclude = importOptions.webpackExclude;
}
}
if (importOptions.webpackExports !== undefined) {
if (
!(
typeof importOptions.webpackExports === "string" ||
(Array.isArray(importOptions.webpackExports) &&
2023-06-17 03:44:20 +08:00
/** @type {string[]} */ (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}.`,
2023-06-17 03:44:20 +08:00
/** @type {DependencyLocation} */ (expr.loc)
)
);
2024-07-31 04:54:55 +08:00
} else if (typeof importOptions.webpackExports === "string") {
exports = [[importOptions.webpackExports]];
} else {
2024-07-31 04:54:55 +08:00
exports = exportsFromEnumerable(importOptions.webpackExports);
}
}
}
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}.`,
2023-06-17 03:44:20 +08:00
/** @type {DependencyLocation} */ (expr.loc)
2022-03-15 23:34:05 +08:00
)
);
mode = "lazy";
}
const referencedPropertiesInDestructuring =
parser.destructuringAssignmentPropertiesFor(expr);
if (referencedPropertiesInDestructuring) {
if (exports) {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
2024-07-31 12:23:44 +08:00
"`webpackExports` could not be used with destructuring assignment.",
2023-06-17 03:44:20 +08:00
/** @type {DependencyLocation} */ (expr.loc)
)
);
}
2024-04-13 02:40:28 +08:00
exports = exportsFromEnumerable(
[...referencedPropertiesInDestructuring].map(({ id }) => id)
);
}
2022-03-15 23:34:05 +08:00
if (param.isString()) {
2024-10-31 22:43:31 +08:00
const attributes = getImportAttributes(expr);
2024-03-15 22:24:33 +08:00
2018-02-25 09:00:20 +08:00
if (mode === "eager") {
const dep = new ImportEagerDependency(
2023-06-17 03:44:20 +08:00
/** @type {string} */ (param.string),
/** @type {Range} */ (expr.range),
2024-03-15 22:24:33 +08:00
exports,
attributes
);
parser.state.current.addDependency(dep);
2018-02-25 09:00:20 +08:00
} else if (mode === "weak") {
const dep = new ImportWeakDependency(
2023-06-17 03:44:20 +08:00
/** @type {string} */ (param.string),
/** @type {Range} */ (expr.range),
2024-03-15 22:24:33 +08:00
exports,
attributes
);
parser.state.current.addDependency(dep);
} else {
2020-06-10 19:31:01 +08:00
const depBlock = new AsyncDependenciesBlock(
{
...groupOptions,
name: chunkName
},
2023-06-17 03:44:20 +08:00
/** @type {DependencyLocation} */ (expr.loc),
2020-06-10 19:31:01 +08:00
param.string
2018-02-25 09:00:20 +08:00
);
2023-06-17 03:44:20 +08:00
const dep = new ImportDependency(
/** @type {string} */ (param.string),
/** @type {Range} */ (expr.range),
2024-03-15 22:24:33 +08:00
exports,
attributes
2023-06-17 03:44:20 +08:00
);
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
2024-07-31 11:11:11 +08:00
dep.optional = Boolean(parser.scope.inTry);
depBlock.addDependency(dep);
parser.state.current.addBlock(depBlock);
}
return true;
}
2024-07-31 04:21:27 +08:00
if (mode === "weak") {
mode = "async-weak";
}
const dep = ContextDependencyHelpers.create(
ImportContextDependency,
/** @type {Range} */ (expr.range),
param,
expr,
this.options,
{
chunkName,
groupOptions,
include,
exclude,
mode,
2025-07-08 22:46:17 +08:00
namespaceObject:
/** @type {BuildMeta} */
(parser.state.module.buildMeta).strictHarmonyModule
? "strict"
: true,
2024-07-31 04:21:27 +08:00
typePrefix: "import()",
category: "esm",
referencedExports: exports,
2024-10-31 22:43:31 +08:00
attributes: getImportAttributes(expr)
2024-07-31 04:21:27 +08:00
},
parser
);
if (!dep) return;
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
2024-07-31 11:11:11 +08:00
dep.optional = Boolean(parser.scope.inTry);
2024-07-31 04:21:27 +08:00
parser.state.current.addDependency(dep);
return true;
});
}
}
module.exports = ImportParserPlugin;