-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
41 lines (31 loc) · 1.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const dotenv = require('dotenv-safe');
const fs = require('fs');
const DefinePlugin = require('webpack').DefinePlugin;
module.exports = DotenvPlugin;
function DotenvPlugin(options) {
options = options || {};
if (!options.sample) options.sample = './.env.sample';
if (!options.path) options.path = './.env';
dotenv.config(options);
this.example = dotenv.parse(fs.readFileSync(options.sample));
this.env = {};
if (fs.existsSync(options.path)) {
this.env = dotenv.parse(fs.readFileSync(options.path));
}
}
DotenvPlugin.prototype.apply = function(compiler) {
const definitions = Object.keys(this.example).reduce((definitions, key) => {
const existing = process.env[key];
if (existing) {
definitions[key] = JSON.stringify(existing);
return definitions;
}
const value = this.env[key];
if (value) definitions[key] = JSON.stringify(value);
return definitions;
}, {});
const plugin = {
'process.env': definitions,
};
new DefinePlugin(plugin).apply(compiler);
};