| 
									
										
										
										
											2015-01-07 06:02:44 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							| 
									
										
										
										
											2016-11-09 23:25:21 +08:00
										 |  |  | 	Authors Simen Brekken @simenbrekken, Einar Löve @einarlove | 
					
						
							| 
									
										
										
										
											2015-01-07 06:02:44 +08:00
										 |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 07:26:26 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const DefinePlugin = require("./DefinePlugin"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class EnvironmentPlugin { | 
					
						
							|  |  |  | 	constructor(keys) { | 
					
						
							| 
									
										
										
										
											2017-01-06 22:40:28 +08:00
										 |  |  | 		if(Array.isArray(keys)) { | 
					
						
							|  |  |  | 			this.keys = keys; | 
					
						
							| 
									
										
										
										
											2017-01-11 16:12:55 +08:00
										 |  |  | 			this.defaultValues = {}; | 
					
						
							| 
									
										
										
										
											2017-01-06 22:40:28 +08:00
										 |  |  | 		} else if(keys && typeof keys === "object") { | 
					
						
							|  |  |  | 			this.keys = Object.keys(keys); | 
					
						
							|  |  |  | 			this.defaultValues = keys; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			this.keys = Array.prototype.slice.call(arguments); | 
					
						
							| 
									
										
										
										
											2017-01-11 16:12:55 +08:00
										 |  |  | 			this.defaultValues = {}; | 
					
						
							| 
									
										
										
										
											2015-01-07 06:02:44 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-30 07:26:26 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-07 06:02:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 07:26:26 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2017-01-11 16:12:55 +08:00
										 |  |  | 		const definitions = this.keys.reduce((defs, key) => { | 
					
						
							| 
									
										
										
										
											2017-03-16 23:11:02 +08:00
										 |  |  | 			const value = process.env[key] !== undefined ? process.env[key] : this.defaultValues[key]; | 
					
						
							| 
									
										
										
										
											2016-12-22 06:11:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 07:26:26 +08:00
										 |  |  | 			if(value === undefined) { | 
					
						
							| 
									
										
										
										
											2017-01-11 16:12:55 +08:00
										 |  |  | 				compiler.plugin("this-compilation", compilation => { | 
					
						
							| 
									
										
										
										
											2017-01-06 22:40:28 +08:00
										 |  |  | 					const error = new Error( | 
					
						
							| 
									
										
										
										
											2017-01-11 16:12:55 +08:00
										 |  |  | 						`EnvironmentPlugin - ${key} environment variable is undefined.\n\n` + | 
					
						
							| 
									
										
										
										
											2017-01-11 21:55:49 +08:00
										 |  |  | 						"You can pass an object with default values to suppress this warning.\n" + | 
					
						
							|  |  |  | 						"See https://webpack.js.org/plugins/environment-plugin for example." | 
					
						
							| 
									
										
										
										
											2017-01-06 22:40:28 +08:00
										 |  |  | 					); | 
					
						
							| 
									
										
										
										
											2015-01-07 06:02:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 07:26:26 +08:00
										 |  |  | 					error.name = "EnvVariableNotDefinedError"; | 
					
						
							|  |  |  | 					compilation.warnings.push(error); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-01-07 06:02:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 16:14:07 +08:00
										 |  |  | 			defs[`process.env.${key}`] = typeof value === "undefined" ? "undefined" : JSON.stringify(value); | 
					
						
							| 
									
										
										
										
											2017-01-06 22:40:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			return defs; | 
					
						
							| 
									
										
										
										
											2017-01-11 16:12:55 +08:00
										 |  |  | 		}, {}); | 
					
						
							| 
									
										
										
										
											2015-01-07 06:02:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-06 22:40:28 +08:00
										 |  |  | 		compiler.apply(new DefinePlugin(definitions)); | 
					
						
							| 
									
										
										
										
											2016-12-30 07:26:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-11-09 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = EnvironmentPlugin; |