webpack/lib/dependencies/HarmonyImportDependency.js

429 lines
13 KiB
JavaScript
Raw Normal View History

2015-01-13 00:45:30 +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-11 17:51:58 +08:00
"use strict";
2018-04-04 15:17:10 +08:00
const ConditionalInitFragment = require("../ConditionalInitFragment");
const Dependency = require("../Dependency");
const HarmonyLinkingError = require("../HarmonyLinkingError");
const InitFragment = require("../InitFragment");
2018-07-30 23:08:51 +08:00
const Template = require("../Template");
2019-06-05 20:17:15 +08:00
const AwaitDependenciesInitFragment = require("../async-modules/AwaitDependenciesInitFragment");
const { filterRuntime, mergeRuntime } = require("../util/runtime");
const ModuleDependency = require("./ModuleDependency");
2015-01-13 00:45:30 +08:00
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
2025-09-11 08:10:10 +08:00
/** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
2024-03-18 23:28:40 +08:00
/** @typedef {import("../ExportsInfo")} ExportsInfo */
/** @typedef {import("../Module")} Module */
2024-03-18 23:28:40 +08:00
/** @typedef {import("../Module").BuildMeta} BuildMeta */
2018-07-17 22:42:05 +08:00
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../WebpackError")} WebpackError */
2024-06-11 00:21:03 +08:00
/** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
2023-04-12 03:22:51 +08:00
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
2025-04-07 21:09:05 +08:00
/** @typedef {0 | 1 | 2 | 3 | false} ExportPresenceMode */
const ExportPresenceModes = {
2025-04-07 21:09:05 +08:00
NONE: /** @type {ExportPresenceMode} */ (0),
WARN: /** @type {ExportPresenceMode} */ (1),
AUTO: /** @type {ExportPresenceMode} */ (2),
ERROR: /** @type {ExportPresenceMode} */ (3),
2024-03-18 23:28:40 +08:00
/**
* @param {string | false} str param
2025-04-07 21:09:05 +08:00
* @returns {ExportPresenceMode} result
2024-03-18 23:28:40 +08:00
*/
fromUserOption(str) {
switch (str) {
case "error":
return ExportPresenceModes.ERROR;
case "warn":
return ExportPresenceModes.WARN;
case "auto":
return ExportPresenceModes.AUTO;
case false:
return ExportPresenceModes.NONE;
default:
throw new Error(`Invalid export presence value ${str}`);
}
}
};
2025-09-11 08:10:10 +08:00
/** @typedef {string[]} Ids */
class HarmonyImportDependency extends ModuleDependency {
/**
* @param {string} request request string
* @param {number} sourceOrder source order
2024-06-11 00:21:03 +08:00
* @param {ImportAttributes=} attributes import attributes
* @param {boolean=} defer import attributes
*/
constructor(request, sourceOrder, attributes, defer) {
super(request);
this.sourceOrder = sourceOrder;
this.attributes = attributes;
this.defer = defer;
}
2020-05-27 04:56:24 +08:00
get category() {
return "esm";
2020-05-27 04:56:24 +08:00
}
/**
* @returns {string | null} an identifier to merge equal requests
*/
getResourceIdentifier() {
let str = super.getResourceIdentifier();
if (this.defer) {
str += "|defer";
}
if (this.attributes) {
str += `|importAttributes${JSON.stringify(this.attributes)}`;
}
return str;
}
2018-07-25 15:33:48 +08:00
/**
* Returns list of exports referenced by this dependency
* @param {ModuleGraph} moduleGraph module graph
* @param {RuntimeSpec} runtime the runtime for which the module is analysed
2025-09-11 08:10:10 +08:00
* @returns {ReferencedExports} referenced exports
2018-07-25 15:33:48 +08:00
*/
getReferencedExports(moduleGraph, runtime) {
2019-10-30 14:57:55 +08:00
return Dependency.NO_EXPORTS_REFERENCED;
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {string} name of the variable for the import
*/
getImportVar(moduleGraph) {
2024-08-15 02:38:08 +08:00
const module = /** @type {Module} */ (moduleGraph.getParentModule(this));
const meta = moduleGraph.getMeta(module);
const defer = this.defer;
const metaKey = defer ? "deferredImportVarMap" : "importVarMap";
let importVarMap = meta[metaKey];
2025-08-28 18:34:30 +08:00
if (!importVarMap) {
meta[metaKey] = importVarMap =
/** @type {Map<Module, string>} */
(new Map());
}
2024-03-18 23:28:40 +08:00
let importVar = importVarMap.get(
2025-08-28 18:34:30 +08:00
/** @type {Module} */
(moduleGraph.getModule(this))
2024-03-18 23:28:40 +08:00
);
2018-02-25 09:00:20 +08:00
if (importVar) return importVar;
importVar = `${Template.toIdentifier(
`${this.userRequest}`
)}__WEBPACK_${this.defer ? "DEFERRED_" : ""}IMPORTED_MODULE_${importVarMap.size}__`;
2024-03-18 23:28:40 +08:00
importVarMap.set(
2025-08-28 18:34:30 +08:00
/** @type {Module} */
(moduleGraph.getModule(this)),
2024-03-18 23:28:40 +08:00
importVar
);
return importVar;
}
/**
* @param {DependencyTemplateContext} context the template context
* @returns {string} the expression
*/
getModuleExports({
runtimeTemplate,
moduleGraph,
chunkGraph,
runtimeRequirements
}) {
return runtimeTemplate.moduleExports({
module: moduleGraph.getModule(this),
chunkGraph,
request: this.request,
runtimeRequirements
});
}
/**
* @param {boolean} update create new variables or update existing one
* @param {DependencyTemplateContext} templateContext the template context
* @returns {[string, string]} the import statement and the compat statement
*/
getImportStatement(
update,
{ runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
) {
return runtimeTemplate.importStatement({
update,
2024-03-18 23:28:40 +08:00
module: /** @type {Module} */ (moduleGraph.getModule(this)),
moduleGraph,
chunkGraph,
importVar: this.getImportVar(moduleGraph),
request: this.request,
2018-11-17 01:18:44 +08:00
originModule: module,
runtimeRequirements,
defer: this.defer
});
}
/**
* @param {ModuleGraph} moduleGraph module graph
2025-09-11 08:10:10 +08:00
* @param {Ids} ids imported ids
* @param {string} additionalMessage extra info included in the error message
* @returns {WebpackError[] | undefined} errors
*/
getLinkingErrors(moduleGraph, ids, additionalMessage) {
const importedModule = moduleGraph.getModule(this);
// ignore errors for missing or failed modules
if (!importedModule || importedModule.getNumberOfErrors() > 0) {
return;
}
2024-03-18 23:28:40 +08:00
const parentModule =
/** @type {Module} */
(moduleGraph.getParentModule(this));
const exportsType = importedModule.getExportsType(
moduleGraph,
2024-03-18 23:28:40 +08:00
/** @type {BuildMeta} */ (parentModule.buildMeta).strictHarmonyModule
);
if (exportsType === "namespace" || exportsType === "default-with-named") {
if (ids.length === 0) {
return;
}
if (
(exportsType !== "default-with-named" || ids[0] !== "default") &&
moduleGraph.isExportProvided(importedModule, ids) === false
) {
// We are sure that it's not provided
// Try to provide detailed info in the error message
let pos = 0;
let exportsInfo = moduleGraph.getExportsInfo(importedModule);
while (pos < ids.length && exportsInfo) {
const id = ids[pos++];
const exportInfo = exportsInfo.getReadOnlyExportInfo(id);
if (exportInfo.provided === false) {
// We are sure that it's not provided
const providedExports = exportsInfo.getProvidedExports();
const moreInfo = !Array.isArray(providedExports)
? " (possible exports unknown)"
: providedExports.length === 0
2024-07-30 22:21:50 +08:00
? " (module has no exports)"
: ` (possible exports: ${providedExports.join(", ")})`;
return [
new HarmonyLinkingError(
`export ${ids
.slice(0, pos)
.map((id) => `'${id}'`)
.join(".")} ${additionalMessage} was not found in '${
this.userRequest
}'${moreInfo}`
)
];
}
2024-03-18 23:28:40 +08:00
exportsInfo =
/** @type {ExportsInfo} */
(exportInfo.getNestedExportsInfo());
}
// General error message
return [
new HarmonyLinkingError(
`export ${ids
.map((id) => `'${id}'`)
.join(".")} ${additionalMessage} was not found in '${
this.userRequest
}'`
)
];
}
}
switch (exportsType) {
case "default-only":
// It's has only a default export
if (ids.length > 0 && ids[0] !== "default") {
// In strict harmony modules we only support the default export
return [
new HarmonyLinkingError(
`Can't import the named export ${ids
.map((id) => `'${id}'`)
.join(
"."
)} ${additionalMessage} from default-exporting module (only default export is available)`
)
];
}
break;
case "default-with-named":
// It has a default export and named properties redirect
// In some cases we still want to warn here
if (
ids.length > 0 &&
ids[0] !== "default" &&
2024-03-18 23:28:40 +08:00
/** @type {BuildMeta} */
(importedModule.buildMeta).defaultObject === "redirect-warn"
) {
// For these modules only the default export is supported
return [
new HarmonyLinkingError(
`Should not import the named export ${ids
.map((id) => `'${id}'`)
.join(
"."
)} ${additionalMessage} from default-exporting module (only default export is available soon)`
)
];
}
break;
}
}
2023-04-12 03:22:51 +08:00
/**
* @param {ObjectSerializerContext} context context
*/
2018-10-09 20:30:59 +08:00
serialize(context) {
const { write } = context;
write(this.sourceOrder);
write(this.attributes);
write(this.defer);
2018-10-09 20:30:59 +08:00
super.serialize(context);
}
2023-04-12 03:22:51 +08:00
/**
* @param {ObjectDeserializerContext} context context
*/
2018-10-09 20:30:59 +08:00
deserialize(context) {
const { read } = context;
this.sourceOrder = read();
this.attributes = read();
this.defer = read();
2018-10-09 20:30:59 +08:00
super.deserialize(context);
}
}
module.exports = HarmonyImportDependency;
/** @type {WeakMap<Module, WeakMap<Module, RuntimeSpec | boolean>>} */
const importEmittedMap = new WeakMap();
2020-11-26 17:52:55 +08:00
HarmonyImportDependency.Template = class HarmonyImportDependencyTemplate extends (
ModuleDependency.Template
) {
/**
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {ReplaceSource} source the current replace source which can be modified
* @param {DependencyTemplateContext} templateContext the context object
* @returns {void}
*/
apply(dependency, source, templateContext) {
2018-07-27 17:45:12 +08:00
const dep = /** @type {HarmonyImportDependency} */ (dependency);
const { module, chunkGraph, moduleGraph, runtime } = templateContext;
2018-07-27 17:45:12 +08:00
const connection = moduleGraph.getConnection(dep);
2020-10-07 15:10:29 +08:00
if (connection && !connection.isTargetActive(runtime)) return;
const referencedModule = connection && connection.module;
if (
connection &&
connection.weak &&
referencedModule &&
chunkGraph.getModuleId(referencedModule) === null
) {
// in weak references, module might not be in any chunk
// but that's ok, we don't need that logic in this case
return;
}
const moduleKey = referencedModule
? referencedModule.identifier()
: dep.request;
const key = `${dep.defer ? "deferred " : ""}harmony import ${moduleKey}`;
const runtimeCondition = dep.weak
? false
: connection
? filterRuntime(runtime, (r) => connection.isTargetActive(r))
2024-07-30 22:21:50 +08:00
: true;
if (module && referencedModule) {
let emittedModules = importEmittedMap.get(module);
if (emittedModules === undefined) {
emittedModules = new WeakMap();
importEmittedMap.set(module, emittedModules);
}
let mergedRuntimeCondition = runtimeCondition;
const oldRuntimeCondition = emittedModules.get(referencedModule) || false;
if (oldRuntimeCondition !== false && mergedRuntimeCondition !== true) {
if (mergedRuntimeCondition === false || oldRuntimeCondition === true) {
mergedRuntimeCondition = oldRuntimeCondition;
} else {
mergedRuntimeCondition = mergeRuntime(
oldRuntimeCondition,
mergedRuntimeCondition
);
}
}
emittedModules.set(referencedModule, mergedRuntimeCondition);
}
const importStatement = dep.getImportStatement(false, templateContext);
if (
referencedModule &&
templateContext.moduleGraph.isAsync(referencedModule)
) {
templateContext.initFragments.push(
new ConditionalInitFragment(
importStatement[0],
InitFragment.STAGE_HARMONY_IMPORTS,
dep.sourceOrder,
key,
runtimeCondition
)
);
const importVar = dep.getImportVar(templateContext.moduleGraph);
templateContext.initFragments.push(
new AwaitDependenciesInitFragment(new Map([[importVar, importVar]]))
);
templateContext.initFragments.push(
new ConditionalInitFragment(
importStatement[1],
InitFragment.STAGE_ASYNC_HARMONY_IMPORTS,
dep.sourceOrder,
2024-07-31 10:39:30 +08:00
`${key} compat`,
runtimeCondition
)
);
} else {
templateContext.initFragments.push(
new ConditionalInitFragment(
importStatement[0] + importStatement[1],
InitFragment.STAGE_HARMONY_IMPORTS,
dep.sourceOrder,
key,
runtimeCondition
)
);
}
}
/**
* @param {Module} module the module
* @param {Module} referencedModule the referenced module
* @returns {RuntimeSpec | boolean} runtimeCondition in which this import has been emitted
*/
static getImportEmittedRuntime(module, referencedModule) {
const emittedModules = importEmittedMap.get(module);
if (emittedModules === undefined) return false;
return emittedModules.get(referencedModule) || false;
2015-01-13 00:45:30 +08:00
}
};
module.exports.ExportPresenceModes = ExportPresenceModes;