UNPKG

760 BJavaScriptView Raw
1/**
2 * Module dependencies
3 */
4
5var DefinePlugin = require('webpack/lib/DefinePlugin');
6
7module.exports = Envify;
8
9function Envify(envs) {
10 this.envs = envs || process.env;
11}
12
13Envify.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
25function 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