Skip to content

Commit 0a69985

Browse files
authoredNov 14, 2022
fix(ssr): skip optional peer dep resolve (#10593)
1 parent f6ad607 commit 0a69985

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 {
@@ -687,10 +689,11 @@ export function tryNodeResolve(
687689
// if import can't be found, check if it's an optional peer dep.
688690
// if so, we can resolve to a special id that errors only when imported.
689691
if (
692+
!options.isHookNodeResolve &&
690693
basedir !== root && // root has no peer dep
691-
!isBuiltin(id) &&
692-
!id.includes('\0') &&
693-
bareImportRE.test(id)
694+
!isBuiltin(nestedPath) &&
695+
!nestedPath.includes('\0') &&
696+
bareImportRE.test(nestedPath)
694697
) {
695698
// find package.json with `name` as main
696699
const mainPackageJson = lookupFile(basedir, ['package.json'], {
@@ -699,11 +702,11 @@ export function tryNodeResolve(
699702
if (mainPackageJson) {
700703
const mainPkg = JSON.parse(mainPackageJson)
701704
if (
702-
mainPkg.peerDependencies?.[id] &&
703-
mainPkg.peerDependenciesMeta?.[id]?.optional
705+
mainPkg.peerDependencies?.[nestedPath] &&
706+
mainPkg.peerDependenciesMeta?.[nestedPath]?.optional
704707
) {
705708
return {
706-
id: `${optionalPeerDepId}:${id}:${mainPkg.name}`
709+
id: `${optionalPeerDepId}:${nestedPath}:${mainPkg.name}`
707710
}
708711
}
709712
}

‎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)
Please sign in to comment.