| 
									
										
										
										
											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"; | 
					
						
							| 
									
										
										
										
											2014-10-17 04:31:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	JAVASCRIPT_MODULE_TYPE_AUTO, | 
					
						
							|  |  |  | 	JAVASCRIPT_MODULE_TYPE_DYNAMIC, | 
					
						
							|  |  |  | 	JAVASCRIPT_MODULE_TYPE_ESM | 
					
						
							|  |  |  | } = require("./ModuleTypeConstants"); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | const ConstDependency = require("./dependencies/ConstDependency"); | 
					
						
							| 
									
										
										
										
											2018-07-28 03:37:52 +08:00
										 |  |  | const ProvidedDependency = require("./dependencies/ProvidedDependency"); | 
					
						
							| 
									
										
										
										
											2019-10-22 15:27:52 +08:00
										 |  |  | const { approve } = require("./javascript/JavascriptParserHelpers"); | 
					
						
							| 
									
										
										
										
											2017-02-22 15:47:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | /** @typedef {import("../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ | 
					
						
							| 
									
										
										
										
											2018-11-09 05:59:19 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */ | 
					
						
							|  |  |  | /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */ | 
					
						
							|  |  |  | /** @typedef {import("./javascript/JavascriptParser").Range} Range */ | 
					
						
							| 
									
										
										
										
											2018-11-09 05:59:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | const PLUGIN_NAME = "ProvidePlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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( | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 			PLUGIN_NAME, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			(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, | 
					
						
							| 
									
										
										
										
											2018-07-28 03:37:52 +08:00
										 |  |  | 					new ProvidedDependency.Template() | 
					
						
							| 
									
										
										
										
											2018-07-28 02:42:34 +08:00
										 |  |  | 				); | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 				/** | 
					
						
							|  |  |  | 				 * @param {JavascriptParser} parser the parser | 
					
						
							|  |  |  | 				 * @param {JavascriptParserOptions} parserOptions options | 
					
						
							|  |  |  | 				 * @returns {void} | 
					
						
							|  |  |  | 				 */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				const handler = (parser, parserOptions) => { | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 					for (const name of Object.keys(definitions)) { | 
					
						
							| 
									
										
										
										
											2024-03-18 01:15:44 +08:00
										 |  |  | 						const request = | 
					
						
							|  |  |  | 							/** @type {string[]} */ | 
					
						
							|  |  |  | 							([]).concat(definitions[name]); | 
					
						
							| 
									
										
										
										
											2018-07-28 02:42:34 +08:00
										 |  |  | 						const splittedName = name.split("."); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						if (splittedName.length > 0) { | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 							for (const [i, _] of splittedName.slice(1).entries()) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								const name = splittedName.slice(0, i + 1).join("."); | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 								parser.hooks.canRename.for(name).tap(PLUGIN_NAME, approve); | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2017-02-22 15:47:14 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-07-28 02:42:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 						parser.hooks.expression.for(name).tap(PLUGIN_NAME, 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), | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 								/** @type {Range} */ (expr.range) | 
					
						
							| 
									
										
										
										
											2018-07-28 02:42:34 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 							dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2018-07-28 02:42:34 +08:00
										 |  |  | 							parser.state.module.addDependency(dep); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							return true; | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2020-10-10 08:51:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 						parser.hooks.call.for(name).tap(PLUGIN_NAME, expr => { | 
					
						
							| 
									
										
										
										
											2020-10-10 08:51:51 +08:00
										 |  |  | 							const nameIdentifier = name.includes(".") | 
					
						
							|  |  |  | 								? `__webpack_provided_${name.replace(/\./g, "_dot_")}` | 
					
						
							|  |  |  | 								: name; | 
					
						
							|  |  |  | 							const dep = new ProvidedDependency( | 
					
						
							|  |  |  | 								request[0], | 
					
						
							|  |  |  | 								nameIdentifier, | 
					
						
							|  |  |  | 								request.slice(1), | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 								/** @type {Range} */ (expr.callee.range) | 
					
						
							| 
									
										
										
										
											2020-10-10 08:51:51 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 							dep.loc = /** @type {DependencyLocation} */ (expr.callee.loc); | 
					
						
							| 
									
										
										
										
											2020-10-10 08:51:51 +08:00
										 |  |  | 							parser.state.module.addDependency(dep); | 
					
						
							|  |  |  | 							parser.walkExpressions(expr.arguments); | 
					
						
							|  |  |  | 							return true; | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 					.for(JAVASCRIPT_MODULE_TYPE_AUTO) | 
					
						
							|  |  |  | 					.tap(PLUGIN_NAME, handler); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 					.for(JAVASCRIPT_MODULE_TYPE_DYNAMIC) | 
					
						
							|  |  |  | 					.tap(PLUGIN_NAME, handler); | 
					
						
							| 
									
										
										
										
											2018-11-05 17:23:14 +08:00
										 |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 					.for(JAVASCRIPT_MODULE_TYPE_ESM) | 
					
						
							|  |  |  | 					.tap(PLUGIN_NAME, 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; |