@@ -18,10 +18,10 @@ export function loadEnv(
18
18
prefixes = arraify ( prefixes )
19
19
const env : Record < string , string > = { }
20
20
const envFiles = [
21
- /** mode local file */ `.env.${ mode } .local` ,
22
- /** mode file */ `.env.${ mode } ` ,
21
+ /** default file */ `.env` ,
23
22
/** local file */ `.env.local` ,
24
- /** default file */ `.env`
23
+ /** mode file */ `.env.${ mode } ` ,
24
+ /** mode local file */ `.env.${ mode } .local`
25
25
]
26
26
27
27
// check if there are actual env variables starting with VITE_*
@@ -35,35 +35,38 @@ export function loadEnv(
35
35
}
36
36
}
37
37
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
43
43
} )
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
+ )
44
52
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 )
51
59
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
67
70
}
68
71
}
69
72
return env
0 commit comments