-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.utils.cjs
65 lines (58 loc) · 1.77 KB
/
webpack.utils.cjs
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
58
59
60
61
62
63
64
65
const path = require('path')
const fs = require('fs')
const dotenv = require('dotenv')
// 解析路径
const appDirectory = fs.realpathSync(process.cwd())
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath)
exports.resolveApp = resolveApp
// 处理环境变量
const envHandler = () => {
// 内置环境变量 外部自定义环境变量也扩展到该对象 剥离对process.env的污染
const builtInEnvs = {
PUBLIC_URL: '/',
PORT: '9000',
BUILD_PATH: 'dist',
GENERATE_SOURCEMAP: 'false'
}
// 加载自定义环境变量
const NODE_ENV = process.env.NODE_ENV
dotenv.config({ path: resolveApp('.env'), processEnv: builtInEnvs, override: true })
dotenv.config({
path: resolveApp(`.env${NODE_ENV ? `.${NODE_ENV}` : ''}`),
processEnv: builtInEnvs,
override: true
})
// 非 / 结尾
if (!/\/$/.test(builtInEnvs.PUBLIC_URL)) {
builtInEnvs.PUBLIC_URL = `${builtInEnvs.PUBLIC_URL}/`
}
// PUBLIC_URL
const PUBLIC_URL = builtInEnvs.PUBLIC_URL
// PORT
const PORT = builtInEnvs.PORT
// BUILD_PATH
const BUILD_PATH = builtInEnvs.BUILD_PATH
// GENERATE_SOURCEMAP
const GENERATE_SOURCEMAP = builtInEnvs.GENERATE_SOURCEMAP === 'true'
// 自定义环境变量 仅加载以 L_ 开头的
const prefixRE = /^L_/
const resolveClientEnv = (raw) => {
const env = {}
Object.keys(builtInEnvs).forEach((key) => {
if (prefixRE.test(key) || key === 'NODE_ENV' || builtInEnvs[key]) {
env[key] = builtInEnvs[key]
}
})
if (raw) {
return env
}
for (const key in env) {
env[key] = JSON.stringify(env[key])
}
return {
'process.env': env
}
}
return { PUBLIC_URL, PORT, BUILD_PATH, GENERATE_SOURCEMAP, resolveClientEnv }
}
exports.envHandler = envHandler