webpack/lib/dependencies/ProvidedDependency.js

156 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-07-25 02:05:29 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Florent Cailhol @ooflorent
*/
"use strict";
2022-04-04 22:59:01 +08:00
const Dependency = require("../Dependency");
2018-07-25 02:05:29 +08:00
const InitFragment = require("../InitFragment");
2018-10-09 20:30:59 +08:00
const makeSerializable = require("../util/makeSerializable");
2018-07-25 02:05:29 +08:00
const ModuleDependency = require("./ModuleDependency");
2018-07-27 17:45:12 +08:00
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
2025-09-11 08:10:10 +08:00
/** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
2018-07-17 22:42:05 +08:00
/** @typedef {import("../ModuleGraph")} ModuleGraph */
2023-06-17 02:49:43 +08:00
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
2025-09-11 08:10:10 +08:00
/** @typedef {import("../ExportsInfo").ExportInfoName} ExportInfoName */
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 */
2019-07-17 22:02:33 +08:00
/** @typedef {import("../util/Hash")} Hash */
2022-04-04 22:59:01 +08:00
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
2018-07-25 03:57:48 +08:00
/**
2025-09-11 08:10:10 +08:00
* @param {string[] | null} path the property path array
* @returns {string} the converted path
2018-07-25 03:57:48 +08:00
*/
const pathToString = (path) =>
path !== null && path.length > 0
? path.map((part) => `[${JSON.stringify(part)}]`).join("")
: "";
2018-07-25 03:57:48 +08:00
2018-07-25 02:05:29 +08:00
class ProvidedDependency extends ModuleDependency {
2022-04-04 22:59:01 +08:00
/**
* @param {string} request request
* @param {string} identifier identifier
2025-09-11 08:10:10 +08:00
* @param {ExportInfoName[]} ids ids
2023-05-22 04:31:30 +08:00
* @param {Range} range range
2022-04-04 22:59:01 +08:00
*/
constructor(request, identifier, ids, range) {
2018-07-25 02:05:29 +08:00
super(request);
this.identifier = identifier;
2022-04-04 22:59:01 +08:00
this.ids = ids;
2018-07-25 02:05:29 +08:00
this.range = range;
this._hashUpdate = undefined;
2018-07-25 02:05:29 +08:00
}
get type() {
return "provided";
}
get category() {
return "esm";
}
2022-04-04 22:59:01 +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
2022-04-04 22:59:01 +08:00
*/
getReferencedExports(moduleGraph, runtime) {
2024-07-31 04:09:42 +08:00
const ids = this.ids;
2022-04-04 22:59:01 +08:00
if (ids.length === 0) return Dependency.EXPORTS_OBJECT_REFERENCED;
return [ids];
}
/**
* Update the hash
* @param {Hash} hash hash to be updated
* @param {UpdateHashContext} context context
* @returns {void}
*/
updateHash(hash, context) {
if (this._hashUpdate === undefined) {
2022-04-11 19:18:55 +08:00
this._hashUpdate = this.identifier + (this.ids ? this.ids.join(",") : "");
}
hash.update(this._hashUpdate);
}
2018-10-09 20:30:59 +08:00
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.identifier);
2022-04-04 22:59:01 +08:00
write(this.ids);
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.identifier = read();
2022-04-04 22:59:01 +08:00
this.ids = read();
2018-10-09 20:30:59 +08:00
super.deserialize(context);
}
2018-07-25 02:05:29 +08:00
}
2018-10-09 20:30:59 +08:00
makeSerializable(
ProvidedDependency,
"webpack/lib/dependencies/ProvidedDependency"
);
class ProvidedDependencyTemplate extends ModuleDependency.Template {
2018-07-27 17:45:12 +08:00
/**
* @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
2018-07-27 17:45:12 +08:00
* @returns {void}
*/
apply(
dependency,
source,
2018-11-17 01:18:44 +08:00
{
2022-04-04 22:59:01 +08:00
runtime,
2018-11-17 01:18:44 +08:00
runtimeTemplate,
moduleGraph,
chunkGraph,
initFragments,
runtimeRequirements
}
) {
2018-07-27 17:45:12 +08:00
const dep = /** @type {ProvidedDependency} */ (dependency);
2023-06-17 02:49:43 +08:00
const connection =
/** @type {ModuleGraphConnection} */
(moduleGraph.getConnection(dep));
2022-04-04 22:59:01 +08:00
const exportsInfo = moduleGraph.getExportsInfo(connection.module);
2022-04-11 19:18:55 +08:00
const usedName = exportsInfo.getUsedName(dep.ids, runtime);
initFragments.push(
2018-07-25 02:05:29 +08:00
new InitFragment(
`/* provided dependency */ var ${
dep.identifier
} = ${runtimeTemplate.moduleExports({
module: moduleGraph.getModule(dep),
chunkGraph,
2018-11-17 01:18:44 +08:00
request: dep.request,
runtimeRequirements
2025-09-11 08:10:10 +08:00
})}${pathToString(/** @type {string[] | null} */ (usedName))};\n`,
2018-07-30 16:15:18 +08:00
InitFragment.STAGE_PROVIDES,
1,
`provided ${dep.identifier}`
2018-07-25 02:05:29 +08:00
)
);
source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
2018-07-25 02:05:29 +08:00
}
}
ProvidedDependency.Template = ProvidedDependencyTemplate;
2018-07-25 02:05:29 +08:00
module.exports = ProvidedDependency;