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 @@
fail
+fail
+fail
@@ -206,6 +209,9 @@