webpack/lib/dependencies/ProvidedDependency.js

111 lines
3.2 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";
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 */
2018-07-27 17:45:12 +08:00
/** @typedef {import("../Dependency")} Dependency */
/** @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 */
/** @typedef {import("../util/createHash").Hash} Hash */
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 {
constructor(request, identifier, path, range) {
2018-07-25 02:05:29 +08:00
super(request);
this.identifier = identifier;
this.path = path;
2018-07-25 02:05:29 +08:00
this.range = range;
}
/**
* Update the hash
* @param {Hash} hash hash to be updated
* @param {ChunkGraph} chunkGraph chunk graph
* @returns {void}
*/
updateHash(hash, chunkGraph) {
super.updateHash(hash, chunkGraph);
hash.update(this.identifier);
hash.update(this.path ? this.path.join(",") : "null");
}
2018-10-09 20:30:59 +08:00
serialize(context) {
const { write } = context;
write(this.identifier);
write(this.path);
write(this.range);
super.serialize(context);
}
deserialize(context) {
const { read } = context;
this.identifier = read();
this.path = read();
this.range = read();
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, templateContext) {
2018-07-27 17:45:12 +08:00
const dep = /** @type {ProvidedDependency} */ (dependency);
source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
2018-07-25 02:05:29 +08:00
}
2018-07-27 17:45:12 +08:00
/**
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {DependencyTemplateContext} templateContext the template context
2018-07-27 17:45:12 +08:00
* @returns {InitFragment[]|null} the init fragments
*/
getInitFragments(dependency, { runtimeTemplate, moduleGraph, chunkGraph }) {
2018-07-27 17:45:12 +08:00
const dep = /** @type {ProvidedDependency} */ (dependency);
2018-07-25 02:05:29 +08:00
return [
new InitFragment(
`/* provided dependency */ var ${
dep.identifier
} = ${runtimeTemplate.moduleExports({
module: moduleGraph.getModule(dep),
chunkGraph,
request: dep.request
})}${pathToString(dep.path)};\n`,
2018-07-30 16:15:18 +08:00
InitFragment.STAGE_PROVIDES,
1,
`provided ${dep.identifier}`
2018-07-25 02:05:29 +08:00
)
];
}
}
ProvidedDependency.Template = ProvidedDependencyTemplate;
2018-07-25 02:05:29 +08:00
module.exports = ProvidedDependency;