From 2141d3132d297c6dc3ad5698ef508fd08f5229a9 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 24 Oct 2023 00:05:39 +0800 Subject: [PATCH] fix(resolve)!: remove special .mjs handling (#14723) --- docs/guide/migration.md | 2 ++ packages/vite/src/node/plugins/resolve.ts | 16 +++------------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/docs/guide/migration.md b/docs/guide/migration.md index 65d8b4e78c6101..99e39a4e37fa81 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -133,6 +133,8 @@ Also there are other breaking changes which only affect few users. - Renamed `ResolveWorkerOptions` type to `ResolvedWorkerOptions` - [[#5657] fix: return 404 for resources requests outside the base path](https://github.com/vitejs/vite/pull/5657) - In the past, Vite responded to requests outside the base path without `Accept: text/html`, as if they were requested with the base path. Vite no longer does that and responds with 404 instead. +- [[#14723] fix(resolve)!: remove special .mjs handling](https://github.com/vitejs/vite/pull/14723) + - In the past, when a library `"exports"` field maps to an `.mjs` file, Vite will still try to match the `"browser"` and `"module"` fields to fix compatibility with certain libraries. This behavior is now removed to align with the exports resolution algorithm. ## Migration from v3 diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 7d9a1cc5093b51..556a85101b3db7 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -987,17 +987,8 @@ export function resolvePackageEntry( ) } - 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 - // fields are present, prioritize those instead. - if ( - targetWeb && - options.browserField && - (!entryPoint || entryPoint.endsWith('.mjs')) - ) { + // handle edge case with browser and module field semantics + if (!entryPoint && targetWeb && options.browserField) { // check browser field // https://github.com/defunctzombie/package-browser-field-spec const browserEntry = @@ -1039,8 +1030,7 @@ export function resolvePackageEntry( } // fallback to mainFields if still not resolved - // TODO: review if `.mjs` check is still needed - if (!resolvedFromExports && (!entryPoint || entryPoint.endsWith('.mjs'))) { + if (!entryPoint) { for (const field of options.mainFields) { if (field === 'browser') continue // already checked above if (typeof data[field] === 'string') {