@@ -2,9 +2,11 @@ import fs from 'node:fs'
2
2
import path from 'node:path'
3
3
import { parse } from 'dotenv'
4
4
import { type DotenvPopulateInput , expand } from 'dotenv-expand'
5
- import { arraify , normalizePath , tryStatSync } from './utils'
5
+ import { arraify , createDebugger , normalizePath , tryStatSync } from './utils'
6
6
import type { UserConfig } from './config'
7
7
8
+ const debug = createDebugger ( 'vite:env' )
9
+
8
10
export function getEnvFilesForMode ( mode : string , envDir : string ) : string [ ] {
9
11
return [
10
12
/** default file */ `.env` ,
@@ -19,6 +21,9 @@ export function loadEnv(
19
21
envDir : string ,
20
22
prefixes : string | string [ ] = 'VITE_' ,
21
23
) : Record < string , string > {
24
+ const start = performance . now ( )
25
+ const getTime = ( ) => `${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms`
26
+
22
27
if ( mode === 'local' ) {
23
28
throw new Error (
24
29
`"local" cannot be used as a mode name because it conflicts with ` +
@@ -29,6 +34,8 @@ export function loadEnv(
29
34
const env : Record < string , string > = { }
30
35
const envFiles = getEnvFilesForMode ( mode , envDir )
31
36
37
+ debug ?.( `loading env files: %O` , envFiles )
38
+
32
39
const parsed = Object . fromEntries (
33
40
envFiles . flatMap ( ( filePath ) => {
34
41
if ( ! tryStatSync ( filePath ) ?. isFile ( ) ) return [ ]
@@ -37,6 +44,8 @@ export function loadEnv(
37
44
} ) ,
38
45
)
39
46
47
+ debug ?.( `env files loaded in ${ getTime ( ) } ` )
48
+
40
49
// test NODE_ENV override before expand as otherwise process.env.NODE_ENV would override this
41
50
if ( parsed . NODE_ENV && process . env . VITE_USER_NODE_ENV === undefined ) {
42
51
process . env . VITE_USER_NODE_ENV = parsed . NODE_ENV
@@ -69,6 +78,8 @@ export function loadEnv(
69
78
}
70
79
}
71
80
81
+ debug ?.( `using resolved env: %O` , env )
82
+
72
83
return env
73
84
}
74
85
0 commit comments