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 imports field #16085

Merged
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
7 changes: 5 additions & 2 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -480,9 +480,12 @@ function resolveSubpathImports(
const pkgData = findNearestPackageData(basedir, options.packageCache)
if (!pkgData) return

let { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id.slice(1))
idWithoutPostfix = '#' + idWithoutPostfix

let importsPath = resolveExportsOrImports(
pkgData.data,
id,
idWithoutPostfix,
Comment on lines +483 to +488
Copy link
Member Author

Choose a reason for hiding this comment

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

I guess imports field can contain ? in the key, but given that we don't support it with exports field (#7098 (comment)), I didn't implement that.

options,
targetWeb,
'imports',
Expand All @@ -496,7 +499,7 @@ function resolveSubpathImports(
}
}

return importsPath
return importsPath + postfix
}

function ensureVersionQuery(
Expand Down
7 changes: 7 additions & 0 deletions playground/resolve/__tests__/resolve.spec.ts
Expand Up @@ -209,6 +209,13 @@ test('Resolving from other package with imports field', async () => {
expect(await page.textContent('.imports-pkg-slash')).toMatch('[success]')
})

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

test('Resolve doesnt interrupt page request with trailing query and .css', async () => {
await page.goto(viteTestUrl + '/?test.css')
expect(await page.locator('vite-error-overlay').count()).toBe(0)
Expand Down
3 changes: 3 additions & 0 deletions playground/resolve/imports-path/query.json
@@ -0,0 +1,3 @@
{
"foo": "json"
}
6 changes: 6 additions & 0 deletions playground/resolve/index.html
Expand Up @@ -61,6 +61,9 @@ <h2>Resolving slash with imports filed</h2>
<h2>Resolving from other package with imports field</h2>
<p class="imports-pkg-slash">fail</p>

<h2>Resolving with query with imports field</h2>
<p class="imports-query">fail</p>

<h2>Resolve /index.*</h2>
<p class="index">fail</p>

Expand Down Expand Up @@ -254,6 +257,9 @@ <h2>resolve non normalized absolute path</h2>
import { msg as importsPkgSlash } from '#other-pkg-slash/index.js'
text('.imports-pkg-slash', importsPkgSlash)

import importsQuery from '#query?url'
text('.imports-query', importsQuery)

// implicit index resolving
import { foo } from './util'
text('.index', foo())
Expand Down
3 changes: 2 additions & 1 deletion playground/resolve/package.json
Expand Up @@ -15,7 +15,8 @@
"#nested/path.js": "./imports-path/nested-path.js",
"#star/*": "./imports-path/star/*",
"#slash/": "./imports-path/slash/",
"#other-pkg-slash/": "@vitejs/test-resolve-imports-pkg/nest/"
"#other-pkg-slash/": "@vitejs/test-resolve-imports-pkg/nest/",
"#query": "./imports-path/query.json"
},
"dependencies": {
"@babel/runtime": "^7.23.9",
Expand Down