@@ -838,6 +838,7 @@ function resolveEnvironmentOptions(
838
838
preserveSymlinks : boolean ,
839
839
forceOptimizeDeps : boolean | undefined ,
840
840
logger : Logger ,
841
+ isProduction : boolean ,
841
842
environmentName : string ,
842
843
// Backward compatibility
843
844
isSsrTargetWebworkerSet ?: boolean ,
@@ -901,6 +902,7 @@ function resolveEnvironmentOptions(
901
902
options . build ?? { } ,
902
903
logger ,
903
904
consumer ,
905
+ isProduction ,
904
906
) ,
905
907
plugins : undefined ! , // to be resolved later
906
908
// will be set by `setOptimizeDepsPluginNames` later
@@ -1485,6 +1487,36 @@ export async function resolveConfig(
1485
1487
config . ssr ?. target === 'webworker' ,
1486
1488
)
1487
1489
1490
+ // load .env files
1491
+ // Backward compatibility: set envDir to false when envFile is false
1492
+ let envDir = config . envFile === false ? false : config . envDir
1493
+ if ( envDir !== false ) {
1494
+ envDir = config . envDir
1495
+ ? normalizePath ( path . resolve ( resolvedRoot , config . envDir ) )
1496
+ : resolvedRoot
1497
+ }
1498
+
1499
+ const userEnv = loadEnv ( mode , envDir , resolveEnvPrefix ( config ) )
1500
+
1501
+ // Note it is possible for user to have a custom mode, e.g. `staging` where
1502
+ // development-like behavior is expected. This is indicated by NODE_ENV=development
1503
+ // loaded from `.staging.env` and set by us as VITE_USER_NODE_ENV
1504
+ const userNodeEnv = process . env . VITE_USER_NODE_ENV
1505
+ if ( ! isNodeEnvSet && userNodeEnv ) {
1506
+ if ( userNodeEnv === 'development' ) {
1507
+ process . env . NODE_ENV = 'development'
1508
+ } else {
1509
+ // NODE_ENV=production is not supported as it could break HMR in dev for frameworks like Vue
1510
+ logger . warn (
1511
+ `NODE_ENV=${ userNodeEnv } is not supported in the .env file. ` +
1512
+ `Only NODE_ENV=development is supported to create a development build of your project. ` +
1513
+ `If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.` ,
1514
+ )
1515
+ }
1516
+ }
1517
+
1518
+ const isProduction = process . env . NODE_ENV === 'production'
1519
+
1488
1520
// Backward compatibility: merge config.environments.client.resolve back into config.resolve
1489
1521
config . resolve ??= { }
1490
1522
config . resolve . conditions = config . environments . client . resolve ?. conditions
@@ -1500,6 +1532,7 @@ export async function resolveConfig(
1500
1532
resolvedDefaultResolve . preserveSymlinks ,
1501
1533
inlineConfig . forceOptimizeDeps ,
1502
1534
logger ,
1535
+ isProduction ,
1503
1536
environmentName ,
1504
1537
config . ssr ?. target === 'webworker' ,
1505
1538
config . server ?. preTransformRequests ,
@@ -1524,6 +1557,7 @@ export async function resolveConfig(
1524
1557
config . build ?? { } ,
1525
1558
logger ,
1526
1559
undefined ,
1560
+ isProduction ,
1527
1561
)
1528
1562
1529
1563
// Backward compatibility: merge config.environments.ssr back into config.ssr
@@ -1544,36 +1578,6 @@ export async function resolveConfig(
1544
1578
resolvedDefaultResolve . preserveSymlinks ,
1545
1579
)
1546
1580
1547
- // load .env files
1548
- // Backward compatibility: set envDir to false when envFile is false
1549
- let envDir = config . envFile === false ? false : config . envDir
1550
- if ( envDir !== false ) {
1551
- envDir = config . envDir
1552
- ? normalizePath ( path . resolve ( resolvedRoot , config . envDir ) )
1553
- : resolvedRoot
1554
- }
1555
-
1556
- const userEnv = loadEnv ( mode , envDir , resolveEnvPrefix ( config ) )
1557
-
1558
- // Note it is possible for user to have a custom mode, e.g. `staging` where
1559
- // development-like behavior is expected. This is indicated by NODE_ENV=development
1560
- // loaded from `.staging.env` and set by us as VITE_USER_NODE_ENV
1561
- const userNodeEnv = process . env . VITE_USER_NODE_ENV
1562
- if ( ! isNodeEnvSet && userNodeEnv ) {
1563
- if ( userNodeEnv === 'development' ) {
1564
- process . env . NODE_ENV = 'development'
1565
- } else {
1566
- // NODE_ENV=production is not supported as it could break HMR in dev for frameworks like Vue
1567
- logger . warn (
1568
- `NODE_ENV=${ userNodeEnv } is not supported in the .env file. ` +
1569
- `Only NODE_ENV=development is supported to create a development build of your project. ` +
1570
- `If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.` ,
1571
- )
1572
- }
1573
- }
1574
-
1575
- const isProduction = process . env . NODE_ENV === 'production'
1576
-
1577
1581
// resolve public base url
1578
1582
const relativeBaseShortcut = config . base === '' || config . base === './'
1579
1583
0 commit comments