Skip to content

Commit c868e64

Browse files
authored
fix: use browser field if it is not likely UMD or CJS (fixes #9445) (#9459)
1 parent 8a5b575 commit c868e64

File tree

11 files changed

+53
-0
lines changed

11 files changed

+53
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,8 @@ export function resolvePackageEntry(
867867
) {
868868
// likely UMD or CJS(!!! e.g. firebase 7.x), prefer module
869869
entryPoint = data.module
870+
} else {
871+
entryPoint = browserEntry
870872
}
871873
}
872874
} else {

playground/resolve/__tests__/resolve.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ test('browser field', async () => {
8787
expect(await page.textContent('.browser')).toMatch('[success]')
8888
})
8989

90+
test('Resolve browser field even if module field exists', async () => {
91+
expect(await page.textContent('.browser-module1')).toMatch('[success]')
92+
})
93+
94+
test('Resolve module field if browser field is likely UMD or CJS', async () => {
95+
expect(await page.textContent('.browser-module2')).toMatch('[success]')
96+
})
97+
9098
test('css entry', async () => {
9199
expect(await page.textContent('.css')).toMatch('[success]')
92100
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default '[fail] this should not run in the browser'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default '[success] this should run in browser'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "resolve-browser-module-field1",
3+
"private": true,
4+
"version": "1.0.0",
5+
"//": "real world example: https://github.com/aws/aws-sdk-js-v3/blob/59cdfd81452bce16bb26d07668e5550ed05d9d06/packages/credential-providers/package.json#L6-L7",
6+
"module": "index.js",
7+
"browser": "index.web.js"
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default '[success] this should run in browser'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = '[fail] this should not run in the browser'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "resolve-browser-module-field2",
3+
"private": true,
4+
"version": "1.0.0",
5+
"module": "index.js",
6+
"browser": "index.web.js"
7+
}

playground/resolve/index.html

+12
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ <h2>Resolve absolute path</h2>
7676
<h2>Browser Field</h2>
7777
<p class="browser">fail</p>
7878

79+
<h2>Resolve browser field even if module field exists</h2>
80+
<p class="browser-module1">fail</p>
81+
82+
<h2>Resolve module field if browser field is likely UMD or CJS</h2>
83+
<p class="browser-module2">fail</p>
84+
7985
<h2>Don't resolve to the `module` field if the importer is a `require` call</h2>
8086
<p class="require-pkg-with-module-field">fail</p>
8187

@@ -213,6 +219,12 @@ <h2>resolve package that contains # in path</h2>
213219
text('.browser', main)
214220
}
215221

222+
import browserModule1 from 'resolve-browser-module-field1'
223+
text('.browser-module1', browserModule1)
224+
225+
import browserModule2 from 'resolve-browser-module-field2'
226+
text('.browser-module2', browserModule2)
227+
216228
import { msg as requireButWithModuleFieldMsg } from 'require-pkg-with-module-field'
217229
text('.require-pkg-with-module-field', requireButWithModuleFieldMsg)
218230

playground/resolve/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"normalize.css": "^8.0.1",
1515
"require-pkg-with-module-field": "link:./require-pkg-with-module-field",
1616
"resolve-browser-field": "link:./browser-field",
17+
"resolve-browser-module-field1": "link:./browser-module-field1",
18+
"resolve-browser-module-field2": "link:./browser-module-field2",
1719
"resolve-custom-condition": "link:./custom-condition",
1820
"resolve-custom-main-field": "link:./custom-main-field",
1921
"resolve-exports-env": "link:./exports-env",

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)