diff --git a/packages/vite/src/node/plugins/clientInjections.ts b/packages/vite/src/node/plugins/clientInjections.ts index eb5c7183c2ac59..84811dfc10df2a 100644 --- a/packages/vite/src/node/plugins/clientInjections.ts +++ b/packages/vite/src/node/plugins/clientInjections.ts @@ -55,7 +55,8 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin { // avoiding inconsistencies between dev and build return code.replace( /\bprocess\.env\.NODE_ENV\b/g, - JSON.stringify(config.mode) + config.define?.['process.env.NODE_ENV'] || + JSON.stringify(process.env.NODE_ENV || config.mode) ) } } diff --git a/playground/define/__tests__/define.spec.ts b/playground/define/__tests__/define.spec.ts index b2eb571734cc54..695d210a822ed6 100644 --- a/playground/define/__tests__/define.spec.ts +++ b/playground/define/__tests__/define.spec.ts @@ -14,6 +14,9 @@ test('string', async () => { expect(await page.textContent('.object')).toBe( JSON.stringify(defines.__OBJ__, null, 2) ) + expect(await page.textContent('.process-node-env')).toBe( + JSON.parse(defines['process.env.NODE_ENV']) + ) expect(await page.textContent('.env-var')).toBe( JSON.parse(defines['process.env.SOMEVAR']) ) diff --git a/playground/define/index.html b/playground/define/index.html index 97a7b9902a1dcb..1260b119149d28 100644 --- a/playground/define/index.html +++ b/playground/define/index.html @@ -6,6 +6,7 @@

Define

Boolean

Object

Env Var

+

process node env:

process as property:

spread object:

spread array:

@@ -23,6 +24,7 @@

Define

text('.number', __NUMBER__) text('.boolean', __BOOLEAN__) text('.object', JSON.stringify(__OBJ__, null, 2)) + text('.process-node-env', process.env.NODE_ENV) text('.env-var', process.env.SOMEVAR) text('.process-as-property', __OBJ__.process.env.SOMEVAR) text( diff --git a/playground/define/vite.config.js b/playground/define/vite.config.js index fb49d658f49e90..ec4a76136e8f47 100644 --- a/playground/define/vite.config.js +++ b/playground/define/vite.config.js @@ -15,6 +15,7 @@ module.exports = { } } }, + 'process.env.NODE_ENV': '"dev"', 'process.env.SOMEVAR': '"SOMEVAR"', $DOLLAR: 456, ÖUNICODE_LETTERɵ: 789, diff --git a/playground/env/__tests__/env.spec.ts b/playground/env/__tests__/env.spec.ts index f9955706101bb6..4583c9039116c8 100644 --- a/playground/env/__tests__/env.spec.ts +++ b/playground/env/__tests__/env.spec.ts @@ -37,7 +37,7 @@ test('inline variables', async () => { }) test('NODE_ENV', async () => { - expect(await page.textContent('.node-env')).toBe(mode) + expect(await page.textContent('.node-env')).toBe(process.env.NODE_ENV) }) test('env object', async () => { diff --git a/playground/worker/__tests__/es/es-worker.spec.ts b/playground/worker/__tests__/es/es-worker.spec.ts index b901a7deaeedbc..9959cc29aef0bd 100644 --- a/playground/worker/__tests__/es/es-worker.spec.ts +++ b/playground/worker/__tests__/es/es-worker.spec.ts @@ -8,7 +8,7 @@ test('normal', async () => { await untilUpdated(() => page.textContent('.pong'), 'pong') await untilUpdated( () => page.textContent('.mode'), - isBuild ? 'production' : 'development' + process.env.NODE_ENV // match workerImport.js ) await untilUpdated( () => page.textContent('.bundle-with-plugin'), diff --git a/playground/worker/__tests__/iife/worker.spec.ts b/playground/worker/__tests__/iife/worker.spec.ts index 0aba9611368737..d7b437c8a3272d 100644 --- a/playground/worker/__tests__/iife/worker.spec.ts +++ b/playground/worker/__tests__/iife/worker.spec.ts @@ -9,7 +9,7 @@ test('normal', async () => { await untilUpdated(() => page.textContent('.pong'), 'pong') await untilUpdated( () => page.textContent('.mode'), - isBuild ? 'production' : 'development' + process.env.NODE_ENV // match workerImport.js ) await untilUpdated( () => page.textContent('.bundle-with-plugin'), diff --git a/playground/worker/__tests__/relative-base/relative-base-worker.spec.ts b/playground/worker/__tests__/relative-base/relative-base-worker.spec.ts index 90c1e55798b51f..2b146075f38146 100644 --- a/playground/worker/__tests__/relative-base/relative-base-worker.spec.ts +++ b/playground/worker/__tests__/relative-base/relative-base-worker.spec.ts @@ -8,7 +8,7 @@ test('normal', async () => { await untilUpdated(() => page.textContent('.pong'), 'pong') await untilUpdated( () => page.textContent('.mode'), - isBuild ? 'production' : 'development' + process.env.NODE_ENV // match workerImport.js ) await untilUpdated( () => page.textContent('.bundle-with-plugin'),