From 4d4e7e257eaec15b32ee493eaa526c5767466b54 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 31 Jan 2022 21:23:36 +0000 Subject: [PATCH] feat: support overriding nested runtime config --- package.json | 1 + src/runtime/app/config.ts | 18 +++++++++++------- yarn.lock | 1 + 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 45bf1a1aa1..987c025a73 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "rollup": "^2.66.1", "rollup-plugin-terser": "^7.0.2", "rollup-plugin-visualizer": "^5.5.4", + "scule": "^0.2.1", "serve-placeholder": "^1.2.4", "serve-static": "^1.14.2", "std-env": "^3.0.1", diff --git a/src/runtime/app/config.ts b/src/runtime/app/config.ts index 548937df84..11f3bc8854 100644 --- a/src/runtime/app/config.ts +++ b/src/runtime/app/config.ts @@ -1,5 +1,6 @@ import destr from 'destr' import defu from 'defu' +import { snakeCase } from 'scule' // Bundled runtime config (injected by nitro) const _runtimeConfig = process.env.RUNTIME_CONFIG as any @@ -7,16 +8,19 @@ const _runtimeConfig = process.env.RUNTIME_CONFIG as any // Allow override from process.env and deserialize for (const type of ['private', 'public']) { for (const key in _runtimeConfig[type]) { - _runtimeConfig[type][key] = destr(process.env[key] || _runtimeConfig[type][key]) + // baseURL can be overridden by BASE_URL + const envKey = snakeCase(key).toUpperCase() + _runtimeConfig[type][key] = destr(process.env[envKey] || _runtimeConfig[type][key]) + if (_runtimeConfig[type][key] && typeof _runtimeConfig[type][key] === 'object') { + for (const subkey in _runtimeConfig[type][key]) { + // key: { subKey } can be overridden by KEY_SUB_KEY` + const envKeyName = `${envKey}_${snakeCase(subkey).toUpperCase()}` + _runtimeConfig[type][key][subkey] = destr(process.env[envKeyName]) || _runtimeConfig[type][key][subkey] + } + } } } -// Load dynamic app configuration -const appConfig = _runtimeConfig.public.app -appConfig.baseURL = process.env.NUXT_APP_BASE_URL || appConfig.baseURL -appConfig.cdnURL = process.env.NUXT_APP_CDN_URL || appConfig.cdnURL -appConfig.buildAssetsDir = process.env.NUXT_APP_BUILD_ASSETS_DIR || appConfig.buildAssetsDir - // Named exports export const privateConfig = deepFreeze(defu(_runtimeConfig.private, _runtimeConfig.public)) export const publicConfig = deepFreeze(_runtimeConfig.public) diff --git a/yarn.lock b/yarn.lock index 981d587b77..2af2fb2ade 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4533,6 +4533,7 @@ __metadata: rollup: ^2.66.1 rollup-plugin-terser: ^7.0.2 rollup-plugin-visualizer: ^5.5.4 + scule: ^0.2.1 serve-placeholder: ^1.2.4 serve-static: ^1.14.2 std-env: ^3.0.1