Skip to content

fix: importing an optional peer dep should throw an runtime error #20029

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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?

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