Skip to content

Commit 739175b

Browse files
authored
fix: avoid replacing defines and NODE_ENV in optimized deps (fix #8593) (#8606)
1 parent 7157b15 commit 739175b

File tree

8 files changed

+36
-12
lines changed

8 files changed

+36
-12
lines changed

packages/vite/src/node/optimizer/index.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ export async function runOptimizeDeps(
385385
resolvedConfig: ResolvedConfig,
386386
depsInfo: Record<string, OptimizedDepInfo>
387387
): Promise<DepOptimizationResult> {
388+
const isBuild = resolvedConfig.command === 'build'
388389
const config: ResolvedConfig = {
389390
...resolvedConfig,
390391
command: 'build'
@@ -471,30 +472,24 @@ export async function runOptimizeDeps(
471472
flatIdToExports[flatId] = exportsData
472473
}
473474

474-
const define: Record<string, string> = {
475-
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || config.mode)
476-
}
477-
for (const key in config.define) {
478-
const value = config.define[key]
479-
define[key] = typeof value === 'string' ? value : JSON.stringify(value)
480-
}
481-
482475
const start = performance.now()
483476

484477
const result = await build({
485478
absWorkingDir: process.cwd(),
486479
entryPoints: Object.keys(flatIdDeps),
487480
bundle: true,
481+
// Ensure resolution is handled by esbuildDepPlugin and
482+
// avoid replacing `process.env.NODE_ENV` for 'browser'
483+
platform: 'neutral',
488484
format: 'esm',
489485
target: config.build.target || undefined,
490486
external: config.optimizeDeps?.exclude,
491487
logLevel: 'error',
492488
splitting: true,
493489
sourcemap: true,
494490
outdir: processingCacheDir,
495-
ignoreAnnotations: resolvedConfig.command !== 'build',
491+
ignoreAnnotations: !isBuild,
496492
metafile: true,
497-
define,
498493
plugins: [
499494
...plugins,
500495
esbuildDepPlugin(flatIdDeps, flatIdToExports, config)
@@ -887,7 +882,6 @@ export function getDepHash(config: ResolvedConfig): string {
887882
{
888883
mode: process.env.NODE_ENV || config.mode,
889884
root: config.root,
890-
define: config.define,
891885
resolve: config.resolve,
892886
buildTarget: config.build.target,
893887
assetsInclude: config.assetsInclude,

playground/define/__tests__/define.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@ test('string', async () => {
4040
// html would't need to define replacement
4141
expect(await page.textContent('.exp-define')).toBe('__EXP__')
4242
expect(await page.textContent('.import-json')).toBe('__EXP__')
43+
expect(await page.textContent('.define-in-dep')).toBe(
44+
defines.__STRINGIFIED_OBJ__
45+
)
4346
})
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { defined: __STRINGIFIED_OBJ__ }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "commonjs-dep",
3+
"private": true,
4+
"version": "1.0.0",
5+
"type": "commonjs"
6+
}

playground/define/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ <h1>Define</h1>
1616
<p>no identifier substring: <span class="no-identifier-substring"></span></p>
1717
<p>define variable in html: <code class="exp-define">__EXP__</code></p>
1818
<p>import json: <code class="import-json"></code></p>
19+
<p>define in dep: <code class="define-in-dep"></code></p>
1920

2021
<script type="module">
2122
const __VAR_NAME__ = true // ensure define doesn't replace var name
@@ -50,6 +51,9 @@ <h1>Define</h1>
5051
function text(el, text) {
5152
document.querySelector(el).textContent = text
5253
}
54+
55+
import { defined } from 'dep'
56+
text('.define-in-dep', JSON.stringify(defined))
5357
</script>
5458

5559
<style>

playground/define/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
"build": "vite build",
88
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
99
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
"dep": "file:./commonjs-dep"
1013
}
1114
}

playground/define/vite.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = {
1919
'process.env.SOMEVAR': '"SOMEVAR"',
2020
$DOLLAR: 456,
2121
ÖUNICODE_LETTERɵ: 789,
22-
__VAR_NAME__: false
22+
__VAR_NAME__: false,
23+
__STRINGIFIED_OBJ__: JSON.stringify({ foo: true })
2324
}
2425
}

pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)