@@ -824,7 +824,6 @@ function resolveEnvironmentOptions(
824
824
preserveSymlinks : boolean ,
825
825
forceOptimizeDeps : boolean | undefined ,
826
826
logger : Logger ,
827
- isProduction : boolean ,
828
827
environmentName : string ,
829
828
// Backward compatibility
830
829
isSsrTargetWebworkerSet ?: boolean ,
@@ -888,7 +887,6 @@ function resolveEnvironmentOptions(
888
887
options . build ?? { } ,
889
888
logger ,
890
889
consumer ,
891
- isProduction ,
892
890
) ,
893
891
plugins : undefined ! , // to be resolved later
894
892
// will be set by `setOptimizeDepsPluginNames` later
@@ -1458,36 +1456,6 @@ export async function resolveConfig(
1458
1456
config . ssr ?. target === 'webworker' ,
1459
1457
)
1460
1458
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
-
1491
1459
// Backward compatibility: merge config.environments.client.resolve back into config.resolve
1492
1460
config . resolve ??= { }
1493
1461
config . resolve . conditions = config . environments . client . resolve ?. conditions
@@ -1503,7 +1471,6 @@ export async function resolveConfig(
1503
1471
resolvedDefaultResolve . preserveSymlinks ,
1504
1472
inlineConfig . forceOptimizeDeps ,
1505
1473
logger ,
1506
- isProduction ,
1507
1474
environmentName ,
1508
1475
config . ssr ?. target === 'webworker' ,
1509
1476
config . server ?. preTransformRequests ,
@@ -1528,7 +1495,6 @@ export async function resolveConfig(
1528
1495
config . build ?? { } ,
1529
1496
logger ,
1530
1497
undefined ,
1531
- isProduction ,
1532
1498
)
1533
1499
1534
1500
// Backward compatibility: merge config.environments.ssr back into config.ssr
@@ -1549,6 +1515,36 @@ export async function resolveConfig(
1549
1515
resolvedDefaultResolve . preserveSymlinks ,
1550
1516
)
1551
1517
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
+
1552
1548
// resolve public base url
1553
1549
const relativeBaseShortcut = config . base === '' || config . base === './'
1554
1550
0 commit comments