-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.js
57 lines (51 loc) · 1.35 KB
/
env.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const path = require("path");
const fs = require("fs");
module.exports = (env) => {
// 创建环境变量
let dotenvFiles = [];
switch (env) {
case "development":
dotenvFiles.push(path.resolve(".env.development"));
break;
case "production":
dotenvFiles.push(path.resolve(".env.production"));
break;
case "uat":
dotenvFiles.push(path.resolve(".env.uat"));
break;
default:
break;
}
dotenvFiles.push(path.resolve(".env"));
dotenvFiles = dotenvFiles.filter(Boolean);
dotenvFiles.forEach((dotenvFile) => {
if (fs.existsSync(dotenvFile)) {
require("dotenv-expand")(
require("dotenv").config({
path: dotenvFile,
})
);
}
});
const BYHEALTH_ENV = /^BY_HEALTH/i;
const raw = Object.keys(process.env)
.filter((key) => BYHEALTH_ENV.test(key))
.reduce(
(env, key) => {
env[key] = process.env[key];
return env;
},
{
NODE_ENV: (env || "development"),
PUBLIC_PATH: (process.env.PUBLIC_PATH ? `${process.env.PUBLIC_PATH}/` : './')
}
);
// Stringify all values so we can feed into webpack DefinePlugin
const stringified = {
"process.env": Object.keys(raw).reduce((env, key) => {
env[key] = JSON.stringify(raw[key]);
return env;
}, {}),
};
return { raw, stringified };
};