| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const {
|
|---|
| 9 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
|---|
| 10 | JAVASCRIPT_MODULE_TYPE_DYNAMIC,
|
|---|
| 11 | JAVASCRIPT_MODULE_TYPE_ESM
|
|---|
| 12 | } = require("./ModuleTypeConstants");
|
|---|
| 13 | const ConstDependency = require("./dependencies/ConstDependency");
|
|---|
| 14 | const ProvidedDependency = require("./dependencies/ProvidedDependency");
|
|---|
| 15 | const { approve } = require("./javascript/JavascriptParserHelpers");
|
|---|
| 16 |
|
|---|
| 17 | /** @typedef {import("../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
|---|
| 18 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 19 | /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
|
|---|
| 20 | /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
|
|---|
| 21 | /** @typedef {import("./javascript/JavascriptParser").Range} Range */
|
|---|
| 22 |
|
|---|
| 23 | const PLUGIN_NAME = "ProvidePlugin";
|
|---|
| 24 |
|
|---|
| 25 | class ProvidePlugin {
|
|---|
| 26 | /**
|
|---|
| 27 | * Creates an instance of ProvidePlugin.
|
|---|
| 28 | * @param {Record<string, string | string[]>} definitions the provided identifiers
|
|---|
| 29 | */
|
|---|
| 30 | constructor(definitions) {
|
|---|
| 31 | this.definitions = definitions;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 36 | * @param {Compiler} compiler the compiler instance
|
|---|
| 37 | * @returns {void}
|
|---|
| 38 | */
|
|---|
| 39 | apply(compiler) {
|
|---|
| 40 | const definitions = this.definitions;
|
|---|
| 41 | compiler.hooks.compilation.tap(
|
|---|
| 42 | PLUGIN_NAME,
|
|---|
| 43 | (compilation, { normalModuleFactory }) => {
|
|---|
| 44 | compilation.dependencyTemplates.set(
|
|---|
| 45 | ConstDependency,
|
|---|
| 46 | new ConstDependency.Template()
|
|---|
| 47 | );
|
|---|
| 48 | compilation.dependencyFactories.set(
|
|---|
| 49 | ProvidedDependency,
|
|---|
| 50 | normalModuleFactory
|
|---|
| 51 | );
|
|---|
| 52 | compilation.dependencyTemplates.set(
|
|---|
| 53 | ProvidedDependency,
|
|---|
| 54 | new ProvidedDependency.Template()
|
|---|
| 55 | );
|
|---|
| 56 | /**
|
|---|
| 57 | * Handles the hook callback for this code path.
|
|---|
| 58 | * @param {JavascriptParser} parser the parser
|
|---|
| 59 | * @param {JavascriptParserOptions} parserOptions options
|
|---|
| 60 | * @returns {void}
|
|---|
| 61 | */
|
|---|
| 62 | const handler = (parser, parserOptions) => {
|
|---|
| 63 | for (const name of Object.keys(definitions)) {
|
|---|
| 64 | const request = [
|
|---|
| 65 | ...(Array.isArray(definitions[name])
|
|---|
| 66 | ? definitions[name]
|
|---|
| 67 | : [definitions[name]])
|
|---|
| 68 | ];
|
|---|
| 69 | const splittedName = name.split(".");
|
|---|
| 70 | if (splittedName.length > 0) {
|
|---|
| 71 | for (const [i, _] of splittedName.slice(1).entries()) {
|
|---|
| 72 | const name = splittedName.slice(0, i + 1).join(".");
|
|---|
| 73 | parser.hooks.canRename.for(name).tap(PLUGIN_NAME, approve);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | parser.hooks.expression.for(name).tap(PLUGIN_NAME, (expr) => {
|
|---|
| 78 | const nameIdentifier = name.includes(".")
|
|---|
| 79 | ? `__webpack_provided_${name.replace(/\./g, "_dot_")}`
|
|---|
| 80 | : name;
|
|---|
| 81 | const dep = new ProvidedDependency(
|
|---|
| 82 | request[0],
|
|---|
| 83 | nameIdentifier,
|
|---|
| 84 | request.slice(1),
|
|---|
| 85 | /** @type {Range} */ (expr.range)
|
|---|
| 86 | );
|
|---|
| 87 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|---|
| 88 | parser.state.module.addDependency(dep);
|
|---|
| 89 | return true;
|
|---|
| 90 | });
|
|---|
| 91 |
|
|---|
| 92 | parser.hooks.call.for(name).tap(PLUGIN_NAME, (expr) => {
|
|---|
| 93 | const nameIdentifier = name.includes(".")
|
|---|
| 94 | ? `__webpack_provided_${name.replace(/\./g, "_dot_")}`
|
|---|
| 95 | : name;
|
|---|
| 96 | const dep = new ProvidedDependency(
|
|---|
| 97 | request[0],
|
|---|
| 98 | nameIdentifier,
|
|---|
| 99 | request.slice(1),
|
|---|
| 100 | /** @type {Range} */ (expr.callee.range)
|
|---|
| 101 | );
|
|---|
| 102 | dep.loc = /** @type {DependencyLocation} */ (expr.callee.loc);
|
|---|
| 103 | parser.state.module.addDependency(dep);
|
|---|
| 104 | parser.walkExpressions(expr.arguments);
|
|---|
| 105 | return true;
|
|---|
| 106 | });
|
|---|
| 107 | }
|
|---|
| 108 | };
|
|---|
| 109 | normalModuleFactory.hooks.parser
|
|---|
| 110 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
|---|
| 111 | .tap(PLUGIN_NAME, handler);
|
|---|
| 112 | normalModuleFactory.hooks.parser
|
|---|
| 113 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
|---|
| 114 | .tap(PLUGIN_NAME, handler);
|
|---|
| 115 | normalModuleFactory.hooks.parser
|
|---|
| 116 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
|---|
| 117 | .tap(PLUGIN_NAME, handler);
|
|---|
| 118 | }
|
|---|
| 119 | );
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | module.exports = ProvidePlugin;
|
|---|