diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index eee6ba0f92d742..a9b68b1fe5ba30 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -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, options, targetWeb, 'imports', @@ -496,7 +499,7 @@ function resolveSubpathImports( } } - return importsPath + return importsPath + postfix } function ensureVersionQuery( diff --git a/playground/resolve/__tests__/resolve.spec.ts b/playground/resolve/__tests__/resolve.spec.ts index d930ac1e49a2df..24715df90a1e29 100644 --- a/playground/resolve/__tests__/resolve.spec.ts +++ b/playground/resolve/__tests__/resolve.spec.ts @@ -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) diff --git a/playground/resolve/imports-path/query.json b/playground/resolve/imports-path/query.json new file mode 100644 index 00000000000000..97e19265d6c843 --- /dev/null +++ b/playground/resolve/imports-path/query.json @@ -0,0 +1,3 @@ +{ + "foo": "json" +} diff --git a/playground/resolve/index.html b/playground/resolve/index.html index fa6b5afbb84cd6..5badd9bf57bb6e 100644 --- a/playground/resolve/index.html +++ b/playground/resolve/index.html @@ -61,6 +61,9 @@

Resolving slash with imports filed

Resolving from other package with imports field

fail

+

Resolving with query with imports field

+

fail

+

Resolve /index.*

fail

@@ -254,6 +257,9 @@

resolve non normalized absolute path

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()) diff --git a/playground/resolve/package.json b/playground/resolve/package.json index 04b932cf63b990..fe0d3295d1e842 100644 --- a/playground/resolve/package.json +++ b/playground/resolve/package.json @@ -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",