diff --git a/packages/vite/src/node/env.ts b/packages/vite/src/node/env.ts index 8d39707ddd7455..e611dadc0f8269 100644 --- a/packages/vite/src/node/env.ts +++ b/packages/vite/src/node/env.ts @@ -40,11 +40,18 @@ export function loadEnv( ) // let environment variables use each other - dotenvExpand({ - parsed, + const expandParsed = dotenvExpand({ + parsed: { + ...(process.env as any), + ...parsed + }, // prevent process.env mutation ignoreProcessEnv: true - } as any) + } as any).parsed! + + Object.keys(parsed).forEach((key) => { + parsed[key] = expandParsed[key] + }) // only keys that start with prefix are exposed to client for (const [key, value] of Object.entries(parsed)) { diff --git a/playground/env/.env b/playground/env/.env index ade94e520ffbcf..7d2085287f2c39 100644 --- a/playground/env/.env +++ b/playground/env/.env @@ -1,4 +1,5 @@ VITE_CUSTOM_ENV_VARIABLE=1 CUSTOM_PREFIX_ENV_VARIABLE=1 VITE_EFFECTIVE_MODE_FILE_NAME=.env -VITE_BOOL=true \ No newline at end of file +VITE_BOOL=true +VITE_EXPAND=$EXPAND diff --git a/playground/env/__tests__/env.spec.ts b/playground/env/__tests__/env.spec.ts index 0fe029422ecfbd..ee05fff1d3736c 100644 --- a/playground/env/__tests__/env.spec.ts +++ b/playground/env/__tests__/env.spec.ts @@ -45,6 +45,10 @@ test('NODE_ENV', async () => { expect(await page.textContent('.node-env')).toBe(process.env.NODE_ENV) }) +test('expand', async () => { + expect(await page.textContent('.expand')).toBe('expand') +}) + test('env object', async () => { const envText = await page.textContent('.env-object') expect(JSON.parse(envText)).toMatchObject({ diff --git a/playground/env/index.html b/playground/env/index.html index 46752dbc2c5ab1..6ccfd421945418 100644 --- a/playground/env/index.html +++ b/playground/env/index.html @@ -14,6 +14,7 @@
import.meta.env.VITE_INLINE:
typeof import.meta.env.VITE_BOOL:
process.env.NODE_ENV:
import.meta.env.VITE_EXPAND:
import.meta.env:
import.meta.url:
@@ -29,6 +30,7 @@