1 |
|
2 |
|
3 |
|
4 |
|
5 | var DefinePlugin = require('webpack/lib/DefinePlugin');
|
6 |
|
7 | module.exports = Envify;
|
8 |
|
9 | function Envify(envs) {
|
10 | this.envs = envs || process.env;
|
11 | }
|
12 |
|
13 | Envify.prototype.apply = function(compiler) {
|
14 | var self = this;
|
15 | var envs = this.envs;
|
16 | var applyPluginsBailResult = compiler.parser.applyPluginsBailResult;
|
17 | compiler.parser.applyPluginsBailResult = function(str, expr) {
|
18 | if (~str.indexOf('process.env.')) {
|
19 | new DefinePlugin(evaluate(expr, envs)).apply(compiler);
|
20 | }
|
21 | return applyPluginsBailResult.apply(this, arguments);
|
22 | }
|
23 | };
|
24 |
|
25 | function evaluate(expr, envs) {
|
26 | var name = expr.property.name;
|
27 | var value = envs[name] || null;
|
28 | var obj = {};
|
29 | obj['process.env.' + name] = JSON.stringify(value);
|
30 | return obj;
|
31 | } |
\ | No newline at end of file |