webpack/lib/dependencies/ImportDependency.js

152 lines
4.7 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
2018-07-30 23:08:51 +08:00
const Dependency = require("../Dependency");
const makeSerializable = require("../util/makeSerializable");
const ModuleDependency = require("./ModuleDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
2020-06-10 19:31:01 +08:00
/** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
2025-09-11 08:10:10 +08:00
/** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
/** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
2023-06-17 03:44:20 +08:00
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
2024-06-11 00:21:03 +08:00
/** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
2023-05-22 04:31:30 +08:00
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
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 */
class ImportDependency extends ModuleDependency {
2020-06-10 19:31:01 +08:00
/**
* @param {string} request the request
2023-05-22 04:31:30 +08:00
* @param {Range} range expression range
2025-09-11 08:10:10 +08:00
* @param {RawReferencedExports | null=} referencedExports list of referenced exports
2024-06-11 00:21:03 +08:00
* @param {ImportAttributes=} attributes import attributes
2020-06-10 19:31:01 +08:00
*/
constructor(request, range, referencedExports, attributes) {
super(request);
2020-06-10 19:31:01 +08:00
this.range = range;
this.referencedExports = referencedExports;
this.attributes = attributes;
}
get type() {
return "import()";
}
2020-05-31 17:48:45 +08:00
get category() {
return "esm";
}
/**
* @returns {string | null} an identifier to merge equal requests
*/
getResourceIdentifier() {
let str = super.getResourceIdentifier();
if (this.attributes) {
str += `|importAttributes${JSON.stringify(this.attributes)}`;
}
return str;
}
/**
* 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
*/
getReferencedExports(moduleGraph, runtime) {
if (!this.referencedExports) return Dependency.EXPORTS_OBJECT_REFERENCED;
const refs = [];
for (const referencedExport of this.referencedExports) {
if (referencedExport[0] === "default") {
2024-03-18 01:15:44 +08:00
const selfModule =
/** @type {Module} */
(moduleGraph.getParentModule(this));
const importedModule =
/** @type {Module} */
(moduleGraph.getModule(this));
const exportsType = importedModule.getExportsType(
moduleGraph,
/** @type {BuildMeta} */
(selfModule.buildMeta).strictHarmonyModule
);
if (
exportsType === "default-only" ||
exportsType === "default-with-named"
) {
return Dependency.EXPORTS_OBJECT_REFERENCED;
}
}
refs.push({
name: referencedExport,
canMangle: false
});
}
return refs;
}
2020-06-10 19:31:01 +08:00
2023-04-12 03:22:51 +08:00
/**
* @param {ObjectSerializerContext} context context
*/
2020-06-10 19:31:01 +08:00
serialize(context) {
context.write(this.range);
context.write(this.referencedExports);
context.write(this.attributes);
2020-06-10 19:31:01 +08:00
super.serialize(context);
}
2023-04-12 03:22:51 +08:00
/**
* @param {ObjectDeserializerContext} context context
*/
2020-06-10 19:31:01 +08:00
deserialize(context) {
this.range = context.read();
this.referencedExports = context.read();
this.attributes = context.read();
2020-06-10 19:31:01 +08:00
super.deserialize(context);
}
}
2018-10-18 03:22:41 +08:00
makeSerializable(ImportDependency, "webpack/lib/dependencies/ImportDependency");
2020-11-26 17:52:55 +08:00
ImportDependency.Template = class ImportDependencyTemplate 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,
2018-11-17 01:18:44 +08:00
{ runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
) {
const dep = /** @type {ImportDependency} */ (dependency);
2021-05-11 15:31:46 +08:00
const block = /** @type {AsyncDependenciesBlock} */ (
moduleGraph.getParentBlock(dep)
);
const content = runtimeTemplate.moduleNamespacePromise({
chunkGraph,
2024-07-31 04:09:42 +08:00
block,
2023-06-17 03:44:20 +08:00
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
request: dep.request,
2023-06-17 03:44:20 +08:00
strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
2018-11-17 01:18:44 +08:00
message: "import()",
runtimeRequirements
});
2020-06-10 19:31:01 +08:00
source.replace(dep.range[0], dep.range[1] - 1, content);
}
2017-01-11 17:51:58 +08:00
};
module.exports = ImportDependency;