webpack/lib/dependencies/ImportDependency.js

102 lines
2.9 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 */
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
class ImportDependency extends ModuleDependency {
2020-06-10 19:31:01 +08:00
/**
* @param {string} request the request
* @param {[number, number]} range expression range
* @param {string[][]=} referencedExports list of referenced exports
*/
constructor(request, range, referencedExports) {
super(request);
2020-06-10 19:31:01 +08:00
this.range = range;
this.referencedExports = referencedExports;
}
get type() {
return "import()";
}
2020-05-31 17:48:45 +08:00
get category() {
return "esm";
}
/**
* Returns list of exports referenced by this dependency
* @param {ModuleGraph} moduleGraph module graph
* @param {RuntimeSpec} runtime the runtime for which the module is analysed
* @returns {(string[] | ReferencedExport)[]} referenced exports
*/
getReferencedExports(moduleGraph, runtime) {
return this.referencedExports
2020-06-10 19:31:01 +08:00
? this.referencedExports.map(e => ({
name: e,
canMangle: false
}))
: Dependency.EXPORTS_OBJECT_REFERENCED;
}
2020-06-10 19:31:01 +08:00
serialize(context) {
context.write(this.range);
context.write(this.referencedExports);
super.serialize(context);
}
deserialize(context) {
this.range = context.read();
this.referencedExports = context.read();
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,
block: block,
module: moduleGraph.getModule(dep),
request: dep.request,
strict: 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;