feat: make env variables available in HTML template

This commit is contained in:
Evan You
2018-01-29 18:43:26 -05:00
parent 6efabe11b0
commit b626ef1a5b
2 changed files with 16 additions and 4 deletions
+5 -1
View File
@@ -123,7 +123,11 @@ module.exports = (api, options) => {
webpackConfig
.plugin('html')
.use(require('html-webpack-plugin'), [
fs.existsSync(htmlPath) ? { template: htmlPath } : {}
Object.assign(
fs.existsSync(htmlPath) ? { template: htmlPath } : {},
// expose client env to html template
{ env: resolveClientEnv(options.baseUrl, true /* raw */) }
)
])
webpackConfig
@@ -1,13 +1,21 @@
const prefixRE = /^VUE_APP_/
module.exports = function resolveClientEnv (publicPath) {
module.exports = function resolveClientEnv (publicPath, raw) {
const env = {}
Object.keys(process.env).forEach(key => {
if (prefixRE.test(key) || key === 'NODE_ENV') {
env[key] = JSON.stringify(process.env[key])
env[key] = process.env[key]
}
})
env.BASE_URL = JSON.stringify(publicPath)
env.BASE_URL = publicPath
if (raw) {
return env
}
for (const key in env) {
env[key] = JSON.stringify(env[key])
}
return {
'process.env': env
}