| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 02:42:34 +08:00
										 |  |  | const { approve } = require("./JavascriptParserHelpers"); | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @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, | 
					
						
							| 
									
										
										
										
											2018-07-28 03:37:52 +08:00
										 |  |  | 					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; | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2018-11-05 17:23:14 +08:00
										 |  |  | 				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; |