You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, Vite loads .env files in the following order:
.env # loaded in all cases
.env.local # loaded in all cases, ignored by git
.env.[mode] # only loaded in specified mode
.env.[mode].local # only loaded in specified mode, ignored by git
My team uses this structure for our .env files:
.env
.env.local
.env.dev
.env.staging
.env.prod
We use .env.local to toggle flags based on development needs without affecting the config for each mode/environment.
In this case, overriding environment variables locally works well enough if a mode is not passed to Vite explicitly as only the default .env + .env.local files will be loaded. However, when passing a mode to Vite explicitly, local overrides in .env.local will be clobbered by the higher precedent .env.[mode] environment variable. This means when switching modes locally, we need to remember to also rename .env.local to .env.[mode].local, which makes for clunky DX.
Suggested solution
I'd like to propose being able to configure the priority of .env files via config (i.e. envPriority). This is an example of how it could look:
Description
Currently, Vite loads
.env
files in the following order:My team uses this structure for our
.env
files:We use
.env.local
to toggle flags based on development needs without affecting the config for each mode/environment.In this case, overriding environment variables locally works well enough if a
mode
is not passed to Vite explicitly as only the default.env
+.env.local
files will be loaded. However, when passing amode
to Vite explicitly, local overrides in.env.local
will be clobbered by the higher precedent.env.[mode]
environment variable. This means when switching modes locally, we need to remember to also rename.env.local
to.env.[mode].local
, which makes for clunky DX.Suggested solution
I'd like to propose being able to configure the priority of
.env
files via config (i.e.envPriority
). This is an example of how it could look:This can then be passed directly to the environment loader:
vite/packages/vite/src/node/env.ts
Lines 8 to 15 in ccd1a4c
Thoughts?
Alternative
To work around this right now, we've patched Vite using
pnpm patch
to adjust the load priority for our use case:Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: