mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 06:05:11 +00:00
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:
parent
2383bcbd0f
commit
6cbcd59d4e
@ -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"
|
||||
|
@ -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) {
|
||||
|
@ -25,7 +25,7 @@ describe('basic ssr', () => {
|
||||
|
||||
expect(payload.data[0].serverConfig).toMatchObject({
|
||||
baseURL: 'https://google.com/api',
|
||||
API_SECRET: '1234'
|
||||
API_SECRET: 1234
|
||||
})
|
||||
})
|
||||
|
||||
|
3
test/fixtures/runtime-config/nuxt.config.js
vendored
3
test/fixtures/runtime-config/nuxt.config.js
vendored
@ -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}',
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user