Skip to content

Commit ec52baa

Browse files
authored
fix: correctly replace process.env.NODE_ENV (#8283)
1 parent d671811 commit ec52baa

File tree

8 files changed

+12
-5
lines changed

8 files changed

+12
-5
lines changed

packages/vite/src/node/plugins/clientInjections.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
5555
// avoiding inconsistencies between dev and build
5656
return code.replace(
5757
/\bprocess\.env\.NODE_ENV\b/g,
58-
JSON.stringify(config.mode)
58+
config.define?.['process.env.NODE_ENV'] ||
59+
JSON.stringify(process.env.NODE_ENV || config.mode)
5960
)
6061
}
6162
}

playground/define/__tests__/define.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ test('string', async () => {
1414
expect(await page.textContent('.object')).toBe(
1515
JSON.stringify(defines.__OBJ__, null, 2)
1616
)
17+
expect(await page.textContent('.process-node-env')).toBe(
18+
JSON.parse(defines['process.env.NODE_ENV'])
19+
)
1720
expect(await page.textContent('.env-var')).toBe(
1821
JSON.parse(defines['process.env.SOMEVAR'])
1922
)

playground/define/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ <h1>Define</h1>
66
<p>Boolean <code class="boolean"></code></p>
77
<p>Object <span class="pre object"></span></p>
88
<p>Env Var <code class="env-var"></code></p>
9+
<p>process node env: <code class="process-node-env"></code></p>
910
<p>process as property: <code class="process-as-property"></code></p>
1011
<p>spread object: <code class="spread-object"></code></p>
1112
<p>spread array: <code class="spread-array"></code></p>
@@ -23,6 +24,7 @@ <h1>Define</h1>
2324
text('.number', __NUMBER__)
2425
text('.boolean', __BOOLEAN__)
2526
text('.object', JSON.stringify(__OBJ__, null, 2))
27+
text('.process-node-env', process.env.NODE_ENV)
2628
text('.env-var', process.env.SOMEVAR)
2729
text('.process-as-property', __OBJ__.process.env.SOMEVAR)
2830
text(

playground/define/vite.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
}
1616
}
1717
},
18+
'process.env.NODE_ENV': '"dev"',
1819
'process.env.SOMEVAR': '"SOMEVAR"',
1920
$DOLLAR: 456,
2021
ÖUNICODE_LETTERɵ: 789,

playground/env/__tests__/env.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ test('inline variables', async () => {
3737
})
3838

3939
test('NODE_ENV', async () => {
40-
expect(await page.textContent('.node-env')).toBe(mode)
40+
expect(await page.textContent('.node-env')).toBe(process.env.NODE_ENV)
4141
})
4242

4343
test('env object', async () => {

playground/worker/__tests__/es/es-worker.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test('normal', async () => {
88
await untilUpdated(() => page.textContent('.pong'), 'pong')
99
await untilUpdated(
1010
() => page.textContent('.mode'),
11-
isBuild ? 'production' : 'development'
11+
process.env.NODE_ENV // match workerImport.js
1212
)
1313
await untilUpdated(
1414
() => page.textContent('.bundle-with-plugin'),

playground/worker/__tests__/iife/worker.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test('normal', async () => {
99
await untilUpdated(() => page.textContent('.pong'), 'pong')
1010
await untilUpdated(
1111
() => page.textContent('.mode'),
12-
isBuild ? 'production' : 'development'
12+
process.env.NODE_ENV // match workerImport.js
1313
)
1414
await untilUpdated(
1515
() => page.textContent('.bundle-with-plugin'),

playground/worker/__tests__/relative-base/relative-base-worker.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test('normal', async () => {
88
await untilUpdated(() => page.textContent('.pong'), 'pong')
99
await untilUpdated(
1010
() => page.textContent('.mode'),
11-
isBuild ? 'production' : 'development'
11+
process.env.NODE_ENV // match workerImport.js
1212
)
1313
await untilUpdated(
1414
() => page.textContent('.bundle-with-plugin'),

0 commit comments

Comments
 (0)