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): use only root package.json as exports source #11259

Merged
merged 6 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ export function tryNodeResolve(
// nearest package.json that may have the `exports` field
let pkg: PackageData | undefined

let pkgId = possiblePkgIds.reverse().find((pkgId) => {
bluwy marked this conversation as resolved.
Show resolved Hide resolved
let pkgId = possiblePkgIds.find((pkgId) => {
nearestPkg = resolvePackageData(
pkgId,
basedir,
Expand Down
4 changes: 4 additions & 0 deletions playground/resolve/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ test('deep import with exports field + mapped dir', async () => {
)
})

test('exports read from the root package.json', async () => {
expect(await page.textContent('.exports-from-root')).toMatch('[success]')
})

// this is how Svelte 3 is packaged
test('deep import with exports and legacy fallback', async () => {
expect(await page.textContent('.exports-legacy-fallback')).toMatch(
Expand Down
1 change: 1 addition & 0 deletions playground/resolve/exports-from-root/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msg = '[success] exports from root (./file.js)'
Empty file.
1 change: 1 addition & 0 deletions playground/resolve/exports-from-root/nested/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msg = 'fail exports from root (./nested/file.js)'
5 changes: 5 additions & 0 deletions playground/resolve/exports-from-root/nested/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"exports": {
".": "./file.js"
}
}
9 changes: 9 additions & 0 deletions playground/resolve/exports-from-root/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@vitejs/test-resolve-exports-from-root",
"private": true,
"version": "1.0.0",
"exports": {
".": "./index.js",
"./nested": "./file.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 field read only from the root package.json</h2>
<p class="exports-from-root">fail</p>

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

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

import { msg as exportsFromRootMsg } from '@vitejs/test-resolve-exports-from-root/nested'
text('.exports-from-root', exportsFromRootMsg)

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

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 @@
"@vitejs/test-resolve-custom-condition": "link:./custom-condition",
"@vitejs/test-resolve-custom-main-field": "link:./custom-main-field",
"@vitejs/test-resolve-exports-env": "link:./exports-env",
"@vitejs/test-resolve-exports-from-root": "link:./exports-from-root",
"@vitejs/test-resolve-exports-legacy-fallback": "link:./exports-legacy-fallback",
"@vitejs/test-resolve-exports-path": "link:./exports-path",
"@vitejs/test-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.