From 935d9b6a0aad46265a4b53b684dc4f6ccd8dfef2 Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 11 Mar 2023 22:59:57 +0800 Subject: [PATCH 1/2] fix(resolve): rebase sub imports relative path (fix #12370) --- packages/vite/src/node/plugins/resolve.ts | 17 +++++++++++++++-- playground/resolve/__tests__/resolve.spec.ts | 6 ++++++ playground/resolve/imports-path/same-level.js | 1 + playground/resolve/index.html | 6 ++++++ playground/resolve/package.json | 1 + 5 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 playground/resolve/imports-path/same-level.js diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 11db96a2898179..6fb61592507ca8 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -1,4 +1,4 @@ -import fs from 'node:fs' +import fs, { realpath } from 'node:fs' import path from 'node:path' import colors from 'picocolors' import type { PartialResolvedId } from 'rollup' @@ -162,13 +162,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/", From 96e75de0d87237b465d28126dab2433c4cb0a4c7 Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 11 Mar 2023 23:07:59 +0800 Subject: [PATCH 2/2] fix: remove useless realpath --- packages/vite/src/node/plugins/resolve.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 6fb61592507ca8..bd325812ef2e12 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -1,4 +1,4 @@ -import fs, { realpath } from 'node:fs' +import fs from 'node:fs' import path from 'node:path' import colors from 'picocolors' import type { PartialResolvedId } from 'rollup'