Skip to content

Commit

Permalink
fix: correctly replace process.env.NODE_ENV (#8283)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed May 23, 2022
1 parent d671811 commit ec52baa
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/clientInjections.ts
Expand Up @@ -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)
)
}
}
Expand Down
3 changes: 3 additions & 0 deletions playground/define/__tests__/define.spec.ts
Expand Up @@ -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'])
)
Expand Down
2 changes: 2 additions & 0 deletions playground/define/index.html
Expand Up @@ -6,6 +6,7 @@ <h1>Define</h1>
<p>Boolean <code class="boolean"></code></p>
<p>Object <span class="pre object"></span></p>
<p>Env Var <code class="env-var"></code></p>
<p>process node env: <code class="process-node-env"></code></p>
<p>process as property: <code class="process-as-property"></code></p>
<p>spread object: <code class="spread-object"></code></p>
<p>spread array: <code class="spread-array"></code></p>
Expand All @@ -23,6 +24,7 @@ <h1>Define</h1>
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(
Expand Down
1 change: 1 addition & 0 deletions playground/define/vite.config.js
Expand Up @@ -15,6 +15,7 @@ module.exports = {
}
}
},
'process.env.NODE_ENV': '"dev"',
'process.env.SOMEVAR': '"SOMEVAR"',
$DOLLAR: 456,
ÖUNICODE_LETTERɵ: 789,
Expand Down
2 changes: 1 addition & 1 deletion playground/env/__tests__/env.spec.ts
Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion playground/worker/__tests__/es/es-worker.spec.ts
Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion playground/worker/__tests__/iife/worker.spec.ts
Expand Up @@ -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'),
Expand Down
Expand Up @@ -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'),
Expand Down

0 comments on commit ec52baa

Please sign in to comment.