Skip to content

Commit ae3db58

Browse files
fix(vite): .env files not being loaded (#3416)
Fixes #3364
1 parent dd41a87 commit ae3db58

File tree

8 files changed

+83
-1
lines changed

8 files changed

+83
-1
lines changed

deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@fresh/build-id": "jsr:@fresh/build-id@^1.0.0",
4545
"@std/cli": "jsr:@std/cli@^1.0.19",
4646
"@std/collections": "jsr:@std/collections@^1.1.2",
47+
"@std/dotenv": "jsr:@std/dotenv@^0.225.5",
4748
"@std/http": "jsr:@std/http@^1.0.15",
4849
"@std/uuid": "jsr:@std/uuid@^1.0.7",
4950
"@types/node": "npm:@types/node@^24.3.0",

deno.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/plugin-vite/demo/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MY_ENV="MY_ENV test value"
2+
VITE_MY_ENV="VITE_MY_ENV test value"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MY_LOCAL_ENV="MY_LOCAL_ENV test value"
2+
VITE_MY_LOCAL_ENV="VITE_MY_LOCAL_ENV test value"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const handler = () => {
2+
const json = {
3+
MY_ENV: Deno.env.get("MY_ENV"),
4+
VITE_MY_ENV: Deno.env.get("VITE_MY_ENV"),
5+
MY_LOCAL_ENV: Deno.env.get("MY_LOCAL_ENV"),
6+
VITE_MY_LOCAL_ENV: Deno.env.get("VITE_MY_LOCAL_ENV"),
7+
};
8+
9+
return Response.json(json);
10+
};

packages/plugin-vite/src/mod.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
} from "@fresh/core/internal-dev";
2323
import { checkImports } from "./plugins/verify_imports.ts";
2424
import { isBuiltin } from "node:module";
25+
import { load as stdLoadEnv } from "@std/dotenv";
26+
import path from "node:path";
2527

2628
export function fresh(config?: FreshViteConfig): Plugin[] {
2729
const fConfig: ResolvedFreshViteConfig = {
@@ -48,10 +50,14 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
4850
}
4951
});
5052

53+
let isDev = false;
54+
5155
const plugins: Plugin[] = [
5256
{
5357
name: "fresh",
5458
config(config, env) {
59+
isDev = env.command === "serve";
60+
5561
return {
5662
esbuild: {
5763
jsx: "automatic",
@@ -151,7 +157,7 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
151157
},
152158
};
153159
},
154-
configResolved(vConfig) {
160+
async configResolved(vConfig) {
155161
// Run update check in background
156162
updateCheck(UPDATE_INTERVAL).catch(() => {});
157163

@@ -163,6 +169,17 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
163169
const name = fConfig.namer.getUniqueName(specName);
164170
fConfig.islandSpecifiers.set(spec, name);
165171
});
172+
173+
const envDir = pathWithRoot(
174+
vConfig.envDir || vConfig.root,
175+
vConfig.root,
176+
);
177+
178+
await loadEnvFile(path.join(envDir, ".env"));
179+
await loadEnvFile(path.join(envDir, ".env.local"));
180+
const mode = isDev ? "development" : "production";
181+
await loadEnvFile(path.join(envDir, `.env.${mode}`));
182+
await loadEnvFile(path.join(envDir, `.env.${mode}.local`));
166183
},
167184
},
168185
serverEntryPlugin(fConfig),
@@ -190,3 +207,11 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
190207

191208
return plugins;
192209
}
210+
211+
async function loadEnvFile(envPath: string) {
212+
try {
213+
await stdLoadEnv({ envPath, export: true });
214+
} catch {
215+
// Ignoe
216+
}
217+
}

packages/plugin-vite/tests/build_test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,24 @@ Deno.test({
453453
sanitizeOps: false,
454454
sanitizeResources: false,
455455
});
456+
457+
Deno.test({
458+
name: "vite build - env files",
459+
fn: async () => {
460+
await launchProd(
461+
{ cwd: viteResult.tmp },
462+
async (address) => {
463+
const res = await fetch(`${address}/tests/env_files`);
464+
const json = await res.json();
465+
expect(json).toEqual({
466+
MY_ENV: "MY_ENV test value",
467+
VITE_MY_ENV: "VITE_MY_ENV test value",
468+
MY_LOCAL_ENV: "MY_LOCAL_ENV test value",
469+
VITE_MY_LOCAL_ENV: "VITE_MY_LOCAL_ENV test value",
470+
});
471+
},
472+
);
473+
},
474+
sanitizeOps: false,
475+
sanitizeResources: false,
476+
});

packages/plugin-vite/tests/dev_server_test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,19 @@ Deno.test({
403403
sanitizeOps: false,
404404
sanitizeResources: false,
405405
});
406+
407+
Deno.test({
408+
name: "vite dev - load .env files",
409+
fn: async () => {
410+
const res = await fetch(`${demoServer.address()}/tests/env_files`);
411+
const json = await res.json();
412+
expect(json).toEqual({
413+
MY_ENV: "MY_ENV test value",
414+
VITE_MY_ENV: "VITE_MY_ENV test value",
415+
MY_LOCAL_ENV: "MY_LOCAL_ENV test value",
416+
VITE_MY_LOCAL_ENV: "VITE_MY_LOCAL_ENV test value",
417+
});
418+
},
419+
sanitizeOps: false,
420+
sanitizeResources: false,
421+
});

0 commit comments

Comments
 (0)