From 95c44b9efb8d481a657729fd22c9dd13a952df48 Mon Sep 17 00:00:00 2001 From: patak Date: Tue, 28 Mar 2023 11:50:21 +0200 Subject: [PATCH] fix: use nearest pkg to resolved for moduleSideEffects --- packages/vite/src/node/plugins/resolve.ts | 35 ++++++++++++++--------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 0d6b732e5ade2c..730035386f03ea 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -254,19 +254,20 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { } if ((res = tryFsResolve(fsPath, options))) { + const resPkg = findNearestPackageData( + path.dirname(res), + options.packageCache, + ) res = ensureVersionQuery(res, id, options, depsOptimizer) isDebug && debug(`[relative] ${colors.cyan(id)} -> ${colors.dim(res)}`) - const pkg = - importer && - findNearestPackageData(path.dirname(importer), options.packageCache) - if (pkg) { - return { - id: res, - moduleSideEffects: pkg.hasSideEffects(res), - } - } - return res + + return resPkg + ? { + id: res, + moduleSideEffects: resPkg.hasSideEffects(res), + } + : res } } @@ -1191,10 +1192,16 @@ function tryResolveBrowserMapping( ) { isDebug && debug(`[browser mapped] ${colors.cyan(id)} -> ${colors.dim(res)}`) - const result = { - id: res, - moduleSideEffects: pkg.hasSideEffects(res), - } + const resPkg = findNearestPackageData( + path.dirname(res), + options.packageCache, + ) + const result = resPkg + ? { + id: res, + moduleSideEffects: resPkg.hasSideEffects(res), + } + : { id: res } return externalize ? { ...result, external: true } : result } } else if (browserMappedPath === false) {