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 4 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
11 changes: 10 additions & 1 deletion packages/playground/resolve/__tests__/resolve.spec.ts
Expand Up @@ -17,7 +17,16 @@ 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 including "?"', async () => {
expect(await page.textContent('.exports-deep-question-mark')).toMatch(
'[success]'
)
})

test('deep import with exports field + exposed dir', async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/playground/resolve/browser-field/package.json
Expand Up @@ -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"
}
}
1 change: 1 addition & 0 deletions packages/playground/resolve/exports-path/deep2.js
@@ -0,0 +1 @@
export const msg = '[success] deep resolve from exports including postfix'
1 change: 1 addition & 0 deletions packages/playground/resolve/exports-path/package.json
Expand Up @@ -9,6 +9,7 @@
},
"./deep.js": "./deep.js",
"./deep.json": "./deep.json",
"./deep.js?url": "./deep2.js",
"./dir/": "./dir/",
"./dir-mapped/*": {
"import": "./dir/*",
Expand Down
11 changes: 10 additions & 1 deletion packages/playground/resolve/index.html
Expand Up @@ -15,6 +15,9 @@ <h2>Deep import with exports field</h2>
<h2>Deep import with query with exports field</h2>
<p class="exports-deep-query">fail</p>

<h2>Deep import with exports field including "?"</h2>
<p class="exports-deep-question-mark">fail</p>

<h2>Deep import with exports field + exposed directory</h2>
<p class="exports-deep-exposed-dir">fail</p>

Expand Down Expand Up @@ -107,6 +110,10 @@ <h2>resolve package that contains # in path</h2>
import deepPath from 'resolve-exports-path/deep.json?url'
text('.exports-deep-query', deepPath)

// deep import w/ exports including "?"
import { msg as deepMsg2 } from 'resolve-exports-path/deep.js?url'
text('.exports-deep-question-mark', deepMsg2)

// deep import w/ exposed dir
import { msg as exposedDirMsg } from 'resolve-exports-path/dir/dir'
text('.exports-deep-exposed-dir', exposedDirMsg)
Expand Down Expand Up @@ -151,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 (
Expand Down
96 changes: 70 additions & 26 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 @@ -772,7 +777,7 @@ function packageEntryFailure(id: string, details?: string) {

function resolveExports(
pkg: PackageData['data'],
key: string,
keyWithQuery: string,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's nice if we can leave this back as key

options: InternalResolveOptions,
targetWeb: boolean
) {
Expand All @@ -783,11 +788,30 @@ function resolveExports(
if (options.conditions) {
conditions.push(...options.conditions)
}
return _resolveExports(pkg, key, {
browser: targetWeb,
require: options.isRequire,
conditions
})

try {
const resolved = _resolveExports(pkg, keyWithQuery, {
browser: targetWeb,
require: options.isRequire,
conditions
})
return resolved
} catch (err) {
// not found

// try without postfix for `import 'something/path.js?query'` (see #7098)
const { file, postfix } = splitFileAndPostfix(keyWithQuery)
if (!postfix) {
throw err
}

const resolvedWithoutPostfix = _resolveExports(pkg, file, {
browser: targetWeb,
require: options.isRequire,
conditions
})
return resolvedWithoutPostfix + postfix
}
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}

function resolveDeepImport(
Expand All @@ -813,12 +837,7 @@ function resolveDeepImport(
// map relative based on exports data
if (exportsField) {
if (isObject(exportsField) && !Array.isArray(exportsField)) {
relativeId = resolveExports(
data,
cleanUrl(relativeId),
options,
targetWeb
)
relativeId = resolveExports(data, relativeId, options, targetWeb)
} else {
// not exposed
relativeId = undefined
Expand Down Expand Up @@ -898,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]
}
Expand Down