Skip to content

Commit ab823ab

Browse files
authored
fix: import with query with imports field (#16085)
1 parent 0c0aeae commit ab823ab

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

packages/vite/src/node/plugins/resolve.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,12 @@ function resolveSubpathImports(
480480
const pkgData = findNearestPackageData(basedir, options.packageCache)
481481
if (!pkgData) return
482482

483+
let { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id.slice(1))
484+
idWithoutPostfix = '#' + idWithoutPostfix
485+
483486
let importsPath = resolveExportsOrImports(
484487
pkgData.data,
485-
id,
488+
idWithoutPostfix,
486489
options,
487490
targetWeb,
488491
'imports',
@@ -496,7 +499,7 @@ function resolveSubpathImports(
496499
}
497500
}
498501

499-
return importsPath
502+
return importsPath + postfix
500503
}
501504

502505
function ensureVersionQuery(

playground/resolve/__tests__/resolve.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ test('Resolving from other package with imports field', async () => {
209209
expect(await page.textContent('.imports-pkg-slash')).toMatch('[success]')
210210
})
211211

212+
test('Resolving with query with imports field', async () => {
213+
// since it is imported with `?url` it should return a URL
214+
expect(await page.textContent('.imports-query')).toMatch(
215+
isBuild ? /base64/ : '/imports-path/query.json',
216+
)
217+
})
218+
212219
test('Resolve doesnt interrupt page request with trailing query and .css', async () => {
213220
await page.goto(viteTestUrl + '/?test.css')
214221
expect(await page.locator('vite-error-overlay').count()).toBe(0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "json"
3+
}

playground/resolve/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ <h2>Resolving slash with imports filed</h2>
6161
<h2>Resolving from other package with imports field</h2>
6262
<p class="imports-pkg-slash">fail</p>
6363

64+
<h2>Resolving with query with imports field</h2>
65+
<p class="imports-query">fail</p>
66+
6467
<h2>Resolve /index.*</h2>
6568
<p class="index">fail</p>
6669

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

260+
import importsQuery from '#query?url'
261+
text('.imports-query', importsQuery)
262+
257263
// implicit index resolving
258264
import { foo } from './util'
259265
text('.index', foo())

playground/resolve/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"#nested/path.js": "./imports-path/nested-path.js",
1616
"#star/*": "./imports-path/star/*",
1717
"#slash/": "./imports-path/slash/",
18-
"#other-pkg-slash/": "@vitejs/test-resolve-imports-pkg/nest/"
18+
"#other-pkg-slash/": "@vitejs/test-resolve-imports-pkg/nest/",
19+
"#query": "./imports-path/query.json"
1920
},
2021
"dependencies": {
2122
"@babel/runtime": "^7.23.9",

0 commit comments

Comments
 (0)