Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolve): rebase sub imports relative path #12373

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions playground/resolve/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '#top-level'
6 changes: 6 additions & 0 deletions playground/resolve/index.html
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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