Skip to content

Conversation

@sapphi-red
Copy link
Member

Description

Currently await import('optional-peer-dep') and require('optional-peer-dep') does not throw an runtime error after bundling with Vite. This is caused by mocking the optional peer dep module (introduced by #9321).

Note: This current mocking behavior can be skipped by adding the module to external option.

While we still need to keep the module mocked for require (see comment in the code for details), we can make await import('optional-peer-dep') to throw an runtime error to align with the input behavior (although, import 'optional-peer-dep' should be an early error, not a runtime error).

refs #6007 #9321 vitejs#165 rolldown/rolldown#4051 (comment)

Comment on lines +492 to 498
// 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 {}'
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this part and fix the require case when we migrate to rolldown (vitejs#167).

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. Good catch 👍

@punkpeye
Copy link

Any reason for not merging this?

@sapphi-red
Copy link
Member Author

Any reason for not merging this?

I'm holding off until another team member has a chance to review it, or until I can revisit it with fresh eyes.

@sapphi-red sapphi-red merged commit d0221cd into vitejs:main May 27, 2025
25 of 26 checks passed
@sapphi-red sapphi-red deleted the fix/import-optional-peer-dep-should-throw-error branch June 7, 2025 17:31
github-merge-queue bot pushed a commit to rolldown/rolldown that referenced this pull request Jun 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

has workaround p2-edge-case Bug, but has workaround or limited in scope (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants