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 | } = require("./ModuleTypeConstants");
|
---|
12 | const NodeStuffInWebError = require("./NodeStuffInWebError");
|
---|
13 | const RuntimeGlobals = require("./RuntimeGlobals");
|
---|
14 | const CachedConstDependency = require("./dependencies/CachedConstDependency");
|
---|
15 | const ConstDependency = require("./dependencies/ConstDependency");
|
---|
16 | const ExternalModuleDependency = require("./dependencies/ExternalModuleDependency");
|
---|
17 | const {
|
---|
18 | evaluateToString,
|
---|
19 | expressionIsUnsupported
|
---|
20 | } = require("./javascript/JavascriptParserHelpers");
|
---|
21 | const { relative } = require("./util/fs");
|
---|
22 | const { parseResource } = require("./util/identifier");
|
---|
23 |
|
---|
24 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
25 | /** @typedef {import("../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
---|
26 | /** @typedef {import("../declarations/WebpackOptions").NodeOptions} NodeOptions */
|
---|
27 | /** @typedef {import("./Compiler")} Compiler */
|
---|
28 | /** @typedef {import("./Dependency")} Dependency */
|
---|
29 | /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
|
---|
30 | /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
|
---|
31 | /** @typedef {import("./NormalModule")} NormalModule */
|
---|
32 | /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
|
---|
33 | /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
|
---|
34 | /** @typedef {import("./javascript/JavascriptParser").Range} Range */
|
---|
35 | /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
|
---|
36 |
|
---|
37 | const PLUGIN_NAME = "NodeStuffPlugin";
|
---|
38 |
|
---|
39 | class NodeStuffPlugin {
|
---|
40 | /**
|
---|
41 | * @param {NodeOptions} options options
|
---|
42 | */
|
---|
43 | constructor(options) {
|
---|
44 | this.options = options;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Apply the plugin
|
---|
49 | * @param {Compiler} compiler the compiler instance
|
---|
50 | * @returns {void}
|
---|
51 | */
|
---|
52 | apply(compiler) {
|
---|
53 | const options = this.options;
|
---|
54 | compiler.hooks.compilation.tap(
|
---|
55 | PLUGIN_NAME,
|
---|
56 | (compilation, { normalModuleFactory }) => {
|
---|
57 | compilation.dependencyTemplates.set(
|
---|
58 | ExternalModuleDependency,
|
---|
59 | new ExternalModuleDependency.Template()
|
---|
60 | );
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * @param {JavascriptParser} parser the parser
|
---|
64 | * @param {JavascriptParserOptions} parserOptions options
|
---|
65 | * @returns {void}
|
---|
66 | */
|
---|
67 | const handler = (parser, parserOptions) => {
|
---|
68 | if (parserOptions.node === false) return;
|
---|
69 |
|
---|
70 | let localOptions = options;
|
---|
71 | if (parserOptions.node) {
|
---|
72 | localOptions = { ...localOptions, ...parserOptions.node };
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (localOptions.global !== false) {
|
---|
76 | const withWarning = localOptions.global === "warn";
|
---|
77 | parser.hooks.expression.for("global").tap(PLUGIN_NAME, expr => {
|
---|
78 | const dep = new ConstDependency(
|
---|
79 | RuntimeGlobals.global,
|
---|
80 | /** @type {Range} */ (expr.range),
|
---|
81 | [RuntimeGlobals.global]
|
---|
82 | );
|
---|
83 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
84 | parser.state.module.addPresentationalDependency(dep);
|
---|
85 |
|
---|
86 | // TODO webpack 6 remove
|
---|
87 | if (withWarning) {
|
---|
88 | parser.state.module.addWarning(
|
---|
89 | new NodeStuffInWebError(
|
---|
90 | dep.loc,
|
---|
91 | "global",
|
---|
92 | "The global namespace object is a Node.js feature and isn't available in browsers."
|
---|
93 | )
|
---|
94 | );
|
---|
95 | }
|
---|
96 | });
|
---|
97 | parser.hooks.rename.for("global").tap(PLUGIN_NAME, expr => {
|
---|
98 | const dep = new ConstDependency(
|
---|
99 | RuntimeGlobals.global,
|
---|
100 | /** @type {Range} */ (expr.range),
|
---|
101 | [RuntimeGlobals.global]
|
---|
102 | );
|
---|
103 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
104 | parser.state.module.addPresentationalDependency(dep);
|
---|
105 | return false;
|
---|
106 | });
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * @param {string} expressionName expression name
|
---|
111 | * @param {(module: NormalModule) => string} fn function
|
---|
112 | * @param {string=} warning warning
|
---|
113 | * @returns {void}
|
---|
114 | */
|
---|
115 | const setModuleConstant = (expressionName, fn, warning) => {
|
---|
116 | parser.hooks.expression
|
---|
117 | .for(expressionName)
|
---|
118 | .tap(PLUGIN_NAME, expr => {
|
---|
119 | const dep = new CachedConstDependency(
|
---|
120 | JSON.stringify(fn(parser.state.module)),
|
---|
121 | /** @type {Range} */ (expr.range),
|
---|
122 | expressionName
|
---|
123 | );
|
---|
124 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
125 | parser.state.module.addPresentationalDependency(dep);
|
---|
126 |
|
---|
127 | // TODO webpack 6 remove
|
---|
128 | if (warning) {
|
---|
129 | parser.state.module.addWarning(
|
---|
130 | new NodeStuffInWebError(dep.loc, expressionName, warning)
|
---|
131 | );
|
---|
132 | }
|
---|
133 |
|
---|
134 | return true;
|
---|
135 | });
|
---|
136 | };
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * @param {string} expressionName expression name
|
---|
140 | * @param {(value: string) => string} fn function
|
---|
141 | * @returns {void}
|
---|
142 | */
|
---|
143 | const setUrlModuleConstant = (expressionName, fn) => {
|
---|
144 | parser.hooks.expression
|
---|
145 | .for(expressionName)
|
---|
146 | .tap(PLUGIN_NAME, expr => {
|
---|
147 | const dep = new ExternalModuleDependency(
|
---|
148 | "url",
|
---|
149 | [
|
---|
150 | {
|
---|
151 | name: "fileURLToPath",
|
---|
152 | value: "__webpack_fileURLToPath__"
|
---|
153 | }
|
---|
154 | ],
|
---|
155 | undefined,
|
---|
156 | fn("__webpack_fileURLToPath__"),
|
---|
157 | /** @type {Range} */ (expr.range),
|
---|
158 | expressionName
|
---|
159 | );
|
---|
160 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
161 | parser.state.module.addPresentationalDependency(dep);
|
---|
162 |
|
---|
163 | return true;
|
---|
164 | });
|
---|
165 | };
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * @param {string} expressionName expression name
|
---|
169 | * @param {string} value value
|
---|
170 | * @param {string=} warning warning
|
---|
171 | * @returns {void}
|
---|
172 | */
|
---|
173 | const setConstant = (expressionName, value, warning) =>
|
---|
174 | setModuleConstant(expressionName, () => value, warning);
|
---|
175 |
|
---|
176 | const context = compiler.context;
|
---|
177 | if (localOptions.__filename) {
|
---|
178 | switch (localOptions.__filename) {
|
---|
179 | case "mock":
|
---|
180 | setConstant("__filename", "/index.js");
|
---|
181 | break;
|
---|
182 | case "warn-mock":
|
---|
183 | setConstant(
|
---|
184 | "__filename",
|
---|
185 | "/index.js",
|
---|
186 | "__filename is a Node.js feature and isn't available in browsers."
|
---|
187 | );
|
---|
188 | break;
|
---|
189 | case "node-module":
|
---|
190 | setUrlModuleConstant(
|
---|
191 | "__filename",
|
---|
192 | functionName => `${functionName}(import.meta.url)`
|
---|
193 | );
|
---|
194 | break;
|
---|
195 | case true:
|
---|
196 | setModuleConstant("__filename", module =>
|
---|
197 | relative(
|
---|
198 | /** @type {InputFileSystem} */ (compiler.inputFileSystem),
|
---|
199 | context,
|
---|
200 | module.resource
|
---|
201 | )
|
---|
202 | );
|
---|
203 | break;
|
---|
204 | }
|
---|
205 |
|
---|
206 | parser.hooks.evaluateIdentifier
|
---|
207 | .for("__filename")
|
---|
208 | .tap(PLUGIN_NAME, expr => {
|
---|
209 | if (!parser.state.module) return;
|
---|
210 | const resource = parseResource(parser.state.module.resource);
|
---|
211 | return evaluateToString(resource.path)(expr);
|
---|
212 | });
|
---|
213 | }
|
---|
214 | if (localOptions.__dirname) {
|
---|
215 | switch (localOptions.__dirname) {
|
---|
216 | case "mock":
|
---|
217 | setConstant("__dirname", "/");
|
---|
218 | break;
|
---|
219 | case "warn-mock":
|
---|
220 | setConstant(
|
---|
221 | "__dirname",
|
---|
222 | "/",
|
---|
223 | "__dirname is a Node.js feature and isn't available in browsers."
|
---|
224 | );
|
---|
225 | break;
|
---|
226 | case "node-module":
|
---|
227 | setUrlModuleConstant(
|
---|
228 | "__dirname",
|
---|
229 | functionName =>
|
---|
230 | `${functionName}(import.meta.url + "/..").slice(0, -1)`
|
---|
231 | );
|
---|
232 | break;
|
---|
233 | case true:
|
---|
234 | setModuleConstant("__dirname", module =>
|
---|
235 | relative(
|
---|
236 | /** @type {InputFileSystem} */ (compiler.inputFileSystem),
|
---|
237 | context,
|
---|
238 | /** @type {string} */ (module.context)
|
---|
239 | )
|
---|
240 | );
|
---|
241 | break;
|
---|
242 | }
|
---|
243 |
|
---|
244 | parser.hooks.evaluateIdentifier
|
---|
245 | .for("__dirname")
|
---|
246 | .tap(PLUGIN_NAME, expr => {
|
---|
247 | if (!parser.state.module) return;
|
---|
248 | return evaluateToString(
|
---|
249 | /** @type {string} */ (parser.state.module.context)
|
---|
250 | )(expr);
|
---|
251 | });
|
---|
252 | }
|
---|
253 | parser.hooks.expression
|
---|
254 | .for("require.extensions")
|
---|
255 | .tap(
|
---|
256 | PLUGIN_NAME,
|
---|
257 | expressionIsUnsupported(
|
---|
258 | parser,
|
---|
259 | "require.extensions is not supported by webpack. Use a loader instead."
|
---|
260 | )
|
---|
261 | );
|
---|
262 | };
|
---|
263 |
|
---|
264 | normalModuleFactory.hooks.parser
|
---|
265 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
266 | .tap(PLUGIN_NAME, handler);
|
---|
267 | normalModuleFactory.hooks.parser
|
---|
268 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
---|
269 | .tap(PLUGIN_NAME, handler);
|
---|
270 | }
|
---|
271 | );
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | module.exports = NodeStuffPlugin;
|
---|