Skip to content

Commit a201cd4

Browse files
patak-devsapphi-red
andauthoredJun 22, 2022
fix: use esbuild platform browser/node instead of neutral (#8714)
Co-authored-by: sapphi-red <green@sapphi.red>
1 parent 4f9097b commit a201cd4

File tree

9 files changed

+57
-4
lines changed

9 files changed

+57
-4
lines changed
 

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,30 @@ export async function runOptimizeDeps(
533533
flatIdToExports[flatId] = exportsData
534534
}
535535

536+
// esbuild automatically replaces process.env.NODE_ENV for platform 'browser'
537+
// In lib mode, we need to keep process.env.NODE_ENV untouched, so to at build
538+
// time we replace it by __vite_process_env_NODE_ENV. This placeholder will be
539+
// later replaced by the define plugin
540+
const define = {
541+
'process.env.NODE_ENV': isBuild
542+
? '__vite_process_env_NODE_ENV'
543+
: JSON.stringify(process.env.NODE_ENV || config.mode)
544+
}
545+
536546
const start = performance.now()
537547

538548
const result = await build({
539549
absWorkingDir: process.cwd(),
540550
entryPoints: Object.keys(flatIdDeps),
541551
bundle: true,
542-
// Ensure resolution is handled by esbuildDepPlugin and
543-
// avoid replacing `process.env.NODE_ENV` for 'browser'
544-
platform: 'neutral',
552+
// We can't use platform 'neutral', as esbuild has custom handling
553+
// when the platform is 'node' or 'browser' that can't be emulated
554+
// by using mainFields and conditions
555+
platform:
556+
config.build.ssr && config.ssr?.target !== 'webworker'
557+
? 'node'
558+
: 'browser',
559+
define,
545560
format: 'esm',
546561
target: config.build.target || undefined,
547562
external: config.optimizeDeps?.exclude,

‎packages/vite/src/node/plugins/define.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export function definePlugin(config: ResolvedConfig): Plugin {
2525
Object.assign(processNodeEnv, {
2626
'process.env.NODE_ENV': JSON.stringify(nodeEnv),
2727
'global.process.env.NODE_ENV': JSON.stringify(nodeEnv),
28-
'globalThis.process.env.NODE_ENV': JSON.stringify(nodeEnv)
28+
'globalThis.process.env.NODE_ENV': JSON.stringify(nodeEnv),
29+
__vite_process_env_NODE_ENV: JSON.stringify(nodeEnv)
2930
})
3031
}
3132

@@ -65,6 +66,10 @@ export function definePlugin(config: ResolvedConfig): Plugin {
6566
...(replaceProcessEnv ? processEnv : {})
6667
}
6768

69+
if (isBuild && !replaceProcessEnv) {
70+
replacements['__vite_process_env_NODE_ENV'] = 'process.env.NODE_ENV'
71+
}
72+
6873
const replacementsKeys = Object.keys(replacements)
6974
const pattern = replacementsKeys.length
7075
? new RegExp(

‎playground/optimize-deps/__tests__/optimize-deps.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ test('import from dep with .notjs files', async () => {
7777
expect(await page.textContent('.not-js')).toMatch(`[success]`)
7878
})
7979

80+
test('Import from dependency which uses relative path which needs to be resolved by main field', async () => {
81+
expect(await page.textContent('.relative-to-main')).toMatch(`[success]`)
82+
})
83+
8084
test('dep with dynamic import', async () => {
8185
expect(await page.textContent('.dep-with-dynamic-import')).toMatch(
8286
`[success]`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = '[success] imported from main'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "dep-relative-to-main",
3+
"private": true,
4+
"version": "1.0.0",
5+
"main": "lib/main.js"
6+
}

‎playground/optimize-deps/index.html

+9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ <h2>Import from dependency with process.env.NODE_ENV</h2>
5050
<h2>Import from dependency with .notjs files</h2>
5151
<div class="not-js"></div>
5252

53+
<h2>
54+
Import from dependency which uses relative path which needs to be resolved by
55+
main field
56+
</h2>
57+
<div class="relative-to-main"></div>
58+
5359
<h2>Import from dependency with dynamic import</h2>
5460
<div class="dep-with-dynamic-import"></div>
5561

@@ -109,6 +115,9 @@ <h2>Flatten Id</h2>
109115
import { notjsValue } from 'dep-not-js'
110116
text('.not-js', notjsValue)
111117

118+
import foo from 'dep-relative-to-main/entry'
119+
text('.relative-to-main', foo)
120+
112121
import { lazyFoo } from 'dep-with-dynamic-import'
113122
lazyFoo().then((foo) => {
114123
text('.dep-with-dynamic-import', foo)

‎playground/optimize-deps/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"dep-linked-include": "link:./dep-linked-include",
2020
"dep-node-env": "file:./dep-node-env",
2121
"dep-not-js": "file:./dep-not-js",
22+
"dep-relative-to-main": "file:./dep-relative-to-main",
2223
"dep-with-builtin-module-cjs": "file:./dep-with-builtin-module-cjs",
2324
"dep-with-builtin-module-esm": "file:./dep-with-builtin-module-esm",
2425
"dep-with-dynamic-import": "file:./dep-with-dynamic-import",

‎pnpm-lock.yaml

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

0 commit comments

Comments
 (0)
Please sign in to comment.