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