| 
									
										
										
										
											2013-02-11 03:37:30 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-09 02:11:26 +08:00
										 |  |  | var ParserHelpers = require("./ParserHelpers"); | 
					
						
							| 
									
										
										
										
											2014-10-17 04:31:20 +08:00
										 |  |  | var ConstDependency = require("./dependencies/ConstDependency"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var NullFactory = require("./NullFactory"); | 
					
						
							| 
									
										
										
										
											2013-02-11 03:37:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | function ProvidePlugin(definitions) { | 
					
						
							|  |  |  | 	this.definitions = definitions; | 
					
						
							| 
									
										
										
										
											2013-02-11 03:37:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = ProvidePlugin; | 
					
						
							|  |  |  | ProvidePlugin.prototype.apply = function(compiler) { | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 	var definitions = this.definitions; | 
					
						
							|  |  |  | 	compiler.plugin("compilation", function(compilation, params) { | 
					
						
							| 
									
										
										
										
											2014-10-17 04:31:20 +08:00
										 |  |  | 		compilation.dependencyFactories.set(ConstDependency, new NullFactory()); | 
					
						
							|  |  |  | 		compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template()); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 		params.normalModuleFactory.plugin("parser", function(parser, parserOptions) { | 
					
						
							|  |  |  | 			Object.keys(definitions).forEach(function(name) { | 
					
						
							| 
									
										
										
										
											2016-12-29 03:41:09 +08:00
										 |  |  | 				var request = [].concat(definitions[name]); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 				var splittedName = name.split("."); | 
					
						
							|  |  |  | 				if(splittedName.length > 0) { | 
					
						
							|  |  |  | 					splittedName.slice(1).forEach(function(_, i) { | 
					
						
							|  |  |  | 						var name = splittedName.slice(0, i + 1).join("."); | 
					
						
							| 
									
										
										
										
											2017-01-11 20:11:42 +08:00
										 |  |  | 						parser.plugin("can-rename " + name, ParserHelpers.approve); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				parser.plugin("expression " + name, function(expr) { | 
					
						
							|  |  |  | 					var nameIdentifier = name; | 
					
						
							|  |  |  | 					var scopedName = name.indexOf(".") >= 0; | 
					
						
							| 
									
										
										
										
											2016-12-29 03:41:09 +08:00
										 |  |  | 					var expression = "require(" + JSON.stringify(request[0]) + ")"; | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 					if(scopedName) { | 
					
						
							|  |  |  | 						nameIdentifier = "__webpack_provided_" + name.replace(/\./g, "_dot_"); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-12-29 03:41:09 +08:00
										 |  |  | 					if(request.length > 1) { | 
					
						
							|  |  |  | 						expression += request.slice(1).map(function(r) { | 
					
						
							|  |  |  | 							return "[" + JSON.stringify(r) + "]"; | 
					
						
							|  |  |  | 						}).join(""); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-01-09 02:11:26 +08:00
										 |  |  | 					if(!ParserHelpers.addParsedVariableToModule(this, nameIdentifier, expression)) { | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 						return false; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if(scopedName) { | 
					
						
							| 
									
										
										
										
											2017-01-08 20:46:29 +08:00
										 |  |  | 						ParserHelpers.toConstantDependency(nameIdentifier).bind(this)(expr); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 					return true; | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2015-02-28 07:26:44 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-07-08 20:39:02 +08:00
										 |  |  | }; |