Skip to content

Commit

Permalink
fix(client-inject): replace globalThis.process.env.NODE_ENV (fix vite…
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Feb 25, 2023
1 parent 3d3788d commit 8226fac
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 1 deletion.
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,
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

0 comments on commit 8226fac

Please sign in to comment.