diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 7be77c0d6172f1..4cdd2043cc41b7 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -161,13 +161,26 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { if (!pkgJsonPath) return const pkgData = loadPackageData(pkgJsonPath, options.preserveSymlinks) - return resolveExportsOrImports( + let importsPath = resolveExportsOrImports( pkgData.data, id, options, targetWeb, 'imports', ) + + if (importsPath?.startsWith('.')) { + importsPath = path.relative( + basedir, + path.join(path.dirname(pkgJsonPath), importsPath), + ) + + if (!importsPath.startsWith('.')) { + importsPath = `./${importsPath}` + } + } + + return importsPath } const resolvedImports = resolveSubpathImports(id, importer) diff --git a/playground/resolve/__tests__/resolve.spec.ts b/playground/resolve/__tests__/resolve.spec.ts index f30fc3e9338140..483c3767833935 100644 --- a/playground/resolve/__tests__/resolve.spec.ts +++ b/playground/resolve/__tests__/resolve.spec.ts @@ -163,6 +163,12 @@ test('Resolving top level with imports field', async () => { expect(await page.textContent('.imports-top-level')).toMatch('[success]') }) +test('Resolving same level with imports field', async () => { + expect(await page.textContent('.imports-same-level')).toMatch( + await page.textContent('.imports-top-level'), + ) +}) + test('Resolving nested path with imports field', async () => { expect(await page.textContent('.imports-nested')).toMatch('[success]') }) diff --git a/playground/resolve/imports-path/same-level.js b/playground/resolve/imports-path/same-level.js new file mode 100644 index 00000000000000..2f8b67543787f9 --- /dev/null +++ b/playground/resolve/imports-path/same-level.js @@ -0,0 +1 @@ +export * from '#top-level' diff --git a/playground/resolve/index.html b/playground/resolve/index.html index 0966f050bbd580..5de5ef46289c69 100644 --- a/playground/resolve/index.html +++ b/playground/resolve/index.html @@ -39,6 +39,9 @@

Exports with module

Resolving top level with imports field

fail

+

Resolving same level with imports field

+

fail

+

Resolving nested path with imports field

fail

@@ -206,6 +209,9 @@

resolve package that contains # in path

import { msg as importsTopLevel } from '#top-level' text('.imports-top-level', importsTopLevel) + import { msg as importsSameLevel } from '#same-level' + text('.imports-same-level', importsSameLevel) + import { msg as importsNested } from '#nested/path.js' text('.imports-nested', importsNested) diff --git a/playground/resolve/package.json b/playground/resolve/package.json index 6c0fd6f16afc86..345198eeb9b8f0 100644 --- a/playground/resolve/package.json +++ b/playground/resolve/package.json @@ -10,6 +10,7 @@ }, "imports": { "#top-level": "./imports-path/top-level.js", + "#same-level": "./imports-path/same-level.js", "#nested/path.js": "./imports-path/nested-path.js", "#star/*": "./imports-path/star/*", "#slash/": "./imports-path/slash/",