mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-12 03:38:07 +00:00
perf(kit): update env expansion regex to match nitro (#30766)
This commit is contained in:
parent
f90ed0ff67
commit
6b82e834e3
76
packages/kit/src/runtime-config.test.ts
Normal file
76
packages/kit/src/runtime-config.test.ts
Normal file
@ -0,0 +1,76 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { useRuntimeConfig } from './runtime-config'
|
||||
|
||||
const { useNuxt, klona } = vi.hoisted(() => ({ useNuxt: vi.fn(), klona: vi.fn() }))
|
||||
|
||||
vi.mock('./context', () => ({ useNuxt }))
|
||||
vi.mock('klona', () => ({ klona }))
|
||||
|
||||
const testCases = [
|
||||
{
|
||||
description:
|
||||
'should return runtime config with environment variables applied',
|
||||
runtimeConfig: {
|
||||
apiUrl: 'http://localhost',
|
||||
authUrl: 'http://auth.com',
|
||||
},
|
||||
envExpansion: true,
|
||||
env: {
|
||||
NITRO_API_URL: 'http://example.com',
|
||||
},
|
||||
expected: {
|
||||
apiUrl: 'http://example.com',
|
||||
authUrl: 'http://auth.com',
|
||||
},
|
||||
},
|
||||
{
|
||||
description: 'should expand environment variables in strings',
|
||||
runtimeConfig: {
|
||||
apiUrl: '{{BASE_URL}}/api',
|
||||
mail: '{{MAIL_SCHEME}}://{{MAIL_HOST}}:{{MAIL_PORT}}',
|
||||
},
|
||||
envExpansion: true,
|
||||
env: {
|
||||
BASE_URL: 'http://example.com',
|
||||
MAIL_SCHEME: 'http',
|
||||
MAIL_HOST: 'localhost',
|
||||
MAIL_PORT: '3366',
|
||||
},
|
||||
expected: {
|
||||
apiUrl: 'http://example.com/api',
|
||||
mail: 'http://localhost:3366',
|
||||
},
|
||||
},
|
||||
{
|
||||
description:
|
||||
'should not expand environment variables if envExpansion is false',
|
||||
runtimeConfig: {
|
||||
apiUrl: '{{BASE_URL}}/api',
|
||||
someUrl: '',
|
||||
},
|
||||
envExpansion: false,
|
||||
env: {
|
||||
BASE_URL: 'http://example1.com',
|
||||
NITRO_NOT_API_URL: 'http://example2.com',
|
||||
NUXT_SOME_URL: 'http://example3.com',
|
||||
},
|
||||
expected: {
|
||||
apiUrl: '{{BASE_URL}}/api',
|
||||
someUrl: 'http://example3.com',
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
describe('useRuntimeConfig', () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs()
|
||||
})
|
||||
|
||||
it.each(testCases)('$description', ({ runtimeConfig, envExpansion, env, expected }) => {
|
||||
useNuxt.mockReturnValue({ options: { nitro: { runtimeConfig, experimental: { envExpansion } } } })
|
||||
klona.mockReturnValue(runtimeConfig)
|
||||
Object.entries(env).forEach(([key, value]) => vi.stubEnv(key, value))
|
||||
|
||||
expect(useRuntimeConfig()).toEqual(expected)
|
||||
})
|
||||
})
|
@ -94,7 +94,7 @@ function applyEnv (
|
||||
return obj
|
||||
}
|
||||
|
||||
const envExpandRx = /\{\{(.*?)\}\}/g
|
||||
const envExpandRx = /\{\{([^{}]*)\}\}/g
|
||||
|
||||
function _expandFromEnv (value: string, env: Record<string, any> = process.env) {
|
||||
return value.replace(envExpandRx, (match, key) => {
|
||||
|
Loading…
Reference in New Issue
Block a user