fix(vite): .env files not being loaded (#3416)

Fixes https://github.com/denoland/fresh/issues/3364
This commit is contained in:
Marvin Hagemeister
2025-09-12 18:21:51 +02:00
committed by GitHub
parent dd41a8706b
commit ae3db58a99
8 changed files with 83 additions and 1 deletions
+2
View File
@@ -0,0 +1,2 @@
MY_ENV="MY_ENV test value"
VITE_MY_ENV="VITE_MY_ENV test value"
+2
View File
@@ -0,0 +1,2 @@
MY_LOCAL_ENV="MY_LOCAL_ENV test value"
VITE_MY_LOCAL_ENV="VITE_MY_LOCAL_ENV test value"
@@ -0,0 +1,10 @@
export const handler = () => {
const json = {
MY_ENV: Deno.env.get("MY_ENV"),
VITE_MY_ENV: Deno.env.get("VITE_MY_ENV"),
MY_LOCAL_ENV: Deno.env.get("MY_LOCAL_ENV"),
VITE_MY_LOCAL_ENV: Deno.env.get("VITE_MY_LOCAL_ENV"),
};
return Response.json(json);
};
+26 -1
View File
@@ -22,6 +22,8 @@ import {
} from "@fresh/core/internal-dev";
import { checkImports } from "./plugins/verify_imports.ts";
import { isBuiltin } from "node:module";
import { load as stdLoadEnv } from "@std/dotenv";
import path from "node:path";
export function fresh(config?: FreshViteConfig): Plugin[] {
const fConfig: ResolvedFreshViteConfig = {
@@ -48,10 +50,14 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
}
});
let isDev = false;
const plugins: Plugin[] = [
{
name: "fresh",
config(config, env) {
isDev = env.command === "serve";
return {
esbuild: {
jsx: "automatic",
@@ -151,7 +157,7 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
},
};
},
configResolved(vConfig) {
async configResolved(vConfig) {
// Run update check in background
updateCheck(UPDATE_INTERVAL).catch(() => {});
@@ -163,6 +169,17 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
const name = fConfig.namer.getUniqueName(specName);
fConfig.islandSpecifiers.set(spec, name);
});
const envDir = pathWithRoot(
vConfig.envDir || vConfig.root,
vConfig.root,
);
await loadEnvFile(path.join(envDir, ".env"));
await loadEnvFile(path.join(envDir, ".env.local"));
const mode = isDev ? "development" : "production";
await loadEnvFile(path.join(envDir, `.env.${mode}`));
await loadEnvFile(path.join(envDir, `.env.${mode}.local`));
},
},
serverEntryPlugin(fConfig),
@@ -190,3 +207,11 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
return plugins;
}
async function loadEnvFile(envPath: string) {
try {
await stdLoadEnv({ envPath, export: true });
} catch {
// Ignoe
}
}
+21
View File
@@ -453,3 +453,24 @@ Deno.test({
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "vite build - env files",
fn: async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
const res = await fetch(`${address}/tests/env_files`);
const json = await res.json();
expect(json).toEqual({
MY_ENV: "MY_ENV test value",
VITE_MY_ENV: "VITE_MY_ENV test value",
MY_LOCAL_ENV: "MY_LOCAL_ENV test value",
VITE_MY_LOCAL_ENV: "VITE_MY_LOCAL_ENV test value",
});
},
);
},
sanitizeOps: false,
sanitizeResources: false,
});
@@ -403,3 +403,19 @@ Deno.test({
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "vite dev - load .env files",
fn: async () => {
const res = await fetch(`${demoServer.address()}/tests/env_files`);
const json = await res.json();
expect(json).toEqual({
MY_ENV: "MY_ENV test value",
VITE_MY_ENV: "VITE_MY_ENV test value",
MY_LOCAL_ENV: "MY_LOCAL_ENV test value",
VITE_MY_LOCAL_ENV: "VITE_MY_LOCAL_ENV test value",
});
},
sanitizeOps: false,
sanitizeResources: false,
});