Skip to content

Commit a6d5d4b

Browse files
committed
feat: disable minify by default in development
1 parent 00b476f commit a6d5d4b

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
@@ -817,6 +817,7 @@ function resolveEnvironmentOptions(
817817
preserveSymlinks: boolean,
818818
forceOptimizeDeps: boolean | undefined,
819819
logger: Logger,
820+
isProduction: boolean,
820821
environmentName: string,
821822
// Backward compatibility
822823
isSsrTargetWebworkerSet?: boolean,
@@ -880,6 +881,7 @@ function resolveEnvironmentOptions(
880881
options.build ?? {},
881882
logger,
882883
consumer,
884+
isProduction,
883885
),
884886
plugins: undefined!, // to be resolved later
885887
// will be set by `setOptimizeDepsPluginNames` later
@@ -1449,6 +1451,36 @@ export async function resolveConfig(
14491451
config.ssr?.target === 'webworker',
14501452
)
14511453

1454+
// load .env files
1455+
// Backward compatibility: set envDir to false when envFile is false
1456+
let envDir = config.envFile === false ? false : config.envDir
1457+
if (envDir !== false) {
1458+
envDir = config.envDir
1459+
? normalizePath(path.resolve(resolvedRoot, config.envDir))
1460+
: resolvedRoot
1461+
}
1462+
1463+
const userEnv = loadEnv(mode, envDir, resolveEnvPrefix(config))
1464+
1465+
// Note it is possible for user to have a custom mode, e.g. `staging` where
1466+
// development-like behavior is expected. This is indicated by NODE_ENV=development
1467+
// loaded from `.staging.env` and set by us as VITE_USER_NODE_ENV
1468+
const userNodeEnv = process.env.VITE_USER_NODE_ENV
1469+
if (!isNodeEnvSet && userNodeEnv) {
1470+
if (userNodeEnv === 'development') {
1471+
process.env.NODE_ENV = 'development'
1472+
} else {
1473+
// NODE_ENV=production is not supported as it could break HMR in dev for frameworks like Vue
1474+
logger.warn(
1475+
`NODE_ENV=${userNodeEnv} is not supported in the .env file. ` +
1476+
`Only NODE_ENV=development is supported to create a development build of your project. ` +
1477+
`If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.`,
1478+
)
1479+
}
1480+
}
1481+
1482+
const isProduction = process.env.NODE_ENV === 'production'
1483+
14521484
// Backward compatibility: merge config.environments.client.resolve back into config.resolve
14531485
config.resolve ??= {}
14541486
config.resolve.conditions = config.environments.client.resolve?.conditions
@@ -1464,6 +1496,7 @@ export async function resolveConfig(
14641496
resolvedDefaultResolve.preserveSymlinks,
14651497
inlineConfig.forceOptimizeDeps,
14661498
logger,
1499+
isProduction,
14671500
environmentName,
14681501
config.ssr?.target === 'webworker',
14691502
config.server?.preTransformRequests,
@@ -1488,6 +1521,7 @@ export async function resolveConfig(
14881521
config.build ?? {},
14891522
logger,
14901523
undefined,
1524+
isProduction,
14911525
)
14921526

14931527
// Backward compatibility: merge config.environments.ssr back into config.ssr
@@ -1508,36 +1542,6 @@ export async function resolveConfig(
15081542
resolvedDefaultResolve.preserveSymlinks,
15091543
)
15101544

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

0 commit comments

Comments
 (0)