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

feat(resolve): only use exports field when defined in package.json #8484

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,19 +773,17 @@ export function resolvePackageEntry(
// using https:/lukeed/resolve.exports
if (data.exports) {
entryPoint = resolveExports(data, '.', options, targetWeb)
}

// 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
// fields are present, prioritize those instead.
if (targetWeb && (!entryPoint || entryPoint.endsWith('.mjs'))) {
if (!entryPoint) {
packageEntryFailure(id)
}
} else if (targetWeb) {
// check browser field
// https:/defunctzombie/package-browser-field-spec
const browserEntry =
typeof data.browser === 'string'
? data.browser
: isObject(data.browser) && data.browser['.']

if (browserEntry) {
// check if the package also has a "module" field.
if (
Expand Down Expand Up @@ -820,15 +818,15 @@ export function resolvePackageEntry(
}
}

if (!entryPoint || entryPoint.endsWith('.mjs')) {
if (!entryPoint) {
for (const field of options.mainFields || DEFAULT_MAIN_FIELDS) {
if (typeof data[field] === 'string') {
entryPoint = data[field]
break
}
}
entryPoint ||= data.main
}
entryPoint ||= data.main

// try default entry when entry is not define
// https://nodejs.org/api/modules.html#all-together
Expand Down Expand Up @@ -871,7 +869,7 @@ export function resolvePackageEntry(
packageEntryFailure(id)
}

function packageEntryFailure(id: string, details?: string) {
function packageEntryFailure(id: string, details?: string): never {
throw new Error(
`Failed to resolve entry for package "${id}". ` +
`The package may have incorrect main/module/exports specified in its package.json` +
Expand Down