Skip to content

Commit fe1d61a

Browse files
authoredMar 14, 2023
fix(resolve): rebase sub imports relative path (#12373)
1 parent 42e0d6a commit fe1d61a

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed
 

‎packages/vite/src/node/plugins/resolve.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,26 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
161161
if (!pkgJsonPath) return
162162

163163
const pkgData = loadPackageData(pkgJsonPath, options.preserveSymlinks)
164-
return resolveExportsOrImports(
164+
let importsPath = resolveExportsOrImports(
165165
pkgData.data,
166166
id,
167167
options,
168168
targetWeb,
169169
'imports',
170170
)
171+
172+
if (importsPath?.startsWith('.')) {
173+
importsPath = path.relative(
174+
basedir,
175+
path.join(path.dirname(pkgJsonPath), importsPath),
176+
)
177+
178+
if (!importsPath.startsWith('.')) {
179+
importsPath = `./${importsPath}`
180+
}
181+
}
182+
183+
return importsPath
171184
}
172185

173186
const resolvedImports = resolveSubpathImports(id, importer)

‎playground/resolve/__tests__/resolve.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ test('Resolving top level with imports field', async () => {
163163
expect(await page.textContent('.imports-top-level')).toMatch('[success]')
164164
})
165165

166+
test('Resolving same level with imports field', async () => {
167+
expect(await page.textContent('.imports-same-level')).toMatch(
168+
await page.textContent('.imports-top-level'),
169+
)
170+
})
171+
166172
test('Resolving nested path with imports field', async () => {
167173
expect(await page.textContent('.imports-nested')).toMatch('[success]')
168174
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '#top-level'

‎playground/resolve/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ <h2>Exports with module</h2>
3939
<h2>Resolving top level with imports field</h2>
4040
<p class="imports-top-level">fail</p>
4141

42+
<h2>Resolving same level with imports field</h2>
43+
<p class="imports-same-level">fail</p>
44+
4245
<h2>Resolving nested path with imports field</h2>
4346
<p class="imports-nested">fail</p>
4447

@@ -206,6 +209,9 @@ <h2>resolve package that contains # in path</h2>
206209
import { msg as importsTopLevel } from '#top-level'
207210
text('.imports-top-level', importsTopLevel)
208211

212+
import { msg as importsSameLevel } from '#same-level'
213+
text('.imports-same-level', importsSameLevel)
214+
209215
import { msg as importsNested } from '#nested/path.js'
210216
text('.imports-nested', importsNested)
211217

‎playground/resolve/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"imports": {
1212
"#top-level": "./imports-path/top-level.js",
13+
"#same-level": "./imports-path/same-level.js",
1314
"#nested/path.js": "./imports-path/nested-path.js",
1415
"#star/*": "./imports-path/star/*",
1516
"#slash/": "./imports-path/slash/",

0 commit comments

Comments
 (0)
Please sign in to comment.