Skip to content

Commit

Permalink
fix: prefer exports when resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Oct 7, 2022
1 parent 34f5be7 commit 8902b09
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 5 deletions.
23 changes: 19 additions & 4 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,25 @@ export function tryNodeResolve(
}

let pkg: PackageData | undefined
const pkgId = possiblePkgIds.reverse().find((pkgId) => {
pkg = resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache)!
return pkg
})!
let pkgId: string

const rootPkg =
possiblePkgIds.length &&
resolvePackageData(
possiblePkgIds[0],
basedir,
preserveSymlinks,
packageCache
)!
if (rootPkg && rootPkg?.data?.exports) {
pkg = rootPkg
pkgId = possiblePkgIds[0]
} else {
pkgId = possiblePkgIds.reverse().find((pkgId) => {
pkg = resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache)!
return pkg
})!
}

if (!pkg) {
// if import can't be found, check if it's an optional peer dep.
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async function instantiateModule(
preserveSymlinks,
isBuild: true,
isProduction,
isRequire: true,
isRequire: false,
root
}

Expand Down
7 changes: 7 additions & 0 deletions playground/resolve/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ test('deep import with exports field + mapped dir', async () => {
)
})

// this is how Svelte 3 is packaged
test('deep import with exports and legacy fallback', async () => {
expect(await page.textContent('.exports-legacy-fallback')).toMatch(
'[success]'
)
})

test('Respect exports field env key priority', async () => {
expect(await page.textContent('.exports-env')).toMatch('[success]')
})
Expand Down
1 change: 1 addition & 0 deletions playground/resolve/exports-legacy-fallback/dir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msg = '[fail] mapped js file'
1 change: 1 addition & 0 deletions playground/resolve/exports-legacy-fallback/dir/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msg = '[success] mapped mjs file'
4 changes: 4 additions & 0 deletions playground/resolve/exports-legacy-fallback/dir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "index.js",
"model": "index.mjs"
}
1 change: 1 addition & 0 deletions playground/resolve/exports-legacy-fallback/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 5
12 changes: 12 additions & 0 deletions playground/resolve/exports-legacy-fallback/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "resolve-exports-legacy-fallback",
"private": true,
"version": "1.0.0",
"exports": {
"./dir": {
"import": "./dir/index.mjs",
"require": "./dir/index.js"
},
".": "index.js"
}
}
6 changes: 6 additions & 0 deletions playground/resolve/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ <h2>Deep import with exports field + mapped directory</h2>
<h2>Exports field env priority</h2>
<p class="exports-env">fail</p>

<h2>Exports with legacy fallback</h2>
<p class="exports-legacy-fallback">fail</p>

<h2>Resolve /index.*</h2>
<p class="index">fail</p>

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

import { msg as exportsLegacyFallbackMsg } from 'resolve-exports-legacy-fallback/dir'
text('.exports-legacy-fallback', exportsLegacyFallbackMsg)

// implicit index resolving
import { foo } from './util'
text('.index', foo())
Expand Down
1 change: 1 addition & 0 deletions playground/resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"resolve-custom-condition": "link:./custom-condition",
"resolve-custom-main-field": "link:./custom-main-field",
"resolve-exports-env": "link:./exports-env",
"resolve-exports-legacy-fallback": "link:./exports-legacy-fallback",
"resolve-exports-path": "link:./exports-path",
"resolve-linked": "workspace:*"
}
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8902b09

Please sign in to comment.