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 | * @param {Record<string, string | string[]>} definitions the provided identifiers
|
---|
28 | */
|
---|
29 | constructor(definitions) {
|
---|
30 | this.definitions = definitions;
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Apply the plugin
|
---|
35 | * @param {Compiler} compiler the compiler instance
|
---|
36 | * @returns {void}
|
---|
37 | */
|
---|
38 | apply(compiler) {
|
---|
39 | const definitions = this.definitions;
|
---|
40 | compiler.hooks.compilation.tap(
|
---|
41 | PLUGIN_NAME,
|
---|
42 | (compilation, { normalModuleFactory }) => {
|
---|
43 | compilation.dependencyTemplates.set(
|
---|
44 | ConstDependency,
|
---|
45 | new ConstDependency.Template()
|
---|
46 | );
|
---|
47 | compilation.dependencyFactories.set(
|
---|
48 | ProvidedDependency,
|
---|
49 | normalModuleFactory
|
---|
50 | );
|
---|
51 | compilation.dependencyTemplates.set(
|
---|
52 | ProvidedDependency,
|
---|
53 | new ProvidedDependency.Template()
|
---|
54 | );
|
---|
55 | /**
|
---|
56 | * @param {JavascriptParser} parser the parser
|
---|
57 | * @param {JavascriptParserOptions} parserOptions options
|
---|
58 | * @returns {void}
|
---|
59 | */
|
---|
60 | const handler = (parser, parserOptions) => {
|
---|
61 | for (const name of Object.keys(definitions)) {
|
---|
62 | const request =
|
---|
63 | /** @type {string[]} */
|
---|
64 | ([]).concat(definitions[name]);
|
---|
65 | const splittedName = name.split(".");
|
---|
66 | if (splittedName.length > 0) {
|
---|
67 | for (const [i, _] of splittedName.slice(1).entries()) {
|
---|
68 | const name = splittedName.slice(0, i + 1).join(".");
|
---|
69 | parser.hooks.canRename.for(name).tap(PLUGIN_NAME, approve);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | parser.hooks.expression.for(name).tap(PLUGIN_NAME, expr => {
|
---|
74 | const nameIdentifier = name.includes(".")
|
---|
75 | ? `__webpack_provided_${name.replace(/\./g, "_dot_")}`
|
---|
76 | : name;
|
---|
77 | const dep = new ProvidedDependency(
|
---|
78 | request[0],
|
---|
79 | nameIdentifier,
|
---|
80 | request.slice(1),
|
---|
81 | /** @type {Range} */ (expr.range)
|
---|
82 | );
|
---|
83 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
84 | parser.state.module.addDependency(dep);
|
---|
85 | return true;
|
---|
86 | });
|
---|
87 |
|
---|
88 | parser.hooks.call.for(name).tap(PLUGIN_NAME, expr => {
|
---|
89 | const nameIdentifier = name.includes(".")
|
---|
90 | ? `__webpack_provided_${name.replace(/\./g, "_dot_")}`
|
---|
91 | : name;
|
---|
92 | const dep = new ProvidedDependency(
|
---|
93 | request[0],
|
---|
94 | nameIdentifier,
|
---|
95 | request.slice(1),
|
---|
96 | /** @type {Range} */ (expr.callee.range)
|
---|
97 | );
|
---|
98 | dep.loc = /** @type {DependencyLocation} */ (expr.callee.loc);
|
---|
99 | parser.state.module.addDependency(dep);
|
---|
100 | parser.walkExpressions(expr.arguments);
|
---|
101 | return true;
|
---|
102 | });
|
---|
103 | }
|
---|
104 | };
|
---|
105 | normalModuleFactory.hooks.parser
|
---|
106 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
107 | .tap(PLUGIN_NAME, handler);
|
---|
108 | normalModuleFactory.hooks.parser
|
---|
109 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
---|
110 | .tap(PLUGIN_NAME, handler);
|
---|
111 | normalModuleFactory.hooks.parser
|
---|
112 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
---|
113 | .tap(PLUGIN_NAME, handler);
|
---|
114 | }
|
---|
115 | );
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | module.exports = ProvidePlugin;
|
---|