Skip to content

Commit 798ccfa

Browse files
committed
feat: disable minify by default in development
1 parent 722f9ed commit 798ccfa

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
@@ -397,6 +397,7 @@ export function resolveBuildEnvironmentOptions(
397397
raw: BuildEnvironmentOptions,
398398
logger: Logger,
399399
consumer: 'client' | 'server' | undefined,
400+
isProduction: boolean,
400401
): ResolvedBuildEnvironmentOptions {
401402
const deprecatedPolyfillModulePreload = raw.polyfillModulePreload
402403
const { polyfillModulePreload, ...rest } = raw
@@ -417,7 +418,7 @@ export function resolveBuildEnvironmentOptions(
417418
{
418419
...buildEnvironmentOptionsDefaults,
419420
cssCodeSplit: !raw.lib,
420-
minify: consumer === 'server' ? false : 'oxc',
421+
minify: !isProduction || consumer === 'server' ? false : 'oxc',
421422
rollupOptions: {
422423
platform: consumer === 'server' ? 'node' : 'browser',
423424
},

packages/vite/src/node/config.ts

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ function resolveEnvironmentOptions(
812812
preserveSymlinks: boolean,
813813
forceOptimizeDeps: boolean | undefined,
814814
logger: Logger,
815+
isProduction: boolean,
815816
environmentName: string,
816817
// Backward compatibility
817818
isSsrTargetWebworkerSet?: boolean,
@@ -875,6 +876,7 @@ function resolveEnvironmentOptions(
875876
options.build ?? {},
876877
logger,
877878
consumer,
879+
isProduction,
878880
),
879881
plugins: undefined!, // to be resolved later
880882
// will be set by `setOptimizeDepsPluginNames` later
@@ -1444,6 +1446,36 @@ export async function resolveConfig(
14441446
config.ssr?.target === 'webworker',
14451447
)
14461448

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+
14471479
// Backward compatibility: merge config.environments.client.resolve back into config.resolve
14481480
config.resolve ??= {}
14491481
config.resolve.conditions = config.environments.client.resolve?.conditions
@@ -1459,6 +1491,7 @@ export async function resolveConfig(
14591491
resolvedDefaultResolve.preserveSymlinks,
14601492
inlineConfig.forceOptimizeDeps,
14611493
logger,
1494+
isProduction,
14621495
environmentName,
14631496
config.ssr?.target === 'webworker',
14641497
config.server?.preTransformRequests,
@@ -1483,6 +1516,7 @@ export async function resolveConfig(
14831516
config.build ?? {},
14841517
logger,
14851518
undefined,
1519+
isProduction,
14861520
)
14871521

14881522
// Backward compatibility: merge config.environments.ssr back into config.ssr
@@ -1503,36 +1537,6 @@ export async function resolveConfig(
15031537
resolvedDefaultResolve.preserveSymlinks,
15041538
)
15051539

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-
15361540
// resolve public base url
15371541
const relativeBaseShortcut = config.base === '' || config.base === './'
15381542

0 commit comments

Comments
 (0)