Skip to content

Commit cd21838

Browse files
committed
Merge branch 'unfold-env-plugin'
2 parents ccf6513 + 6171417 commit cd21838

6 files changed

Lines changed: 82 additions & 0 deletions

File tree

lib/EnvironmentPlugin.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 EnvironmentPlugin(keys) {
8+
this.keys = Array.isArray(keys) ? keys : Array.prototype.slice.call(arguments);
9+
}
10+
module.exports = EnvironmentPlugin;
11+
12+
EnvironmentPlugin.prototype.apply = function(compiler) {
13+
compiler.apply(new DefinePlugin(this.keys.reduce(function(definitions, key) {
14+
var value = process.env[key];
15+
16+
if (value === undefined) {
17+
compiler.plugin("this-compilation", function(compilation) {
18+
var error = new Error(key + " environment variable is undefined.");
19+
error.name = "EnvVariableNotDefinedError";
20+
compilation.warnings.push(error);
21+
});
22+
}
23+
24+
definitions["process.env." + key] = value ? JSON.stringify(value) : "undefined";
25+
26+
return definitions;
27+
}, {})));
28+
};

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",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = [
2+
[/(aaa)/, /module bbb/],
3+
[/(aaa)/, /module ccc/],
4+
[/(aaa)/, /module ddd/],
5+
[/(bbbccc)/, /module aaa/],
6+
[/(bbbccc)/, /module ddd/],
7+
[/(ddd)/, /module aaa/],
8+
[/(ddd)/, /module bbb/],
9+
[/(ddd)/, /module ccc/],
10+
[/(ddd)/, /module ddd/],
11+
];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
it("should import a single process.env var", function() {
2+
if(process.env.AAA !== "aaa")
3+
require.include("aaa");
4+
});
5+
6+
it("should import multiple process.env vars", function() {
7+
if(process.env.BBB !== "bbb")
8+
require.include("bbb");
9+
if(process.env.CCC !== "ccc")
10+
require.include("ccc");
11+
});
12+
13+
it("should warn when a process.env variable is undefined", function() {
14+
if(process.env.DDD !== "ddd")
15+
require.include("ddd");
16+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = [
2+
[/(ddd)/, /DDD environment variable is undefined./]
3+
];
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var EnvironmentPlugin = require("../../../../lib/EnvironmentPlugin");
2+
process.env.AAA = "aaa";
3+
process.env.BBB = "bbb";
4+
process.env.CCC = "ccc";
5+
module.exports = [{
6+
name: "aaa",
7+
module: { unknownContextRegExp: /$^/, unknownContextCritical: false },
8+
plugins: [
9+
new EnvironmentPlugin("AAA")
10+
]
11+
}, {
12+
name: "bbbccc",
13+
module: { unknownContextRegExp: /$^/, unknownContextCritical: false },
14+
plugins: [
15+
new EnvironmentPlugin("BBB", "CCC")
16+
]
17+
}, {
18+
name: "ddd",
19+
module: { unknownContextRegExp: /$^/, unknownContextCritical: false },
20+
plugins: [
21+
new EnvironmentPlugin("DDD")
22+
]
23+
}];

0 commit comments

Comments
 (0)