diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index edc516000fa57c..3bdb0620e7683f 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -755,13 +755,12 @@ async function prepareEsbuildOptimizerRun( if (optimizerContext.cancelled) return { context: undefined, idToExports } // esbuild automatically replaces process.env.NODE_ENV for platform 'browser' - // In lib mode, we need to keep process.env.NODE_ENV untouched, so to at build - // time we replace it by __vite_process_env_NODE_ENV. This placeholder will be - // later replaced by the define plugin + // But in lib mode, we need to keep process.env.NODE_ENV untouched const define = { - 'process.env.NODE_ENV': isBuild - ? '__vite_process_env_NODE_ENV' - : JSON.stringify(process.env.NODE_ENV || config.mode), + 'process.env.NODE_ENV': + isBuild && config.build.lib + ? 'process.env.NODE_ENV' + : JSON.stringify(process.env.NODE_ENV || config.mode), } const platform = diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index cce969843bfdde..c22f966257b12d 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -26,7 +26,6 @@ export function definePlugin(config: ResolvedConfig): Plugin { 'process.env.NODE_ENV': JSON.stringify(nodeEnv), 'global.process.env.NODE_ENV': JSON.stringify(nodeEnv), 'globalThis.process.env.NODE_ENV': JSON.stringify(nodeEnv), - __vite_process_env_NODE_ENV: JSON.stringify(nodeEnv), }) } @@ -69,9 +68,6 @@ export function definePlugin(config: ResolvedConfig): Plugin { } // Additional define fixes based on `ssr` value - if (isBuild && !replaceProcessEnv) { - define['__vite_process_env_NODE_ENV'] = 'process.env.NODE_ENV' - } if ('import.meta.env.SSR' in define) { define['import.meta.env.SSR'] = ssr + '' } @@ -86,7 +82,7 @@ export function definePlugin(config: ResolvedConfig): Plugin { // Create regex pattern as a fast check before running esbuild const patternKeys = Object.keys(userDefine) if (replaceProcessEnv && Object.keys(processEnv).length) { - patternKeys.push('process.env', '__vite_process_env_NODE_ENV') + patternKeys.push('process.env') } if (Object.keys(importMetaKeys).length) { patternKeys.push('import.meta.env', 'import.meta.hot') diff --git a/playground/define/__tests__/define.spec.ts b/playground/define/__tests__/define.spec.ts index 66f5a3371e8440..bdbabb9a52453e 100644 --- a/playground/define/__tests__/define.spec.ts +++ b/playground/define/__tests__/define.spec.ts @@ -72,11 +72,6 @@ test('ignores constants in string literals', async () => { '.ignores-string-literals .globalThis-process-env-NODE_ENV', ), ).toBe('globalThis.process.env.NODE_ENV') - expect( - await page.textContent( - '.ignores-string-literals .__vite_process_env_NODE_ENV', - ), - ).toBe('__vite_process_env_NODE_ENV') expect( await page.textContent('.ignores-string-literals .import-meta-hot'), ).toBe('import' + '.meta.hot') diff --git a/playground/define/index.html b/playground/define/index.html index 7b802867903e2f..4c56dd7674f7a1 100644 --- a/playground/define/index.html +++ b/playground/define/index.html @@ -37,10 +37,6 @@
-
- __vite_process_env_NODE_ENV
-
-
import.meta.hot
-
- __vite_process_env_NODE_ENV
-
-
import.meta.hot