feat(config): improved runtimeConfig (#7420)

* fix: ensure interpolate value is a string

* feat: allow native values from dotenv

* test: update test for regression on native value check
This commit is contained in:
Pooya Parsa 2020-05-27 12:38:39 +02:00 committed by GitHub
parent 2383bcbd0f
commit 6cbcd59d4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 7 deletions

View File

@ -13,7 +13,7 @@
"@nuxt/utils": "2.12.1",
"consola": "^2.12.1",
"defu": "^2.0.4",
"destr": "^0.1.4",
"destr": "^0.1.7",
"dotenv": "^8.2.0",
"esm": "^3.2.25",
"std-env": "^2.2.1"

View File

@ -5,6 +5,7 @@ import consola from 'consola'
import dotenv from 'dotenv'
import { clearRequireCache, scanRequireTree } from '@nuxt/utils'
import esm from 'esm'
import destr from 'destr'
import { defaultNuxtConfigFile } from './config'
export async function loadNuxtConfig ({
@ -98,7 +99,7 @@ export async function loadNuxtConfig ({
if (typeof options[c] === 'function') {
options[c] = options[c](env)
}
expand(options[c], env)
expand(options[c], env, destr)
}
}
}
@ -133,15 +134,18 @@ function loadEnv (envConfig, rootDir = process.cwd()) {
}
// Based on https://github.com/motdotla/dotenv-expand
function expand (target, source = {}) {
function expand (target, source = {}, parse = v => v) {
function getValue (key) {
// Source value 'wins' over target value
return source[key] !== undefined ? source[key] : (target[key] || '')
}
function interpolate (value) {
if (typeof value !== 'string') {
return value
}
const matches = value.match(/(.?\${?(?:[a-zA-Z0-9_:]+)?}?)/g) || []
return matches.reduce((newValue, match) => {
return parse(matches.reduce((newValue, match) => {
const parts = /(.?)\${?([a-zA-Z0-9_:]+)?}?/g.exec(match)
const prefix = parts[1]
@ -161,7 +165,7 @@ function expand (target, source = {}) {
}
return newValue.replace(replacePart, value)
}, value)
}, value))
}
for (const key in target) {

View File

@ -25,7 +25,7 @@ describe('basic ssr', () => {
expect(payload.data[0].serverConfig).toMatchObject({
baseURL: 'https://google.com/api',
API_SECRET: '1234'
API_SECRET: 1234
})
})

View File

@ -1,6 +1,7 @@
export default {
publicRuntimeConfig: {
baseURL: process.env.BASE_URL
baseURL: process.env.BASE_URL,
isFixture: true
},
privateRuntimeConfig: {
baseURL: '${PUBLIC_URL}${BASE_URL}',

View File

@ -4823,6 +4823,11 @@ destr@^0.1.4:
resolved "https://registry.npmjs.org/destr/-/destr-0.1.4.tgz#5ade43c2c02ffa7ad80e4d1516db309158d03d90"
integrity sha512-jGvlTgd/uZHtImTF8idU1FGu0Sg2/KBRp17HdsZ9dgZSkjt6lJRp/YW1gzHb1iEk0yJ4RrSQ3ujxbswa0gmwzQ==
destr@^0.1.7:
version "0.1.7"
resolved "https://registry.npmjs.org/destr/-/destr-0.1.7.tgz#630dcd9b78407759955a93b9536a76d4ab9510fc"
integrity sha512-QC4u2bsOmdMo8g4yo/WShyZg9FnHVDqS8KJaYNNb1Hq1cL0sJTT2e2YLRapntrF8tUNIeZlGt5Mq/+ebaNoZaw==
destroy@^1.0.4, destroy@~1.0.4:
version "1.0.4"
resolved "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"