Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client-inject): replace globalThis.process.env.NODE_ENV (fix #12185) #12194

Merged
merged 2 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/clientInjections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
sun0day marked this conversation as resolved.
Show resolved Hide resolved
config.define?.['process.env.NODE_ENV'] ||
JSON.stringify(process.env.NODE_ENV || config.mode),
)
Expand Down
1 change: 1 addition & 0 deletions playground/env/__tests__/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
2 changes: 2 additions & 0 deletions playground/env/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ <h1>Environment Variables</h1>
<p>import.meta.env.VITE_INLINE: <code class="inline"></code></p>
<p>typeof import.meta.env.VITE_BOOL: <code class="bool"></code></p>
<p>process.env.NODE_ENV: <code class="node-env"></code></p>
<p>globalThis.process.env.NODE_ENV: <code class="global-node-env"></code></p>
<p>import.meta.env.VITE_EXPAND_A: <code class="expand-a"></code></p>
<p>import.meta.env.VITE_EXPAND_B: <code class="expand-b"></code></p>
<p>import.meta.env.SSR: <code class="ssr"></code></p>
Expand All @@ -32,6 +33,7 @@ <h1>Environment Variables</h1>
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)
Expand Down