@@ -22,6 +22,8 @@ import {
2222} from "@fresh/core/internal-dev" ;
2323import { checkImports } from "./plugins/verify_imports.ts" ;
2424import { isBuiltin } from "node:module" ;
25+ import { load as stdLoadEnv } from "@std/dotenv" ;
26+ import path from "node:path" ;
2527
2628export 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+ }
0 commit comments