2015-01-07 06:02:44 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Simen Brekken @simenbrekken
|
|
|
|
*/
|
|
|
|
var DefinePlugin = require("./DefinePlugin");
|
|
|
|
|
2016-11-09 23:25:00 +08:00
|
|
|
|
|
|
|
this.keys = Array.isArray(keys) ? keys : Object.keys(keys);
|
|
|
|
this.defaultValues = Array.isArray(keys) ? {} : keys;
|
2015-01-07 06:02:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EnvironmentPlugin.prototype.apply = function(compiler) {
|
|
|
|
compiler.apply(new DefinePlugin(this.keys.reduce(function(definitions, key) {
|
2016-11-09 23:25:00 +08:00
|
|
|
var value = process.env[key] || this.defaultValues[key];
|
2015-01-07 06:02:44 +08:00
|
|
|
|
2016-10-18 15:48:24 +08:00
|
|
|
if(value === undefined && !this.silent) {
|
2015-01-07 06:02:44 +08:00
|
|
|
compiler.plugin("this-compilation", function(compilation) {
|
|
|
|
var error = new Error(key + " environment variable is undefined.");
|
|
|
|
error.name = "EnvVariableNotDefinedError";
|
|
|
|
compilation.warnings.push(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
definitions["process.env." + key] = value ? JSON.stringify(value) : "undefined";
|
|
|
|
|
|
|
|
return definitions;
|
2016-10-18 15:48:24 +08:00
|
|
|
}.bind(this), {})));
|
2015-01-07 06:02:44 +08:00
|
|
|
};
|
2016-11-09 23:25:00 +08:00
|
|
|
|
|
|
|
module.exports = EnvironmentPlugin;
|