webpack/lib/ProvidePlugin.js

137 lines
4.3 KiB
JavaScript
Raw Normal View History

2013-02-11 03:37:30 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
2017-02-22 15:47:14 +08:00
"use strict";
2018-07-28 02:42:34 +08:00
const InitFragment = require("./InitFragment");
const { approve } = require("./JavascriptParserHelpers");
2017-02-22 15:47:14 +08:00
const NullFactory = require("./NullFactory");
2018-07-30 23:08:51 +08:00
const ConstDependency = require("./dependencies/ConstDependency");
2018-07-28 02:42:34 +08:00
const ModuleDependency = require("./dependencies/ModuleDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("./Dependency")} Dependency */
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
/**
* @param {string[]} path the property path array
* @returns {string} the converted path
*/
const pathToString = path =>
path.length > 0 ? path.map(part => `[${JSON.stringify(part)}]`).join("") : "";
2017-02-22 15:47:14 +08:00
class ProvidePlugin {
constructor(definitions) {
this.definitions = definitions;
}
apply(compiler) {
const definitions = this.definitions;
2018-02-25 09:00:20 +08:00
compiler.hooks.compilation.tap(
"ProvidePlugin",
(compilation, { normalModuleFactory }) => {
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
compilation.dependencyTemplates.set(
ConstDependency,
new ConstDependency.Template()
);
2018-07-28 02:42:34 +08:00
compilation.dependencyFactories.set(
ProvidedDependency,
normalModuleFactory
);
compilation.dependencyTemplates.set(
ProvidedDependency,
new ProvidedDependencyTemplate()
);
2018-02-25 09:00:20 +08:00
const handler = (parser, parserOptions) => {
Object.keys(definitions).forEach(name => {
2018-07-28 02:42:34 +08:00
const request = [].concat(definitions[name]);
const splittedName = name.split(".");
2018-02-25 09:00:20 +08:00
if (splittedName.length > 0) {
splittedName.slice(1).forEach((_, i) => {
const name = splittedName.slice(0, i + 1).join(".");
2018-07-03 16:24:29 +08:00
parser.hooks.canRename.for(name).tap("ProvidePlugin", approve);
2018-02-25 09:00:20 +08:00
});
2017-02-22 15:47:14 +08:00
}
2018-07-28 02:42:34 +08:00
2018-02-25 09:00:20 +08:00
parser.hooks.expression.for(name).tap("ProvidePlugin", expr => {
2018-07-28 02:42:34 +08:00
const nameIdentifier = name.includes(".")
? `__webpack_provided_${name.replace(/\./g, "_dot_")}`
: name;
const dep = new ProvidedDependency(
request[0],
nameIdentifier,
request.slice(1),
expr.range
);
dep.loc = expr.loc;
parser.state.module.addDependency(dep);
2018-02-25 09:00:20 +08:00
return true;
});
2017-02-22 15:47:14 +08:00
});
2018-02-25 09:00:20 +08:00
};
normalModuleFactory.hooks.parser
.for("javascript/auto")
.tap("ProvidePlugin", handler);
normalModuleFactory.hooks.parser
.for("javascript/dynamic")
.tap("ProvidePlugin", handler);
normalModuleFactory.hooks.parser
.for("javascript/esm")
.tap("ProvidePlugin", handler);
}
);
2017-02-22 15:47:14 +08:00
}
}
2018-07-28 02:42:34 +08:00
class ProvidedDependency extends ModuleDependency {
constructor(request, identifier, path, range) {
super(request);
this.identifier = identifier;
this.path = path;
this.range = range;
}
}
class ProvidedDependencyTemplate 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 {RuntimeTemplate} runtimeTemplate the runtime template
* @param {DependencyTemplates} dependencyTemplates the dependency templates
* @returns {void}
*/
apply(dependency, source, runtimeTemplate, dependencyTemplates) {
const dep = /** @type {ProvidedDependency} */ (dependency);
source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
}
/**
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {ReplaceSource} source the current replace source which can be modified
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @param {DependencyTemplates} dependencyTemplates the dependency templates
* @returns {InitFragment[]|null} the init fragments
*/
getInitFragments(dependency, source, runtimeTemplate, dependencyTemplates) {
const dep = /** @type {ProvidedDependency} */ (dependency);
return [
new InitFragment(
`/* provided dependency */ var ${
dep.identifier
} = ${runtimeTemplate.moduleExports({
module: dep.module,
request: dep.request
})}${pathToString(dep.path)};\n`,
1,
`provided ${dep.identifier}`
)
];
}
}
2017-02-22 15:47:14 +08:00
module.exports = ProvidePlugin;