From 0d94bb04053d1d37995e211cd6447e2835d6474a Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Tue, 2 Sep 2025 09:55:21 +0200 Subject: [PATCH] fix(vite): inline `import.meta.env.*` (#3306) --- .../src/plugins/patches/inline_env_vars.ts | 12 ++++++++++++ .../src/plugins/patches/inline_env_vars_test.ts | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/packages/plugin-vite/src/plugins/patches/inline_env_vars.ts b/packages/plugin-vite/src/plugins/patches/inline_env_vars.ts index df73d5f5..dbe7175a 100644 --- a/packages/plugin-vite/src/plugins/patches/inline_env_vars.ts +++ b/packages/plugin-vite/src/plugins/patches/inline_env_vars.ts @@ -41,6 +41,18 @@ export function inlineEnvVarsPlugin(mode: string) { const name = path.node.property.name; replace(path, name); } + + // Check: import.meta.env.* + if ( + t.isIdentifier(path.node.property) && + t.isMemberExpression(path.node.object) && + t.isIdentifier(path.node.object.property) && + path.node.object.property.name === "env" && + t.isMetaProperty(path.node.object.object) + ) { + const name = path.node.property.name; + replace(path, name); + } }, CallExpression(path) { // Check: Deno.env.get("") diff --git a/packages/plugin-vite/src/plugins/patches/inline_env_vars_test.ts b/packages/plugin-vite/src/plugins/patches/inline_env_vars_test.ts index da0ff1f3..4365308a 100644 --- a/packages/plugin-vite/src/plugins/patches/inline_env_vars_test.ts +++ b/packages/plugin-vite/src/plugins/patches/inline_env_vars_test.ts @@ -69,3 +69,11 @@ Deno.test("env vars - inline const _ = Deno.env.get()", () => { expected: `const deno = "test";`, }); }); + +Deno.test("env vars - inline import.meta.env.FRESH_PUBLIC_FOO", () => { + using _ = usingEnv("FRESH_PUBLIC_FOO", "test"); + runTest({ + input: `() => import.meta.env.FRESH_PUBLIC_FOO;`, + expected: `() => "test";`, + }); +});