From 7f59dcfae84905f24282329aa10988548c9b91d8 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 15 Nov 2022 15:38:55 +0800 Subject: [PATCH] fix(ssr): skip optional peer dep resolve (v3) (#10593) (#10931) fix(ssr): skip optional peer dep resolve (#10593) --- packages/vite/src/node/plugins/resolve.ts | 15 +++++++++------ packages/vite/src/node/ssr/ssrModuleLoader.ts | 3 ++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index fcbd7644b6d857..94b6562b1b5706 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -105,6 +105,8 @@ export interface InternalResolveOptions extends Required { // Resolve using esbuild deps optimization getDepsOptimizer?: (ssr: boolean) => DepsOptimizer | undefined shouldExternalize?: (id: string) => boolean | undefined + // Check this resolve is called from `hookNodeResolve` in SSR + isHookNodeResolve?: boolean } export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { @@ -690,10 +692,11 @@ export function tryNodeResolve( // if import can't be found, check if it's an optional peer dep. // if so, we can resolve to a special id that errors only when imported. if ( + !options.isHookNodeResolve && basedir !== root && // root has no peer dep - !isBuiltin(id) && - !id.includes('\0') && - bareImportRE.test(id) + !isBuiltin(nestedPath) && + !nestedPath.includes('\0') && + bareImportRE.test(nestedPath) ) { // find package.json with `name` as main const mainPackageJson = lookupFile(basedir, ['package.json'], { @@ -702,11 +705,11 @@ export function tryNodeResolve( if (mainPackageJson) { const mainPkg = JSON.parse(mainPackageJson) if ( - mainPkg.peerDependencies?.[id] && - mainPkg.peerDependenciesMeta?.[id]?.optional + mainPkg.peerDependencies?.[nestedPath] && + mainPkg.peerDependenciesMeta?.[nestedPath]?.optional ) { return { - id: `${optionalPeerDepId}:${id}:${mainPkg.name}` + id: `${optionalPeerDepId}:${nestedPath}:${mainPkg.name}` } } } diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index 0e4e163570a575..eaae5e73015b46 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -127,7 +127,8 @@ async function instantiateModule( isBuild: true, isProduction, isRequire: true, - root + root, + isHookNodeResolve: true } // Since dynamic imports can happen in parallel, we need to