Skip to content

Commit b80edb6

Browse files
author
Simen Brekken
committed
Added EnvPlugin.
1 parent 6c45e24 commit b80edb6

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

lib/EnvPlugin.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Simen Brekken @simenbrekken
4+
*/
5+
var DefinePlugin = require("./DefinePlugin");
6+
7+
function EnvPlugin(keys) {
8+
this.keys = Array.isArray(keys) ? keys : Array.prototype.slice.call(arguments);
9+
}
10+
module.exports = EnvPlugin;
11+
12+
EnvPlugin.prototype.apply = function(compiler) {
13+
compiler.apply(new DefinePlugin(getDefinitions(this.keys)));
14+
15+
function getDefinitions(keys) {
16+
return keys.reduce(function(definitions, key) {
17+
var value = process.env[key];
18+
19+
if (value === undefined) {
20+
compiler.plugin("compilation", function(compilation) {
21+
var error = new Error(key + " environment variable is undefined.");
22+
error.name = "EnvVariableNotDefinedError";
23+
compilation.warnings.push(error);
24+
});
25+
}
26+
27+
definitions["process.env." + key] = value ? JSON.stringify(value) : "undefined";
28+
29+
return definitions;
30+
}, {});
31+
}
32+
};

lib/webpack.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ exportPlugins(exports, ".", [
7777
"UmdMainTemplatePlugin",
7878
"NoErrorsPlugin",
7979
"NewWatchingPlugin",
80+
"EnvPlugin"
8081
]);
8182
exportPlugins(exports.optimize = {}, "./optimize", [
8283
"AggressiveMergingPlugin",

0 commit comments

Comments
 (0)