From eac376e3d3a636840e8aa056e701b2c7f716792d Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Thu, 23 Mar 2023 17:35:06 +0800 Subject: [PATCH] perf(resolve): fix browser mapping nearest package.json check (#12550) --- packages/vite/src/node/plugins/resolve.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index a40b2542103729..7a07313131e6ce 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -827,8 +827,8 @@ export function tryNodeResolve( !( ext === '.cjs' || (ext === '.js' && - findNearestPackageData(resolved, options.packageCache)?.data.type !== - 'module') + findNearestPackageData(path.dirname(resolved), options.packageCache) + ?.data.type !== 'module') ) && !(include?.includes(pkgId) || include?.includes(id))) @@ -1206,7 +1206,7 @@ function tryResolveBrowserMapping( const pkg = importer && (idToPkgMap.get(importer) || - findNearestPackageData(importer, options.packageCache)) + findNearestPackageData(path.dirname(importer), options.packageCache)) if (pkg && isObject(pkg.data.browser)) { const mapId = isFilePath ? './' + slash(path.relative(pkg.dir, id)) : id const browserMappedPath = mapWithBrowserField(mapId, pkg.data.browser)