Skip to content

Commit

Permalink
fix: import with query with exports/browser field (#7098)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Mar 29, 2022
1 parent 884e994 commit 9ce6732
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
5 changes: 4 additions & 1 deletion packages/playground/resolve/__tests__/resolve.spec.ts
Expand Up @@ -17,7 +17,10 @@ test('deep import with exports field', async () => {
})

test('deep import with query with exports field', async () => {
expect(await page.textContent('.exports-deep-query')).not.toMatch('fail')
// since it is imported with `?url` it should return a url
expect(await page.textContent('.exports-deep-query')).toMatch(
isBuild ? /base64/ : '/exports-path/deep.json'
)
})

test('deep import with exports field + exposed dir', async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/playground/resolve/index.html
Expand Up @@ -171,10 +171,11 @@ <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/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, ra, rc, rd, re, rf]
const noSuccess = [b, g, rb, rg]

if (
Expand Down
48 changes: 29 additions & 19 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -332,23 +332,28 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
}
}

function tryFsResolve(
fsPath: string,
options: InternalResolveOptions,
tryIndex = true,
targetWeb = true
): string | undefined {
let file = fsPath
function splitFileAndPostfix(path: string) {
let file = path
let postfix = ''

let postfixIndex = fsPath.indexOf('?')
let postfixIndex = path.indexOf('?')
if (postfixIndex < 0) {
postfixIndex = fsPath.indexOf('#')
postfixIndex = path.indexOf('#')
}
if (postfixIndex > 0) {
file = fsPath.slice(0, postfixIndex)
postfix = fsPath.slice(postfixIndex)
file = path.slice(0, postfixIndex)
postfix = path.slice(postfixIndex)
}
return { file, postfix }
}

function tryFsResolve(
fsPath: string,
options: InternalResolveOptions,
tryIndex = true,
targetWeb = true
): string | undefined {
const { file, postfix } = splitFileAndPostfix(fsPath)

let res: string | undefined

Expand Down Expand Up @@ -842,6 +847,7 @@ function resolveExports(
if (options.conditions) {
conditions.push(...options.conditions)
}

return _resolveExports(pkg, key, {
browser: targetWeb,
require: options.isRequire,
Expand Down Expand Up @@ -872,12 +878,14 @@ function resolveDeepImport(
// map relative based on exports data
if (exportsField) {
if (isObject(exportsField) && !Array.isArray(exportsField)) {
relativeId = resolveExports(
data,
cleanUrl(relativeId),
options,
targetWeb
)
// resolve without postfix (see #7098)
const { file, postfix } = splitFileAndPostfix(relativeId)
const exportsId = resolveExports(data, file, options, targetWeb)
if (exportsId !== undefined) {
relativeId = exportsId + postfix
} else {
relativeId = undefined
}
} else {
// not exposed
relativeId = undefined
Expand All @@ -889,9 +897,11 @@ function resolveDeepImport(
)
}
} else if (targetWeb && isObject(browserField)) {
const mapped = mapWithBrowserField(relativeId, browserField)
// resolve without postfix (see #7098)
const { file, postfix } = splitFileAndPostfix(relativeId)
const mapped = mapWithBrowserField(file, browserField)
if (mapped) {
relativeId = mapped
relativeId = mapped + postfix
} else if (mapped === false) {
return (webResolvedImports[id] = browserExternalId)
}
Expand Down

0 comments on commit 9ce6732

Please sign in to comment.