Skip to content

Commit

Permalink
fix(resolve): rebase sub imports relative path (#12373)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Mar 14, 2023
1 parent 42e0d6a commit fe1d61a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions playground/resolve/__tests__/resolve.spec.ts
Expand Up @@ -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]')
})
Expand Down
1 change: 1 addition & 0 deletions playground/resolve/imports-path/same-level.js
@@ -0,0 +1 @@
export * from '#top-level'
6 changes: 6 additions & 0 deletions playground/resolve/index.html
Expand Up @@ -39,6 +39,9 @@ <h2>Exports with module</h2>
<h2>Resolving top level with imports field</h2>
<p class="imports-top-level">fail</p>

<h2>Resolving same level with imports field</h2>
<p class="imports-same-level">fail</p>

<h2>Resolving nested path with imports field</h2>
<p class="imports-nested">fail</p>

Expand Down Expand Up @@ -206,6 +209,9 @@ <h2>resolve package that contains # in path</h2>
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)

Expand Down
1 change: 1 addition & 0 deletions playground/resolve/package.json
Expand Up @@ -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/",
Expand Down

0 comments on commit fe1d61a

Please sign in to comment.