@@ -817,6 +817,7 @@ function resolveEnvironmentOptions(
817
817
preserveSymlinks : boolean ,
818
818
forceOptimizeDeps : boolean | undefined ,
819
819
logger : Logger ,
820
+ isProduction : boolean ,
820
821
environmentName : string ,
821
822
// Backward compatibility
822
823
isSsrTargetWebworkerSet ?: boolean ,
@@ -880,6 +881,7 @@ function resolveEnvironmentOptions(
880
881
options . build ?? { } ,
881
882
logger ,
882
883
consumer ,
884
+ isProduction ,
883
885
) ,
884
886
plugins : undefined ! , // to be resolved later
885
887
// will be set by `setOptimizeDepsPluginNames` later
@@ -1449,6 +1451,36 @@ export async function resolveConfig(
1449
1451
config . ssr ?. target === 'webworker' ,
1450
1452
)
1451
1453
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
+
1452
1484
// Backward compatibility: merge config.environments.client.resolve back into config.resolve
1453
1485
config . resolve ??= { }
1454
1486
config . resolve . conditions = config . environments . client . resolve ?. conditions
@@ -1464,6 +1496,7 @@ export async function resolveConfig(
1464
1496
resolvedDefaultResolve . preserveSymlinks ,
1465
1497
inlineConfig . forceOptimizeDeps ,
1466
1498
logger ,
1499
+ isProduction ,
1467
1500
environmentName ,
1468
1501
config . ssr ?. target === 'webworker' ,
1469
1502
config . server ?. preTransformRequests ,
@@ -1488,6 +1521,7 @@ export async function resolveConfig(
1488
1521
config . build ?? { } ,
1489
1522
logger ,
1490
1523
undefined ,
1524
+ isProduction ,
1491
1525
)
1492
1526
1493
1527
// Backward compatibility: merge config.environments.ssr back into config.ssr
@@ -1508,36 +1542,6 @@ export async function resolveConfig(
1508
1542
resolvedDefaultResolve . preserveSymlinks ,
1509
1543
)
1510
1544
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
-
1541
1545
// resolve public base url
1542
1546
const relativeBaseShortcut = config . base === '' || config . base === './'
1543
1547
0 commit comments