Skip to content

Commit

Permalink
fix(ssr): skip optional peer dep resolve (v3) (#10593) (#10931)
Browse files Browse the repository at this point in the history
fix(ssr): skip optional peer dep resolve (#10593)
  • Loading branch information
bluwy authored Nov 15, 2022
1 parent 3ba45b9 commit 7f59dcf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
15 changes: 9 additions & 6 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export interface InternalResolveOptions extends Required<ResolveOptions> {
// 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 {
Expand Down Expand Up @@ -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'], {
Expand All @@ -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}`
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7f59dcf

Please sign in to comment.