@@ -824,6 +824,7 @@ function resolveEnvironmentOptions(
824824 preserveSymlinks : boolean ,
825825 forceOptimizeDeps : boolean | undefined ,
826826 logger : Logger ,
827+ isProduction : boolean ,
827828 environmentName : string ,
828829 // Backward compatibility
829830 isSsrTargetWebworkerSet ?: boolean ,
@@ -887,6 +888,7 @@ function resolveEnvironmentOptions(
887888 options . build ?? { } ,
888889 logger ,
889890 consumer ,
891+ isProduction ,
890892 ) ,
891893 plugins : undefined ! , // to be resolved later
892894 // will be set by `setOptimizeDepsPluginNames` later
@@ -1456,6 +1458,36 @@ export async function resolveConfig(
14561458 config . ssr ?. target === 'webworker' ,
14571459 )
14581460
1461+ // load .env files
1462+ // Backward compatibility: set envDir to false when envFile is false
1463+ let envDir = config . envFile === false ? false : config . envDir
1464+ if ( envDir !== false ) {
1465+ envDir = config . envDir
1466+ ? normalizePath ( path . resolve ( resolvedRoot , config . envDir ) )
1467+ : resolvedRoot
1468+ }
1469+
1470+ const userEnv = loadEnv ( mode , envDir , resolveEnvPrefix ( config ) )
1471+
1472+ // Note it is possible for user to have a custom mode, e.g. `staging` where
1473+ // development-like behavior is expected. This is indicated by NODE_ENV=development
1474+ // loaded from `.staging.env` and set by us as VITE_USER_NODE_ENV
1475+ const userNodeEnv = process . env . VITE_USER_NODE_ENV
1476+ if ( ! isNodeEnvSet && userNodeEnv ) {
1477+ if ( userNodeEnv === 'development' ) {
1478+ process . env . NODE_ENV = 'development'
1479+ } else {
1480+ // NODE_ENV=production is not supported as it could break HMR in dev for frameworks like Vue
1481+ logger . warn (
1482+ `NODE_ENV=${ userNodeEnv } is not supported in the .env file. ` +
1483+ `Only NODE_ENV=development is supported to create a development build of your project. ` +
1484+ `If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.` ,
1485+ )
1486+ }
1487+ }
1488+
1489+ const isProduction = process . env . NODE_ENV === 'production'
1490+
14591491 // Backward compatibility: merge config.environments.client.resolve back into config.resolve
14601492 config . resolve ??= { }
14611493 config . resolve . conditions = config . environments . client . resolve ?. conditions
@@ -1471,6 +1503,7 @@ export async function resolveConfig(
14711503 resolvedDefaultResolve . preserveSymlinks ,
14721504 inlineConfig . forceOptimizeDeps ,
14731505 logger ,
1506+ isProduction ,
14741507 environmentName ,
14751508 config . ssr ?. target === 'webworker' ,
14761509 config . server ?. preTransformRequests ,
@@ -1495,6 +1528,7 @@ export async function resolveConfig(
14951528 config . build ?? { } ,
14961529 logger ,
14971530 undefined ,
1531+ isProduction ,
14981532 )
14991533
15001534 // Backward compatibility: merge config.environments.ssr back into config.ssr
@@ -1515,36 +1549,6 @@ export async function resolveConfig(
15151549 resolvedDefaultResolve . preserveSymlinks ,
15161550 )
15171551
1518- // load .env files
1519- // Backward compatibility: set envDir to false when envFile is false
1520- let envDir = config . envFile === false ? false : config . envDir
1521- if ( envDir !== false ) {
1522- envDir = config . envDir
1523- ? normalizePath ( path . resolve ( resolvedRoot , config . envDir ) )
1524- : resolvedRoot
1525- }
1526-
1527- const userEnv = loadEnv ( mode , envDir , resolveEnvPrefix ( config ) )
1528-
1529- // Note it is possible for user to have a custom mode, e.g. `staging` where
1530- // development-like behavior is expected. This is indicated by NODE_ENV=development
1531- // loaded from `.staging.env` and set by us as VITE_USER_NODE_ENV
1532- const userNodeEnv = process . env . VITE_USER_NODE_ENV
1533- if ( ! isNodeEnvSet && userNodeEnv ) {
1534- if ( userNodeEnv === 'development' ) {
1535- process . env . NODE_ENV = 'development'
1536- } else {
1537- // NODE_ENV=production is not supported as it could break HMR in dev for frameworks like Vue
1538- logger . warn (
1539- `NODE_ENV=${ userNodeEnv } is not supported in the .env file. ` +
1540- `Only NODE_ENV=development is supported to create a development build of your project. ` +
1541- `If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.` ,
1542- )
1543- }
1544- }
1545-
1546- const isProduction = process . env . NODE_ENV === 'production'
1547-
15481552 // resolve public base url
15491553 const relativeBaseShortcut = config . base === '' || config . base === './'
15501554
0 commit comments