Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): skip optional peer dep resolve (v3) (#10593) #10931

Merged
merged 1 commit into from Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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
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