fix(vite): .env files not being loaded (#3416)
Fixes https://github.com/denoland/fresh/issues/3364
This commit is contained in:
@@ -44,6 +44,7 @@
|
||||
"@fresh/build-id": "jsr:@fresh/build-id@^1.0.0",
|
||||
"@std/cli": "jsr:@std/cli@^1.0.19",
|
||||
"@std/collections": "jsr:@std/collections@^1.1.2",
|
||||
"@std/dotenv": "jsr:@std/dotenv@^0.225.5",
|
||||
"@std/http": "jsr:@std/http@^1.0.15",
|
||||
"@std/uuid": "jsr:@std/uuid@^1.0.7",
|
||||
"@types/node": "npm:@types/node@^24.3.0",
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
"jsr:@std/collections@^1.1.2": "1.1.2",
|
||||
"jsr:@std/crypto@^1.0.5": "1.0.5",
|
||||
"jsr:@std/data-structures@^1.0.9": "1.0.9",
|
||||
"jsr:@std/dotenv@~0.225.5": "0.225.5",
|
||||
"jsr:@std/encoding@1": "1.0.10",
|
||||
"jsr:@std/encoding@^1.0.10": "1.0.10",
|
||||
"jsr:@std/expect@^1.0.16": "1.0.17",
|
||||
@@ -199,6 +200,9 @@
|
||||
"@std/data-structures@1.0.9": {
|
||||
"integrity": "033d6e17e64bf1f84a614e647c1b015fa2576ae3312305821e1a4cb20674bb4d"
|
||||
},
|
||||
"@std/dotenv@0.225.5": {
|
||||
"integrity": "9ce6f9d0ec3311f74a32535aa1b8c62ed88b1ab91b7f0815797d77a6f60c922f"
|
||||
},
|
||||
"@std/encoding@1.0.10": {
|
||||
"integrity": "8783c6384a2d13abd5e9e87a7ae0520a30e9f56aeeaa3bdf910a3eaaf5c811a1"
|
||||
},
|
||||
@@ -4132,6 +4136,7 @@
|
||||
"jsr:@std/async@^1.0.13",
|
||||
"jsr:@std/cli@^1.0.19",
|
||||
"jsr:@std/collections@^1.1.2",
|
||||
"jsr:@std/dotenv@~0.225.5",
|
||||
"jsr:@std/encoding@1",
|
||||
"jsr:@std/expect@^1.0.16",
|
||||
"jsr:@std/fmt@^1.0.7",
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
MY_ENV="MY_ENV test value"
|
||||
VITE_MY_ENV="VITE_MY_ENV test value"
|
||||
@@ -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);
|
||||
};
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user