Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use browser field if it is not likely UMD or CJS (fixes #9445) #9459

Merged
merged 2 commits into from Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -868,6 +868,8 @@ export function resolvePackageEntry(
) {
// likely UMD or CJS(!!! e.g. firebase 7.x), prefer module
entryPoint = data.module
} else {
entryPoint = browserEntry
}
}
} else {
Expand Down
8 changes: 8 additions & 0 deletions playground/resolve/__tests__/resolve.spec.ts
Expand Up @@ -87,6 +87,14 @@ test('browser field', async () => {
expect(await page.textContent('.browser')).toMatch('[success]')
})

test('Resolve browser field even if module field exists', async () => {
expect(await page.textContent('.browser-module1')).toMatch('[success]')
})

test('Resolve module field if browser field is likely UMD or CJS', async () => {
expect(await page.textContent('.browser-module2')).toMatch('[success]')
})

test('css entry', async () => {
expect(await page.textContent('.css')).toMatch('[success]')
})
Expand Down
1 change: 1 addition & 0 deletions playground/resolve/browser-module-field1/index.js
@@ -0,0 +1 @@
export default '[fail] this should not run in the browser'
1 change: 1 addition & 0 deletions playground/resolve/browser-module-field1/index.web.js
@@ -0,0 +1 @@
export default '[success] this should run in browser'
8 changes: 8 additions & 0 deletions playground/resolve/browser-module-field1/package.json
@@ -0,0 +1,8 @@
{
"name": "resolve-browser-module-field1",
"private": true,
"version": "1.0.0",
"//": "real world example: https://github.com/aws/aws-sdk-js-v3/blob/59cdfd81452bce16bb26d07668e5550ed05d9d06/packages/credential-providers/package.json#L6-L7",
"module": "index.js",
"browser": "index.web.js"
}
1 change: 1 addition & 0 deletions playground/resolve/browser-module-field2/index.js
@@ -0,0 +1 @@
export default '[success] this should run in browser'
1 change: 1 addition & 0 deletions playground/resolve/browser-module-field2/index.web.js
@@ -0,0 +1 @@
module.exports = '[fail] this should not run in the browser'
7 changes: 7 additions & 0 deletions playground/resolve/browser-module-field2/package.json
@@ -0,0 +1,7 @@
{
"name": "resolve-browser-module-field2",
"private": true,
"version": "1.0.0",
"module": "index.js",
"browser": "index.web.js"
}
12 changes: 12 additions & 0 deletions playground/resolve/index.html
Expand Up @@ -76,6 +76,12 @@ <h2>Resolve absolute path</h2>
<h2>Browser Field</h2>
<p class="browser">fail</p>

<h2>Resolve browser field even if module field exists</h2>
<p class="browser-module1">fail</p>

<h2>Resolve module field if browser field is likely UMD or CJS</h2>
<p class="browser-module2">fail</p>

<h2>Don't resolve to the `module` field if the importer is a `require` call</h2>
<p class="require-pkg-with-module-field">fail</p>

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

import browserModule1 from 'resolve-browser-module-field1'
text('.browser-module1', browserModule1)

import browserModule2 from 'resolve-browser-module-field2'
text('.browser-module2', browserModule2)

import { msg as requireButWithModuleFieldMsg } from 'require-pkg-with-module-field'
text('.require-pkg-with-module-field', requireButWithModuleFieldMsg)

Expand Down
2 changes: 2 additions & 0 deletions playground/resolve/package.json
Expand Up @@ -14,6 +14,8 @@
"normalize.css": "^8.0.1",
"require-pkg-with-module-field": "link:./require-pkg-with-module-field",
"resolve-browser-field": "link:./browser-field",
"resolve-browser-module-field1": "link:./browser-module-field1",
"resolve-browser-module-field2": "link:./browser-module-field2",
"resolve-custom-condition": "link:./custom-condition",
"resolve-custom-main-field": "link:./custom-main-field",
"resolve-exports-env": "link:./exports-env",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.