Skip to content

Commit

Permalink
feat: make env variables available in HTML template
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 29, 2018
1 parent 6efabe1 commit b626ef1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/@vue/cli-service/lib/config/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions packages/@vue/cli-service/lib/util/resolveClientEnv.js
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down

0 comments on commit b626ef1

Please sign in to comment.