diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 1c230c128ffc8e..35999d0024778b 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -812,14 +812,7 @@ export function resolvePlugin( } } if (id.startsWith(optionalPeerDepId)) { - const [, peerDep, parentDep, isRequire] = id.split(':') - // rollup + @rollup/plugin-commonjs hoists dynamic `require`s by default - // If we add a `throw` statement, it will be injected to the top-level and break the whole bundle - // Instead, we mock the module for now - // This can be fixed when we migrate to rolldown - if (isRequire === 'true' && isProduction) { - return 'export default {}' - } + const [, peerDep, parentDep] = id.split(':') return ( 'export default {};' + `throw new Error(\`Could not resolve "${peerDep}" imported by "${parentDep}".${isProduction ? '' : ' Is it installed?'}\`)` @@ -1088,7 +1081,7 @@ export function tryNodeResolve( mainPkg.peerDependenciesMeta?.[pkgName]?.optional ) { return { - id: `${optionalPeerDepId}:${id}:${mainPkg.name}:${!!options.isRequire}`, + id: `${optionalPeerDepId}:${id}:${mainPkg.name}`, } } } diff --git a/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/playground/optimize-deps/__tests__/optimize-deps.spec.ts index 430f3c62b6a835..df8f34382fae4c 100644 --- a/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -142,10 +142,9 @@ test('dep with optional peer dep (cjs)', async () => { await expect .poll(() => page.textContent('.dep-with-optional-peer-dep-cjs')) .toMatch(`[success]`) - // FIXME - // await expect - // .poll(() => page.textContent('.dep-with-optional-peer-dep-cjs-error')) - // .toMatch(`[success]`) + await expect + .poll(() => page.textContent('.dep-with-optional-peer-dep-cjs-error')) + .toMatch(`[success]`) }) test('dep with css import', async () => {