Skip to content

Commit b626ef1

Browse files
committed
feat: make env variables available in HTML template
1 parent 6efabe1 commit b626ef1

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

packages/@vue/cli-service/lib/config/base.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ module.exports = (api, options) => {
123123
webpackConfig
124124
.plugin('html')
125125
.use(require('html-webpack-plugin'), [
126-
fs.existsSync(htmlPath) ? { template: htmlPath } : {}
126+
Object.assign(
127+
fs.existsSync(htmlPath) ? { template: htmlPath } : {},
128+
// expose client env to html template
129+
{ env: resolveClientEnv(options.baseUrl, true /* raw */) }
130+
)
127131
])
128132

129133
webpackConfig

packages/@vue/cli-service/lib/util/resolveClientEnv.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
const prefixRE = /^VUE_APP_/
22

3-
module.exports = function resolveClientEnv (publicPath) {
3+
module.exports = function resolveClientEnv (publicPath, raw) {
44
const env = {}
55
Object.keys(process.env).forEach(key => {
66
if (prefixRE.test(key) || key === 'NODE_ENV') {
7-
env[key] = JSON.stringify(process.env[key])
7+
env[key] = process.env[key]
88
}
99
})
10-
env.BASE_URL = JSON.stringify(publicPath)
10+
env.BASE_URL = publicPath
11+
12+
if (raw) {
13+
return env
14+
}
15+
16+
for (const key in env) {
17+
env[key] = JSON.stringify(env[key])
18+
}
1119
return {
1220
'process.env': env
1321
}

0 commit comments

Comments
 (0)