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
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
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'
)
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
})

test('deep import with exports field + exposed dir', async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/playground/resolve/index.html
Expand Up @@ -151,10 +151,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 @@ -289,23 +289,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 @@ -783,6 +788,7 @@ function resolveExports(
if (options.conditions) {
conditions.push(...options.conditions)
}

return _resolveExports(pkg, key, {
browser: targetWeb,
require: options.isRequire,
Expand Down Expand Up @@ -813,12 +819,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 @@ -830,9 +838,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