webpack/lib/dependencies/ProvidedDependency.js

169 lines
4.6 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 */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
2022-04-04 22:59:01 +08:00
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
2018-07-27 17:45:12 +08:00
/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
2018-07-17 22:42:05 +08:00
/** @typedef {import("../ModuleGraph")} ModuleGraph */
2018-07-27 17:45:12 +08:00
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
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 */
const idsSymbol = Symbol("ProvidedDependency.ids");
2018-07-27 17:45:12 +08:00
2018-07-25 03:57:48 +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
* @param {string[]} ids ids
* @param {[number, number]} range range
*/
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
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {string[]} the imported ids
*/
getIds(moduleGraph) {
const meta = moduleGraph.getMetaIfExisting(this);
if (meta === undefined) return this.ids;
const ids = meta[idsSymbol];
return ids !== undefined ? ids : this.ids;
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @param {string[]} ids the imported ids
* @returns {void}
*/
setIds(moduleGraph, ids) {
moduleGraph.getMeta(this)[idsSymbol] = ids;
}
/**
* 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) {
let ids = this.getIds(moduleGraph);
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) {
this._hashUpdate =
2022-04-04 22:59:01 +08:00
this.identifier + (this.ids ? this.ids.join(",") : "null");
}
hash.update(this._hashUpdate);
}
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);
}
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);
2022-04-04 22:59:01 +08:00
const connection = moduleGraph.getConnection(dep);
const exportsInfo = moduleGraph.getExportsInfo(connection.module);
const usedName = exportsInfo.getUsedName(dep.getIds(moduleGraph), 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
2022-04-04 22:59:01 +08:00
})}${pathToString(/** @type {string[]} */ (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;