@@ -18,10 +18,10 @@ export function loadEnv(
1818 prefixes = arraify ( prefixes )
1919 const env : Record < string , string > = { }
2020 const envFiles = [
21- /** mode local file */ `.env.${ mode } .local` ,
22- /** mode file */ `.env.${ mode } ` ,
21+ /** default file */ `.env` ,
2322 /** local file */ `.env.local` ,
24- /** default file */ `.env`
23+ /** mode file */ `.env.${ mode } ` ,
24+ /** mode local file */ `.env.${ mode } .local`
2525 ]
2626
2727 // check if there are actual env variables starting with VITE_*
@@ -35,35 +35,38 @@ export function loadEnv(
3535 }
3636 }
3737
38- for ( const file of envFiles ) {
39- const path = lookupFile ( envDir , [ file ] , { pathOnly : true , rootDir : envDir } )
40- if ( path ) {
41- const parsed = dotenv . parse ( fs . readFileSync ( path ) , {
42- debug : process . env . DEBUG ?. includes ( 'vite:dotenv' ) || undefined
38+ const parsed = Object . fromEntries (
39+ envFiles . flatMap ( ( file ) => {
40+ const path = lookupFile ( envDir , [ file ] , {
41+ pathOnly : true ,
42+ rootDir : envDir
4343 } )
44+ if ( ! path ) return [ ]
45+ return Object . entries (
46+ dotenv . parse ( fs . readFileSync ( path ) , {
47+ debug : process . env . DEBUG ?. includes ( 'vite:dotenv' )
48+ } )
49+ )
50+ } )
51+ )
4452
45- // let environment variables use each other
46- dotenvExpand ( {
47- parsed,
48- // prevent process.env mutation
49- ignoreProcessEnv : true
50- } as any )
53+ // let environment variables use each other
54+ dotenvExpand ( {
55+ parsed,
56+ // prevent process.env mutation
57+ ignoreProcessEnv : true
58+ } as any )
5159
52- // only keys that start with prefix are exposed to client
53- for ( const [ key , value ] of Object . entries ( parsed ) ) {
54- if (
55- prefixes . some ( ( prefix ) => key . startsWith ( prefix ) ) &&
56- env [ key ] === undefined
57- ) {
58- env [ key ] = value
59- } else if (
60- key === 'NODE_ENV' &&
61- process . env . VITE_USER_NODE_ENV === undefined
62- ) {
63- // NODE_ENV override in .env file
64- process . env . VITE_USER_NODE_ENV = value
65- }
66- }
60+ // only keys that start with prefix are exposed to client
61+ for ( const [ key , value ] of Object . entries ( parsed ) ) {
62+ if ( prefixes . some ( ( prefix ) => key . startsWith ( prefix ) ) ) {
63+ env [ key ] = value
64+ } else if (
65+ key === 'NODE_ENV' &&
66+ process . env . VITE_USER_NODE_ENV === undefined
67+ ) {
68+ // NODE_ENV override in .env file
69+ process . env . VITE_USER_NODE_ENV = value
6770 }
6871 }
6972 return env
0 commit comments