Skip to content

Commit 7297cbb

Browse files
committed
feat: disable minify by default in development
1 parent 9572acb commit 7297cbb

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

packages/vite/src/node/__tests__/build.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { fileURLToPath } from 'node:url'
33
import { stripVTControlCharacters } from 'node:util'
44
import fsp from 'node:fs/promises'
55
import colors from 'picocolors'
6-
import { afterEach, describe, expect, test, vi } from 'vitest'
6+
import { afterEach, describe, expect, onTestFinished, test, vi } from 'vitest'
77
import type {
88
LogLevel,
99
OutputChunk,
@@ -809,6 +809,13 @@ test('default sharedConfigBuild true on build api', async () => {
809809
test.for([true, false])(
810810
'minify per environment (builder.sharedPlugins: %s)',
811811
async (sharedPlugins) => {
812+
const _nodeEnv = process.env.NODE_ENV
813+
// Overriding the NODE_ENV set by vitest
814+
process.env.NODE_ENV = ''
815+
onTestFinished(() => {
816+
process.env.NODE_ENV = _nodeEnv
817+
})
818+
812819
const root = resolve(__dirname, 'fixtures/shared-plugins/minify')
813820
const builder = await createBuilder({
814821
root,

packages/vite/src/node/build.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ export function resolveBuildEnvironmentOptions(
401401
raw: BuildEnvironmentOptions,
402402
logger: Logger,
403403
consumer: 'client' | 'server' | undefined,
404+
isProduction: boolean,
404405
): ResolvedBuildEnvironmentOptions {
405406
const deprecatedPolyfillModulePreload = raw.polyfillModulePreload
406407
const { polyfillModulePreload, ...rest } = raw
@@ -421,7 +422,7 @@ export function resolveBuildEnvironmentOptions(
421422
{
422423
...buildEnvironmentOptionsDefaults,
423424
cssCodeSplit: !raw.lib,
424-
minify: consumer === 'server' ? false : 'oxc',
425+
minify: !isProduction || consumer === 'server' ? false : 'oxc',
425426
rollupOptions: {
426427
platform: consumer === 'server' ? 'node' : 'browser',
427428
},

packages/vite/src/node/config.ts

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ function resolveEnvironmentOptions(
824824
preserveSymlinks: boolean,
825825
forceOptimizeDeps: boolean | undefined,
826826
logger: Logger,
827+
isProduction: boolean,
827828
environmentName: string,
828829
// Backward compatibility
829830
isSsrTargetWebworkerSet?: boolean,
@@ -887,6 +888,7 @@ function resolveEnvironmentOptions(
887888
options.build ?? {},
888889
logger,
889890
consumer,
891+
isProduction,
890892
),
891893
plugins: undefined!, // to be resolved later
892894
// will be set by `setOptimizeDepsPluginNames` later
@@ -1456,6 +1458,36 @@ export async function resolveConfig(
14561458
config.ssr?.target === 'webworker',
14571459
)
14581460

1461+
// load .env files
1462+
// Backward compatibility: set envDir to false when envFile is false
1463+
let envDir = config.envFile === false ? false : config.envDir
1464+
if (envDir !== false) {
1465+
envDir = config.envDir
1466+
? normalizePath(path.resolve(resolvedRoot, config.envDir))
1467+
: resolvedRoot
1468+
}
1469+
1470+
const userEnv = loadEnv(mode, envDir, resolveEnvPrefix(config))
1471+
1472+
// Note it is possible for user to have a custom mode, e.g. `staging` where
1473+
// development-like behavior is expected. This is indicated by NODE_ENV=development
1474+
// loaded from `.staging.env` and set by us as VITE_USER_NODE_ENV
1475+
const userNodeEnv = process.env.VITE_USER_NODE_ENV
1476+
if (!isNodeEnvSet && userNodeEnv) {
1477+
if (userNodeEnv === 'development') {
1478+
process.env.NODE_ENV = 'development'
1479+
} else {
1480+
// NODE_ENV=production is not supported as it could break HMR in dev for frameworks like Vue
1481+
logger.warn(
1482+
`NODE_ENV=${userNodeEnv} is not supported in the .env file. ` +
1483+
`Only NODE_ENV=development is supported to create a development build of your project. ` +
1484+
`If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.`,
1485+
)
1486+
}
1487+
}
1488+
1489+
const isProduction = process.env.NODE_ENV === 'production'
1490+
14591491
// Backward compatibility: merge config.environments.client.resolve back into config.resolve
14601492
config.resolve ??= {}
14611493
config.resolve.conditions = config.environments.client.resolve?.conditions
@@ -1471,6 +1503,7 @@ export async function resolveConfig(
14711503
resolvedDefaultResolve.preserveSymlinks,
14721504
inlineConfig.forceOptimizeDeps,
14731505
logger,
1506+
isProduction,
14741507
environmentName,
14751508
config.ssr?.target === 'webworker',
14761509
config.server?.preTransformRequests,
@@ -1495,6 +1528,7 @@ export async function resolveConfig(
14951528
config.build ?? {},
14961529
logger,
14971530
undefined,
1531+
isProduction,
14981532
)
14991533

15001534
// Backward compatibility: merge config.environments.ssr back into config.ssr
@@ -1515,36 +1549,6 @@ export async function resolveConfig(
15151549
resolvedDefaultResolve.preserveSymlinks,
15161550
)
15171551

1518-
// load .env files
1519-
// Backward compatibility: set envDir to false when envFile is false
1520-
let envDir = config.envFile === false ? false : config.envDir
1521-
if (envDir !== false) {
1522-
envDir = config.envDir
1523-
? normalizePath(path.resolve(resolvedRoot, config.envDir))
1524-
: resolvedRoot
1525-
}
1526-
1527-
const userEnv = loadEnv(mode, envDir, resolveEnvPrefix(config))
1528-
1529-
// Note it is possible for user to have a custom mode, e.g. `staging` where
1530-
// development-like behavior is expected. This is indicated by NODE_ENV=development
1531-
// loaded from `.staging.env` and set by us as VITE_USER_NODE_ENV
1532-
const userNodeEnv = process.env.VITE_USER_NODE_ENV
1533-
if (!isNodeEnvSet && userNodeEnv) {
1534-
if (userNodeEnv === 'development') {
1535-
process.env.NODE_ENV = 'development'
1536-
} else {
1537-
// NODE_ENV=production is not supported as it could break HMR in dev for frameworks like Vue
1538-
logger.warn(
1539-
`NODE_ENV=${userNodeEnv} is not supported in the .env file. ` +
1540-
`Only NODE_ENV=development is supported to create a development build of your project. ` +
1541-
`If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.`,
1542-
)
1543-
}
1544-
}
1545-
1546-
const isProduction = process.env.NODE_ENV === 'production'
1547-
15481552
// resolve public base url
15491553
const relativeBaseShortcut = config.base === '' || config.base === './'
15501554

0 commit comments

Comments
 (0)