fix(vite): inline FRESH_PUBLIC_* env vars (#3221)

This commit is contained in:
Marvin Hagemeister
2025-08-16 23:14:03 +02:00
committed by GitHub
parent 59549c9532
commit f80d049dca
7 changed files with 203 additions and 7 deletions
@@ -144,3 +144,26 @@ Deno.test({
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "vite dev - inline env vars",
fn: async () => {
await withDevServer(DEMO_DIR, async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/tests/env`, {
waitUntil: "networkidle2",
});
await page.locator(".ready").wait();
const res = await page.locator("pre").evaluate((el) =>
// deno-lint-ignore no-explicit-any
(el as any).textContent ?? ""
);
expect(JSON.parse(res)).toEqual({ deno: "foobar", nodeEnv: "foobar" });
});
}, { FRESH_PUBLIC_FOO: "foobar" });
},
sanitizeResources: false,
sanitizeOps: false,
});
+11 -1
View File
@@ -50,6 +50,7 @@ async function copyDir(from: string, to: string) {
export async function withDevServer(
fixtureDir: string,
fn: (address: string, dir: string) => void | Promise<void>,
env: Record<string, string> = {},
) {
await using tmp = await withTmpDir({
dir: path.join(import.meta.dirname!, ".."),
@@ -72,7 +73,7 @@ export default defineConfig({
);
await withChildProcessServer(
{ cwd: tmp.dir, args: ["run", "-A", "npm:vite", "--port", "0"] },
{ cwd: tmp.dir, args: ["run", "-A", "npm:vite", "--port", "0"], env },
async (address) => await fn(address, tmp.dir),
);
}
@@ -110,3 +111,12 @@ export async function buildVite(fixtureDir: string) {
},
};
}
export function usingEnv(name: string, value: string) {
Deno.env.set(name, value);
return {
[Symbol.dispose]: () => {
Deno.env.delete(name);
},
};
}