@@ -838,7 +838,6 @@ function resolveEnvironmentOptions(
838
838
preserveSymlinks : boolean ,
839
839
forceOptimizeDeps : boolean | undefined ,
840
840
logger : Logger ,
841
- isProduction : boolean ,
842
841
environmentName : string ,
843
842
// Backward compatibility
844
843
isSsrTargetWebworkerSet ?: boolean ,
@@ -902,7 +901,6 @@ function resolveEnvironmentOptions(
902
901
options . build ?? { } ,
903
902
logger ,
904
903
consumer ,
905
- isProduction ,
906
904
) ,
907
905
plugins : undefined ! , // to be resolved later
908
906
// will be set by `setOptimizeDepsPluginNames` later
@@ -1487,36 +1485,6 @@ export async function resolveConfig(
1487
1485
config . ssr ?. target === 'webworker' ,
1488
1486
)
1489
1487
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
-
1520
1488
// Backward compatibility: merge config.environments.client.resolve back into config.resolve
1521
1489
config . resolve ??= { }
1522
1490
config . resolve . conditions = config . environments . client . resolve ?. conditions
@@ -1532,7 +1500,6 @@ export async function resolveConfig(
1532
1500
resolvedDefaultResolve . preserveSymlinks ,
1533
1501
inlineConfig . forceOptimizeDeps ,
1534
1502
logger ,
1535
- isProduction ,
1536
1503
environmentName ,
1537
1504
config . ssr ?. target === 'webworker' ,
1538
1505
config . server ?. preTransformRequests ,
@@ -1557,7 +1524,6 @@ export async function resolveConfig(
1557
1524
config . build ?? { } ,
1558
1525
logger ,
1559
1526
undefined ,
1560
- isProduction ,
1561
1527
)
1562
1528
1563
1529
// Backward compatibility: merge config.environments.ssr back into config.ssr
@@ -1578,6 +1544,36 @@ export async function resolveConfig(
1578
1544
resolvedDefaultResolve . preserveSymlinks ,
1579
1545
)
1580
1546
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
+
1581
1577
// resolve public base url
1582
1578
const relativeBaseShortcut = config . base === '' || config . base === './'
1583
1579
0 commit comments