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 */
|
2018-08-28 17:50:33 +08:00
|
|
|
/** @typedef {import("../ChunkGraph")} ChunkGraph */
|
2018-07-27 17:45:12 +08:00
|
|
|
/** @typedef {import("../Dependency")} Dependency */
|
2018-07-18 01:38:42 +08:00
|
|
|
/** @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 */
|
2018-07-30 19:23:59 +08:00
|
|
|
/** @typedef {import("../util/createHash").Hash} Hash */
|
2018-07-27 17:45:12 +08:00
|
|
|
|
2018-07-25 03:57:48 +08:00
|
|
|
/**
|
2018-07-28 03:37:52 +08:00
|
|
|
* @param {string[]|null} path the property path array
|
|
|
|
* @returns {string} the converted path
|
2018-07-25 03:57:48 +08:00
|
|
|
*/
|
2018-07-28 03:37:52 +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 {
|
2018-07-28 03:37:52 +08:00
|
|
|
constructor(request, identifier, path, range) {
|
2018-07-25 02:05:29 +08:00
|
|
|
super(request);
|
2018-07-28 03:37:52 +08:00
|
|
|
this.identifier = identifier;
|
|
|
|
this.path = path;
|
2018-07-25 02:05:29 +08:00
|
|
|
this.range = range;
|
|
|
|
}
|
2018-07-30 19:23:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the hash
|
|
|
|
* @param {Hash} hash hash to be updated
|
2018-08-28 17:50:33 +08:00
|
|
|
* @param {ChunkGraph} chunkGraph chunk graph
|
2018-07-30 19:23:59 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-08-28 17:50:33 +08:00
|
|
|
updateHash(hash, chunkGraph) {
|
|
|
|
super.updateHash(hash, chunkGraph);
|
2018-07-30 19:23:59 +08:00
|
|
|
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"
|
|
|
|
);
|
|
|
|
|
2018-07-28 03:37:52 +08:00
|
|
|
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
|
2018-07-18 01:38:42 +08:00
|
|
|
* @param {DependencyTemplateContext} templateContext the context object
|
2018-07-27 17:45:12 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-07-18 01:38:42 +08:00
|
|
|
apply(dependency, source, templateContext) {
|
2018-07-27 17:45:12 +08:00
|
|
|
const dep = /** @type {ProvidedDependency} */ (dependency);
|
2018-07-28 03:37:52 +08:00
|
|
|
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
|
2018-07-18 01:38:42 +08:00
|
|
|
* @param {DependencyTemplateContext} templateContext the template context
|
2018-07-27 17:45:12 +08:00
|
|
|
* @returns {InitFragment[]|null} the init fragments
|
|
|
|
*/
|
2018-08-28 17:50:33 +08:00
|
|
|
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(
|
2018-07-28 03:37:52 +08:00
|
|
|
`/* provided dependency */ var ${
|
|
|
|
dep.identifier
|
|
|
|
} = ${runtimeTemplate.moduleExports({
|
2018-07-24 21:30:37 +08:00
|
|
|
module: moduleGraph.getModule(dep),
|
2018-08-28 17:50:33 +08:00
|
|
|
chunkGraph,
|
2018-07-28 03:37:52 +08:00
|
|
|
request: dep.request
|
|
|
|
})}${pathToString(dep.path)};\n`,
|
2018-07-30 16:15:18 +08:00
|
|
|
InitFragment.STAGE_PROVIDES,
|
2018-07-28 03:37:52 +08:00
|
|
|
1,
|
|
|
|
`provided ${dep.identifier}`
|
2018-07-25 02:05:29 +08:00
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
2018-07-28 03:37:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ProvidedDependency.Template = ProvidedDependencyTemplate;
|
2018-07-25 02:05:29 +08:00
|
|
|
|
|
|
|
module.exports = ProvidedDependency;
|