Skip to content

Commit

Permalink
fix(ssr): skip optional peer dep resolve (#10593)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 14, 2022
1 parent f6ad607 commit 0a69985
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
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 @@ -687,10 +689,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 @@ -699,11 +702,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
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 0a69985

Please sign in to comment.