@@ -817,7 +817,6 @@ function resolveEnvironmentOptions(
817
817
preserveSymlinks : boolean ,
818
818
forceOptimizeDeps : boolean | undefined ,
819
819
logger : Logger ,
820
- isProduction : boolean ,
821
820
environmentName : string ,
822
821
// Backward compatibility
823
822
isSsrTargetWebworkerSet ?: boolean ,
@@ -881,7 +880,6 @@ function resolveEnvironmentOptions(
881
880
options . build ?? { } ,
882
881
logger ,
883
882
consumer ,
884
- isProduction ,
885
883
) ,
886
884
plugins : undefined ! , // to be resolved later
887
885
// will be set by `setOptimizeDepsPluginNames` later
@@ -1451,36 +1449,6 @@ export async function resolveConfig(
1451
1449
config . ssr ?. target === 'webworker' ,
1452
1450
)
1453
1451
1454
- // load .env files
1455
- // Backward compatibility: set envDir to false when envFile is false
1456
- let envDir = config . envFile === false ? false : config . envDir
1457
- if ( envDir !== false ) {
1458
- envDir = config . envDir
1459
- ? normalizePath ( path . resolve ( resolvedRoot , config . envDir ) )
1460
- : resolvedRoot
1461
- }
1462
-
1463
- const userEnv = loadEnv ( mode , envDir , resolveEnvPrefix ( config ) )
1464
-
1465
- // Note it is possible for user to have a custom mode, e.g. `staging` where
1466
- // development-like behavior is expected. This is indicated by NODE_ENV=development
1467
- // loaded from `.staging.env` and set by us as VITE_USER_NODE_ENV
1468
- const userNodeEnv = process . env . VITE_USER_NODE_ENV
1469
- if ( ! isNodeEnvSet && userNodeEnv ) {
1470
- if ( userNodeEnv === 'development' ) {
1471
- process . env . NODE_ENV = 'development'
1472
- } else {
1473
- // NODE_ENV=production is not supported as it could break HMR in dev for frameworks like Vue
1474
- logger . warn (
1475
- `NODE_ENV=${ userNodeEnv } is not supported in the .env file. ` +
1476
- `Only NODE_ENV=development is supported to create a development build of your project. ` +
1477
- `If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.` ,
1478
- )
1479
- }
1480
- }
1481
-
1482
- const isProduction = process . env . NODE_ENV === 'production'
1483
-
1484
1452
// Backward compatibility: merge config.environments.client.resolve back into config.resolve
1485
1453
config . resolve ??= { }
1486
1454
config . resolve . conditions = config . environments . client . resolve ?. conditions
@@ -1496,7 +1464,6 @@ export async function resolveConfig(
1496
1464
resolvedDefaultResolve . preserveSymlinks ,
1497
1465
inlineConfig . forceOptimizeDeps ,
1498
1466
logger ,
1499
- isProduction ,
1500
1467
environmentName ,
1501
1468
config . ssr ?. target === 'webworker' ,
1502
1469
config . server ?. preTransformRequests ,
@@ -1521,7 +1488,6 @@ export async function resolveConfig(
1521
1488
config . build ?? { } ,
1522
1489
logger ,
1523
1490
undefined ,
1524
- isProduction ,
1525
1491
)
1526
1492
1527
1493
// Backward compatibility: merge config.environments.ssr back into config.ssr
@@ -1542,6 +1508,36 @@ export async function resolveConfig(
1542
1508
resolvedDefaultResolve . preserveSymlinks ,
1543
1509
)
1544
1510
1511
+ // load .env files
1512
+ // Backward compatibility: set envDir to false when envFile is false
1513
+ let envDir = config . envFile === false ? false : config . envDir
1514
+ if ( envDir !== false ) {
1515
+ envDir = config . envDir
1516
+ ? normalizePath ( path . resolve ( resolvedRoot , config . envDir ) )
1517
+ : resolvedRoot
1518
+ }
1519
+
1520
+ const userEnv = loadEnv ( mode , envDir , resolveEnvPrefix ( config ) )
1521
+
1522
+ // Note it is possible for user to have a custom mode, e.g. `staging` where
1523
+ // development-like behavior is expected. This is indicated by NODE_ENV=development
1524
+ // loaded from `.staging.env` and set by us as VITE_USER_NODE_ENV
1525
+ const userNodeEnv = process . env . VITE_USER_NODE_ENV
1526
+ if ( ! isNodeEnvSet && userNodeEnv ) {
1527
+ if ( userNodeEnv === 'development' ) {
1528
+ process . env . NODE_ENV = 'development'
1529
+ } else {
1530
+ // NODE_ENV=production is not supported as it could break HMR in dev for frameworks like Vue
1531
+ logger . warn (
1532
+ `NODE_ENV=${ userNodeEnv } is not supported in the .env file. ` +
1533
+ `Only NODE_ENV=development is supported to create a development build of your project. ` +
1534
+ `If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.` ,
1535
+ )
1536
+ }
1537
+ }
1538
+
1539
+ const isProduction = process . env . NODE_ENV === 'production'
1540
+
1545
1541
// resolve public base url
1546
1542
const relativeBaseShortcut = config . base === '' || config . base === './'
1547
1543
0 commit comments