Skip to content

Commit 3259006

Browse files
benmccannbluwy
andauthoredOct 13, 2022
fix: prefer exports when resolving (#10371)
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
1 parent ecba3f8 commit 3259006

File tree

11 files changed

+71
-6
lines changed

11 files changed

+71
-6
lines changed
 

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

+29-5
Original file line numberDiff line numberDiff line change
@@ -648,13 +648,36 @@ export function tryNodeResolve(
648648
basedir = nestedResolveFrom(nestedRoot, basedir, preserveSymlinks)
649649
}
650650

651+
// nearest package.json
652+
let nearestPkg: PackageData | undefined
653+
// nearest package.json that may have the `exports` field
651654
let pkg: PackageData | undefined
652-
const pkgId = possiblePkgIds.reverse().find((pkgId) => {
653-
pkg = resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache)!
654-
return pkg
655+
656+
let pkgId = possiblePkgIds.reverse().find((pkgId) => {
657+
nearestPkg = resolvePackageData(
658+
pkgId,
659+
basedir,
660+
preserveSymlinks,
661+
packageCache
662+
)!
663+
return nearestPkg
655664
})!
656665

657-
if (!pkg) {
666+
const rootPkgId = possiblePkgIds[0]
667+
const rootPkg = resolvePackageData(
668+
rootPkgId,
669+
basedir,
670+
preserveSymlinks,
671+
packageCache
672+
)!
673+
if (rootPkg?.data?.exports) {
674+
pkg = rootPkg
675+
pkgId = rootPkgId
676+
} else {
677+
pkg = nearestPkg
678+
}
679+
680+
if (!pkg || !nearestPkg) {
658681
// if import can't be found, check if it's an optional peer dep.
659682
// if so, we can resolve to a special id that errors only when imported.
660683
if (
@@ -753,7 +776,8 @@ export function tryNodeResolve(
753776
}
754777

755778
const ext = path.extname(resolved)
756-
const isCJS = ext === '.cjs' || (ext === '.js' && pkg.data.type !== 'module')
779+
const isCJS =
780+
ext === '.cjs' || (ext === '.js' && nearestPkg.data.type !== 'module')
757781

758782
if (
759783
!options.ssrOptimizeCheck &&

‎packages/vite/src/node/ssr/ssrModuleLoader.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ async function instantiateModule(
126126
preserveSymlinks,
127127
isBuild: true,
128128
isProduction,
129-
isRequire: true,
129+
isRequire: false,
130130
root
131131
}
132132

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

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ test('deep import with exports field + mapped dir', async () => {
3636
)
3737
})
3838

39+
// this is how Svelte 3 is packaged
40+
test('deep import with exports and legacy fallback', async () => {
41+
expect(await page.textContent('.exports-legacy-fallback')).toMatch(
42+
'[success]'
43+
)
44+
})
45+
3946
test('Respect exports field env key priority', async () => {
4047
expect(await page.textContent('.exports-env')).toMatch('[success]')
4148
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const msg = '[fail] mapped js file'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const msg = '[success] mapped mjs file'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"main": "index.js",
3+
"module": "index.mjs"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "resolve-exports-legacy-fallback",
3+
"private": true,
4+
"version": "1.0.0",
5+
"exports": {
6+
"./dir": {
7+
"import": "./dir/index.mjs",
8+
"require": "./dir/index.js"
9+
},
10+
".": "index.js"
11+
}
12+
}

‎playground/resolve/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ <h2>Deep import with exports field + mapped directory</h2>
2424
<h2>Exports field env priority</h2>
2525
<p class="exports-env">fail</p>
2626

27+
<h2>Exports with legacy fallback</h2>
28+
<p class="exports-legacy-fallback">fail</p>
29+
2730
<h2>Resolve /index.*</h2>
2831
<p class="index">fail</p>
2932

@@ -163,6 +166,9 @@ <h2>resolve package that contains # in path</h2>
163166
import { msg as exportsEnvMsg } from 'resolve-exports-env'
164167
text('.exports-env', exportsEnvMsg)
165168

169+
import { msg as exportsLegacyFallbackMsg } from 'resolve-exports-legacy-fallback/dir'
170+
text('.exports-legacy-fallback', exportsLegacyFallbackMsg)
171+
166172
// implicit index resolving
167173
import { foo } from './util'
168174
text('.index', foo())

‎playground/resolve/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"resolve-custom-condition": "link:./custom-condition",
2121
"resolve-custom-main-field": "link:./custom-main-field",
2222
"resolve-exports-env": "link:./exports-env",
23+
"resolve-exports-legacy-fallback": "link:./exports-legacy-fallback",
2324
"resolve-exports-path": "link:./exports-path",
2425
"resolve-linked": "workspace:*"
2526
}

‎pnpm-lock.yaml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.