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