From 8226facc823f9b57b035ea7bc286e15cf543fd62 Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 25 Feb 2023 12:11:38 +0800 Subject: [PATCH 1/2] fix(client-inject): replace globalThis.process.env.NODE_ENV (fix #12185) --- packages/vite/src/node/plugins/clientInjections.ts | 2 +- playground/env/__tests__/env.spec.ts | 1 + playground/env/index.html | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/clientInjections.ts b/packages/vite/src/node/plugins/clientInjections.ts index 779cdc95ed1246..e3d60045406ee1 100644 --- a/packages/vite/src/node/plugins/clientInjections.ts +++ b/packages/vite/src/node/plugins/clientInjections.ts @@ -66,7 +66,7 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin { // for it to avoid shimming a `process` object during dev, // avoiding inconsistencies between dev and build return code.replace( - /\bprocess\.env\.NODE_ENV\b/g, + /(\bglobalThis\.)?\bprocess\.env\.NODE_ENV\b/g, config.define?.['process.env.NODE_ENV'] || JSON.stringify(process.env.NODE_ENV || config.mode), ) diff --git a/playground/env/__tests__/env.spec.ts b/playground/env/__tests__/env.spec.ts index 281c055e0041c0..5ef4cd0469617a 100644 --- a/playground/env/__tests__/env.spec.ts +++ b/playground/env/__tests__/env.spec.ts @@ -43,6 +43,7 @@ test('bool', async () => { test('NODE_ENV', async () => { expect(await page.textContent('.node-env')).toBe(process.env.NODE_ENV) + expect(await page.textContent('.global-node-env')).toBe(process.env.NODE_ENV) }) test('expand', async () => { diff --git a/playground/env/index.html b/playground/env/index.html index 3e146fae6184d1..03bea70f52d943 100644 --- a/playground/env/index.html +++ b/playground/env/index.html @@ -14,6 +14,7 @@

Environment Variables

import.meta.env.VITE_INLINE:

typeof import.meta.env.VITE_BOOL:

process.env.NODE_ENV:

+

globalThis.process.env.NODE_ENV:

import.meta.env.VITE_EXPAND_A:

import.meta.env.VITE_EXPAND_B:

import.meta.env.SSR:

@@ -32,6 +33,7 @@

Environment Variables

text('.bool', typeof import.meta.env.VITE_BOOL) text('.ssr', import.meta.env.SSR) text('.node-env', process.env.NODE_ENV) + text('.global-node-env', globalThis.process.env.NODE_ENV) text('.env-object', JSON.stringify(import.meta.env, null, 2)) text('.expand-a', import.meta.env.VITE_EXPAND_A) text('.expand-b', import.meta.env.VITE_EXPAND_B) From 5c6c0d381755cf7bd46854937e7dd3e2aea323f3 Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 25 Feb 2023 17:17:20 +0800 Subject: [PATCH 2/2] fix(client-inject): replace global.process.env.NODE_ENV --- packages/vite/src/node/plugins/clientInjections.ts | 2 +- playground/env/__tests__/env.spec.ts | 3 +++ playground/env/index.html | 8 ++++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/clientInjections.ts b/packages/vite/src/node/plugins/clientInjections.ts index e3d60045406ee1..bf46c0e42ce597 100644 --- a/packages/vite/src/node/plugins/clientInjections.ts +++ b/packages/vite/src/node/plugins/clientInjections.ts @@ -66,7 +66,7 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin { // for it to avoid shimming a `process` object during dev, // avoiding inconsistencies between dev and build return code.replace( - /(\bglobalThis\.)?\bprocess\.env\.NODE_ENV\b/g, + /(\bglobal(This)?\.)?\bprocess\.env\.NODE_ENV\b/g, config.define?.['process.env.NODE_ENV'] || JSON.stringify(process.env.NODE_ENV || config.mode), ) diff --git a/playground/env/__tests__/env.spec.ts b/playground/env/__tests__/env.spec.ts index 5ef4cd0469617a..8146eb128df915 100644 --- a/playground/env/__tests__/env.spec.ts +++ b/playground/env/__tests__/env.spec.ts @@ -44,6 +44,9 @@ test('bool', async () => { test('NODE_ENV', async () => { expect(await page.textContent('.node-env')).toBe(process.env.NODE_ENV) expect(await page.textContent('.global-node-env')).toBe(process.env.NODE_ENV) + expect(await page.textContent('.global-this-node-env')).toBe( + process.env.NODE_ENV, + ) }) test('expand', async () => { diff --git a/playground/env/index.html b/playground/env/index.html index 03bea70f52d943..07f1cf8f7df79b 100644 --- a/playground/env/index.html +++ b/playground/env/index.html @@ -14,7 +14,10 @@

Environment Variables

import.meta.env.VITE_INLINE:

typeof import.meta.env.VITE_BOOL:

process.env.NODE_ENV:

-

globalThis.process.env.NODE_ENV:

+

global.process.env.NODE_ENV:

+

+ globalThis.process.env.NODE_ENV: +

import.meta.env.VITE_EXPAND_A:

import.meta.env.VITE_EXPAND_B:

import.meta.env.SSR:

@@ -33,7 +36,8 @@

Environment Variables

text('.bool', typeof import.meta.env.VITE_BOOL) text('.ssr', import.meta.env.SSR) text('.node-env', process.env.NODE_ENV) - text('.global-node-env', globalThis.process.env.NODE_ENV) + text('.global-node-env', global.process.env.NODE_ENV) + text('.global-this-node-env', globalThis.process.env.NODE_ENV) text('.env-object', JSON.stringify(import.meta.env, null, 2)) text('.expand-a', import.meta.env.VITE_EXPAND_A) text('.expand-b', import.meta.env.VITE_EXPAND_B)