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): ensure exports has precedence over mainFields #11234

Merged
merged 1 commit into from
Dec 7, 2022
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
6 changes: 5 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,8 @@ export function resolvePackageEntry(
entryPoint = resolveExports(data, '.', options, targetWeb)
}

const resolvedFromExports = !!entryPoint

// if exports resolved to .mjs, still resolve other fields.
// This is because .mjs files can technically import .cjs files which would
// make them invalid for pure ESM environments - so if other module/browser
Expand Down Expand Up @@ -980,7 +982,9 @@ export function resolvePackageEntry(
}
}

if (!entryPoint || entryPoint.endsWith('.mjs')) {
// fallback to mainFields if still not resolved
// TODO: review if `.mjs` check is still needed
if (!resolvedFromExports && (!entryPoint || entryPoint.endsWith('.mjs'))) {
for (const field of options.mainFields) {
if (field === 'browser') continue // already checked above
if (typeof data[field] === 'string') {
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 @@ -53,6 +53,10 @@ test('Respect production/development conditionals', async () => {
)
})

test('Respect exports to take precedence over mainFields', async () => {
expect(await page.textContent('.exports-with-module')).toMatch('[success]')
})

test('implicit dir/index.js', async () => {
expect(await page.textContent('.index')).toMatch('[success]')
})
Expand Down
2 changes: 2 additions & 0 deletions playground/resolve/exports-with-module/import.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// import.mjs should take precedence
export const msg = '[success] exports with module (import.mjs)'
2 changes: 2 additions & 0 deletions playground/resolve/exports-with-module/module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// import.mjs should take precedence
export const msg = '[fail] exports with module (module.mjs)'
10 changes: 10 additions & 0 deletions playground/resolve/exports-with-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@vitejs/test-resolve-exports-with-module",
"private": true,
"version": "1.0.0",
"type": "commonjs",
"module": "./module.mjs",
"exports": {
"import": "./import.mjs"
}
}
6 changes: 6 additions & 0 deletions playground/resolve/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ <h2>Exports field env priority</h2>
<h2>Exports with legacy fallback</h2>
<p class="exports-legacy-fallback">fail</p>

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

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

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

import { msg as exportsWithModule } from '@vitejs/test-resolve-exports-with-module'
text('.exports-with-module', exportsWithModule)

// 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 @@ -22,6 +22,7 @@
"@vitejs/test-resolve-exports-env": "link:./exports-env",
"@vitejs/test-resolve-exports-legacy-fallback": "link:./exports-legacy-fallback",
"@vitejs/test-resolve-exports-path": "link:./exports-path",
"@vitejs/test-resolve-exports-with-module": "link:./exports-with-module",
"@vitejs/test-resolve-linked": "workspace:*"
}
}
5 changes: 5 additions & 0 deletions pnpm-lock.yaml

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