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: import with query with exports/browser field #7098

Merged
merged 7 commits into from
Mar 29, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: fallback to browser field without postfix
sapphi-red committed Mar 4, 2022

Verified

This commit was signed with the committer’s verified signature.
cbeuw Andy Wang
commit fe040cdc8c675fbb6870d0ced9eb16b7f33bd704
3 changes: 2 additions & 1 deletion packages/playground/resolve/browser-field/package.json
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
"./no-ext-index": "./out/esm.browser.js",
"./not-browser.js": false,
"./multiple.dot.path.js": false,
"jsdom": false
"jsdom": false,
"./question.js?query": "./out/esm.browser.js"
}
}
4 changes: 3 additions & 1 deletion packages/playground/resolve/index.html
Original file line number Diff line number Diff line change
@@ -158,10 +158,12 @@ <h2>resolve package that contains # in path</h2>
import e from 'resolve-browser-field/ext-index/index.js'
import f from 'resolve-browser-field/ext-index'
import g from 'resolve-browser-field/no-ext-index/index.js' // no substitution
import h from 'resolve-browser-field/question.js?query'
import i from 'resolve-browser-field/no-ext?query'

import { ra, rb, rc, rd, re, rf, rg } from 'resolve-browser-field/relative'

const success = [main, a, c, d, e, f, ra, rc, rd, re, rf]
const success = [main, a, c, d, e, f, h, i, ra, rc, rd, re, rf]
const noSuccess = [b, g, rb, rg]

if (
31 changes: 28 additions & 3 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
@@ -917,12 +917,37 @@ function mapWithBrowserField(
): string | false | undefined {
const normalizedPath = path.posix.normalize(relativePathInPkgDir)

const result = _mapWithBrowserField(normalizedPath, map)
if (result !== undefined) {
return result
}

// try without postfix for `import 'something/path.js?query'` (see #7098)
const { file: normalizedPathWithoutPostfix, postfix } =
splitFileAndPostfix(normalizedPath)
if (!postfix) {
return undefined
}
const resultWithoutPostfix = _mapWithBrowserField(
normalizedPathWithoutPostfix,
map
)
if (typeof resultWithoutPostfix === 'string') {
return resultWithoutPostfix + postfix
}
return resultWithoutPostfix
}

function _mapWithBrowserField(
normalizedRelativePath: string,
map: Record<string, string | false>
): string | false | undefined {
for (const key in map) {
const normalizedKey = path.posix.normalize(key)
if (
normalizedPath === normalizedKey ||
equalWithoutSuffix(normalizedPath, normalizedKey, '.js') ||
equalWithoutSuffix(normalizedPath, normalizedKey, '/index.js')
normalizedRelativePath === normalizedKey ||
equalWithoutSuffix(normalizedRelativePath, normalizedKey, '.js') ||
equalWithoutSuffix(normalizedRelativePath, normalizedKey, '/index.js')
) {
return map[key]
}