| 
									
										
										
										
											2015-01-07 06:02:44 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Simen Brekken @simenbrekken | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 07:26:26 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const DefinePlugin = require("./DefinePlugin"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class EnvironmentPlugin { | 
					
						
							|  |  |  | 	constructor(keys) { | 
					
						
							|  |  |  | 		this.keys = Array.isArray(keys) ? keys : Array.prototype.slice.call(arguments); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-07 06:02:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 07:26:26 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.apply(new DefinePlugin(this.keys.reduce((definitions, key) => { | 
					
						
							|  |  |  | 			const value = process.env[key]; | 
					
						
							| 
									
										
										
										
											2015-01-07 06:02:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 07:26:26 +08:00
										 |  |  | 			if(value === undefined) { | 
					
						
							|  |  |  | 				compiler.plugin("this-compilation", (compilation) => { | 
					
						
							|  |  |  | 					const error = new Error(key + " environment variable is undefined."); | 
					
						
							|  |  |  | 					error.name = "EnvVariableNotDefinedError"; | 
					
						
							|  |  |  | 					compilation.warnings.push(error); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-01-07 06:02:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 07:26:26 +08:00
										 |  |  | 			definitions["process.env." + key] = value ? JSON.stringify(value) : "undefined"; | 
					
						
							| 
									
										
										
										
											2015-01-07 06:02:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 07:26:26 +08:00
										 |  |  | 			return definitions; | 
					
						
							|  |  |  | 		}, {}))); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = EnvironmentPlugin; |