Skip to content

Commit 7f59dcf

Browse files
authored
fix(ssr): skip optional peer dep resolve (v3) (#10593) (#10931)
fix(ssr): skip optional peer dep resolve (#10593)
1 parent 3ba45b9 commit 7f59dcf

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

packages/vite/src/node/plugins/resolve.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ export interface InternalResolveOptions extends Required<ResolveOptions> {
105105
// Resolve using esbuild deps optimization
106106
getDepsOptimizer?: (ssr: boolean) => DepsOptimizer | undefined
107107
shouldExternalize?: (id: string) => boolean | undefined
108+
// Check this resolve is called from `hookNodeResolve` in SSR
109+
isHookNodeResolve?: boolean
108110
}
109111

110112
export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
@@ -690,10 +692,11 @@ export function tryNodeResolve(
690692
// if import can't be found, check if it's an optional peer dep.
691693
// if so, we can resolve to a special id that errors only when imported.
692694
if (
695+
!options.isHookNodeResolve &&
693696
basedir !== root && // root has no peer dep
694-
!isBuiltin(id) &&
695-
!id.includes('\0') &&
696-
bareImportRE.test(id)
697+
!isBuiltin(nestedPath) &&
698+
!nestedPath.includes('\0') &&
699+
bareImportRE.test(nestedPath)
697700
) {
698701
// find package.json with `name` as main
699702
const mainPackageJson = lookupFile(basedir, ['package.json'], {
@@ -702,11 +705,11 @@ export function tryNodeResolve(
702705
if (mainPackageJson) {
703706
const mainPkg = JSON.parse(mainPackageJson)
704707
if (
705-
mainPkg.peerDependencies?.[id] &&
706-
mainPkg.peerDependenciesMeta?.[id]?.optional
708+
mainPkg.peerDependencies?.[nestedPath] &&
709+
mainPkg.peerDependenciesMeta?.[nestedPath]?.optional
707710
) {
708711
return {
709-
id: `${optionalPeerDepId}:${id}:${mainPkg.name}`
712+
id: `${optionalPeerDepId}:${nestedPath}:${mainPkg.name}`
710713
}
711714
}
712715
}

packages/vite/src/node/ssr/ssrModuleLoader.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ async function instantiateModule(
127127
isBuild: true,
128128
isProduction,
129129
isRequire: true,
130-
root
130+
root,
131+
isHookNodeResolve: true
131132
}
132133

133134
// Since dynamic imports can happen in parallel, we need to

0 commit comments

Comments
 (0)