From 6cbcd59d4e54d9d51f09c28a8fe1872158a40b6b Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 27 May 2020 12:38:39 +0200 Subject: [PATCH] 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 --- packages/config/package.json | 2 +- packages/config/src/load.js | 12 ++++++++---- test/dev/runtime-config.test.js | 2 +- test/fixtures/runtime-config/nuxt.config.js | 3 ++- yarn.lock | 5 +++++ 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/config/package.json b/packages/config/package.json index 4772af0f7a..5efa12e8f3 100644 --- a/packages/config/package.json +++ b/packages/config/package.json @@ -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" diff --git a/packages/config/src/load.js b/packages/config/src/load.js index b417500fe3..88d429a52c 100644 --- a/packages/config/src/load.js +++ b/packages/config/src/load.js @@ -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) { diff --git a/test/dev/runtime-config.test.js b/test/dev/runtime-config.test.js index 56ab7f7e1a..016ef1233e 100644 --- a/test/dev/runtime-config.test.js +++ b/test/dev/runtime-config.test.js @@ -25,7 +25,7 @@ describe('basic ssr', () => { expect(payload.data[0].serverConfig).toMatchObject({ baseURL: 'https://google.com/api', - API_SECRET: '1234' + API_SECRET: 1234 }) }) diff --git a/test/fixtures/runtime-config/nuxt.config.js b/test/fixtures/runtime-config/nuxt.config.js index 52b757e3f5..a6f7f7c9c1 100644 --- a/test/fixtures/runtime-config/nuxt.config.js +++ b/test/fixtures/runtime-config/nuxt.config.js @@ -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}', diff --git a/yarn.lock b/yarn.lock index 6146ebf963..6c23a4786e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"