From 11cbab45fb42f12ac6e80b89e4f3bcdbde276848 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Wed, 11 May 2022 10:23:55 -0500 Subject: [PATCH] fix: allow Astro to pass `process.env` variables to `import.meta.env` (#3327) --- src/vite-plugin-env/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/vite-plugin-env/index.ts b/src/vite-plugin-env/index.ts index 7a716019e6f1..bc3447214a3c 100644 --- a/src/vite-plugin-env/index.ts +++ b/src/vite-plugin-env/index.ts @@ -22,9 +22,6 @@ function getPrivateEnv(viteConfig: vite.ResolvedConfig, astroConfig: AstroConfig '' ); const privateKeys = Object.keys(fullEnv).filter((key) => { - // don't expose any variables also on `process.env` - // note: this filters out `CLI_ARGS=1` passed to node! - if (typeof process.env[key] !== 'undefined') return false; // don't inject `PUBLIC_` variables, Vite handles that for us for (const envPrefix of envPrefixes) { @@ -37,7 +34,10 @@ function getPrivateEnv(viteConfig: vite.ResolvedConfig, astroConfig: AstroConfig if (privateKeys.length === 0) { return null; } - return Object.fromEntries(privateKeys.map((key) => [key, JSON.stringify(fullEnv[key])])); + return Object.fromEntries(privateKeys.map((key) => { + if (typeof process.env[key] !== 'undefined') return [key, `process.env.${key}`]; + return [key, JSON.stringify(fullEnv[key])] + })); } function getReferencedPrivateKeys(source: string, privateEnv: Record): Set {