From d5fe92cd2a0be2b8636e876a81a63921a808afb2 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Wed, 9 Nov 2022 00:02:01 +0800 Subject: [PATCH] feat(env): support dotenv-expand to contains process env (#10370) --- packages/vite/src/node/env.ts | 13 ++++++++++--- playground/env/.env | 3 ++- playground/env/__tests__/env.spec.ts | 4 ++++ playground/env/index.html | 2 ++ playground/env/vite.config.js | 2 ++ 5 files changed, 20 insertions(+), 4 deletions(-) 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 @@

Environment Variables

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 @@

Environment Variables

text('.bool', typeof import.meta.env.VITE_BOOL) text('.node-env', process.env.NODE_ENV) text('.env-object', JSON.stringify(import.meta.env, null, 2)) + text('.expand', import.meta.env.VITE_EXPAND) function text(el, text) { document.querySelector(el).textContent = text diff --git a/playground/env/vite.config.js b/playground/env/vite.config.js index 7d3f15e41a6136..51be87cbec43f5 100644 --- a/playground/env/vite.config.js +++ b/playground/env/vite.config.js @@ -1,5 +1,7 @@ const { defineConfig } = require('vite') +process.env.EXPAND = 'expand' + module.exports = defineConfig({ base: '/env/', envPrefix: ['VITE_', 'CUSTOM_PREFIX_'],