Skip to content

Commit 9b124cc

Browse files
committed
feat: disable minify by default in development
1 parent 71582a2 commit 9b124cc

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
@@ -407,6 +407,7 @@ export function resolveBuildEnvironmentOptions(
407407
raw: BuildEnvironmentOptions,
408408
logger: Logger,
409409
consumer: 'client' | 'server' | undefined,
410+
isProduction: boolean,
410411
): ResolvedBuildEnvironmentOptions {
411412
const deprecatedPolyfillModulePreload = raw.polyfillModulePreload
412413
const { polyfillModulePreload, ...rest } = raw
@@ -427,7 +428,7 @@ export function resolveBuildEnvironmentOptions(
427428
{
428429
...buildEnvironmentOptionsDefaults,
429430
cssCodeSplit: !raw.lib,
430-
minify: consumer === 'server' ? false : 'oxc',
431+
minify: !isProduction || consumer === 'server' ? false : 'oxc',
431432
rollupOptions: {},
432433
rolldownOptions: undefined,
433434
ssr: consumer === 'server',

packages/vite/src/node/config.ts

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@ function resolveEnvironmentOptions(
838838
preserveSymlinks: boolean,
839839
forceOptimizeDeps: boolean | undefined,
840840
logger: Logger,
841+
isProduction: boolean,
841842
environmentName: string,
842843
// Backward compatibility
843844
isSsrTargetWebworkerSet?: boolean,
@@ -901,6 +902,7 @@ function resolveEnvironmentOptions(
901902
options.build ?? {},
902903
logger,
903904
consumer,
905+
isProduction,
904906
),
905907
plugins: undefined!, // to be resolved later
906908
// will be set by `setOptimizeDepsPluginNames` later
@@ -1485,6 +1487,36 @@ export async function resolveConfig(
14851487
config.ssr?.target === 'webworker',
14861488
)
14871489

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+
14881520
// Backward compatibility: merge config.environments.client.resolve back into config.resolve
14891521
config.resolve ??= {}
14901522
config.resolve.conditions = config.environments.client.resolve?.conditions
@@ -1500,6 +1532,7 @@ export async function resolveConfig(
15001532
resolvedDefaultResolve.preserveSymlinks,
15011533
inlineConfig.forceOptimizeDeps,
15021534
logger,
1535+
isProduction,
15031536
environmentName,
15041537
config.ssr?.target === 'webworker',
15051538
config.server?.preTransformRequests,
@@ -1524,6 +1557,7 @@ export async function resolveConfig(
15241557
config.build ?? {},
15251558
logger,
15261559
undefined,
1560+
isProduction,
15271561
)
15281562

15291563
// Backward compatibility: merge config.environments.ssr back into config.ssr
@@ -1544,36 +1578,6 @@ export async function resolveConfig(
15441578
resolvedDefaultResolve.preserveSymlinks,
15451579
)
15461580

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-
15771581
// resolve public base url
15781582
const relativeBaseShortcut = config.base === '' || config.base === './'
15791583

0 commit comments

Comments
 (0)