fix: handle process.env to have nested keys

This commit is contained in:
Sebastien Chopin 2017-09-05 11:15:01 +02:00
parent 1fcf3e685e
commit 20eb5a5eb0
5 changed files with 25 additions and 4 deletions

View File

@ -76,7 +76,7 @@ export default function webpackClientConfig () {
// Env object defined in nuxt.config.js // Env object defined in nuxt.config.js
let env = {} let env = {}
each(this.options.env, (value, key) => { each(this.options.env, (value, key) => {
env['process.env.' + key] = (typeof value === 'string' ? JSON.stringify(value) : value) env['process.env.' + key] = (['boolean', 'number'].indexOf(typeof value) !== -1 ? value : JSON.stringify(value))
}) })
// Webpack common plugins // Webpack common plugins

View File

@ -17,7 +17,7 @@ export default function webpackServerConfig () {
// env object defined in nuxt.config.js // env object defined in nuxt.config.js
let env = {} let env = {}
each(this.options.env, (value, key) => { each(this.options.env, (value, key) => {
env['process.env.' + key] = (typeof value === 'string' ? JSON.stringify(value) : value) env['process.env.' + key] = (['boolean', 'number'].indexOf(typeof value) !== -1 ? value : JSON.stringify(value))
}) })
config = Object.assign(config, { config = Object.assign(config, {

View File

@ -24,7 +24,15 @@ module.exports = {
env: { env: {
bool: true, bool: true,
num: 23, num: 23,
string: 'Nuxt.js' string: 'Nuxt.js',
object: {
bool: true,
string: 'ok',
num2: 8.23,
obj: {
again: true
}
}
}, },
build: { build: {
// extractCSS: true, // extractCSS: true,

View File

@ -1,11 +1,20 @@
<template> <template>
<pre>{{ env }}</pre> <div>
<pre>{{ env }}</pre>
<p>object:</p>
<pre>{{ processEnv }}</pre>
<nuxt-link to="/">Home</nuxt-link>
</div>
</template> </template>
<script> <script>
export default { export default {
layout: 'custom-env', layout: 'custom-env',
data() {
return { processEnv: process.env.object }
},
asyncData ({ env }) { asyncData ({ env }) {
delete env.object
return { env } return { env }
} }
} }

View File

@ -68,6 +68,10 @@ test('/test/env', async t => {
t.true(html.includes('"bool": true')) t.true(html.includes('"bool": true'))
t.true(html.includes('"num": 23')) t.true(html.includes('"num": 23'))
t.true(html.includes('"string": "Nuxt.js"')) t.true(html.includes('"string": "Nuxt.js"'))
t.true(html.includes('"bool": false'))
t.true(html.includes('"string": "ok"'))
t.true(html.includes('"num2": 8.23'))
t.true(html.includes('"obj": {'))
}) })
test('/test/error', async t => { test('/test/error', async t => {