webpack/lib/ProvidePlugin.js

102 lines
2.8 KiB
JavaScript
Raw Permalink 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-30 23:08:51 +08:00
const ConstDependency = require("./dependencies/ConstDependency");
const ProvidedDependency = require("./dependencies/ProvidedDependency");
const { approve } = require("./javascript/JavascriptParserHelpers");
2017-02-22 15:47:14 +08:00
2018-11-09 05:59:19 +08:00
/** @typedef {import("./Compiler")} Compiler */
2017-02-22 15:47:14 +08:00
class ProvidePlugin {
2018-11-09 05:59:19 +08:00
/**
* @param {Record<string, string | string[]>} definitions the provided identifiers
*/
2017-02-22 15:47:14 +08:00
constructor(definitions) {
this.definitions = definitions;
}
2018-11-09 05:59:19 +08:00
/**
2020-04-23 16:48:36 +08:00
* Apply the plugin
2018-11-09 05:59:19 +08:00
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
2017-02-22 15:47:14 +08:00
apply(compiler) {
const definitions = this.definitions;
2018-02-25 09:00:20 +08:00
compiler.hooks.compilation.tap(
"ProvidePlugin",
(compilation, { normalModuleFactory }) => {
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 ProvidedDependency.Template()
2018-07-28 02:42:34 +08:00
);
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;
});
parser.hooks.call.for(name).tap("ProvidePlugin", expr => {
const nameIdentifier = name.includes(".")
? `__webpack_provided_${name.replace(/\./g, "_dot_")}`
: name;
const dep = new ProvidedDependency(
request[0],
nameIdentifier,
request.slice(1),
expr.callee.range
);
dep.loc = expr.callee.loc;
parser.state.module.addDependency(dep);
parser.walkExpressions(expr.arguments);
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);
2018-02-25 09:00:20 +08:00
}
);
2017-02-22 15:47:14 +08:00
}
}
2018-07-28 02:42:34 +08:00
2017-02-22 15:47:14 +08:00
module.exports = ProvidePlugin;