-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
chore: add PluginWithRequiredHook
type & extract getHookHandler
function
#14845
Conversation
Run & review this pull request in StackBlitz Codeflow. |
3c02c22
to
d262d59
Compare
Thanks for the PR! This looks like a great refactoring to me, and it shows some inconsistencies as you described. I think we should change the implementation to match Rollup's version. If the hook is a function, I think it should be called and not go with its |
WithRequiredHook
type & extract getHookHandler
function PluginWithRequiredHook
type & extract getHookHandler
function
@@ -959,7 +960,7 @@ function injectSsrFlagToHooks(plugin: Plugin): Plugin { | |||
function wrapSsrResolveId(hook?: Plugin['resolveId']): Plugin['resolveId'] { | |||
if (!hook) return | |||
|
|||
const fn = 'handler' in hook ? hook.handler : hook | |||
const fn = getHookHandler(hook) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was also thinking if we should handle if fn
is undefined
here, but I suppose the types always restricts that you need a handler
with the object-form, so I suppose this is also fine for now.
Description
This PR mainly refactors the code to extract the common
getHookHandler
utility to get hook from object style hooks.The repeated pattern of:
is extracted into
getHookHandler
with correct TypeScript type to reduce duplication.This PR also added a
WithRequiredHook
type to get a more precise plugin type when requiring the specific hook.Based on this
if
statement, we can guarantee that all returned Plugins has the hook of hookName.vite/packages/vite/src/node/plugins/index.ts
Lines 145 to 157 in 90e6108
We should pass this information on in subsequent code. Equally,
getHookHandler
's extraction relies on this more precise return typeThis PR is a pure refactoring to improve code structure. No behavior or functionality changes.
Additional context
One usage haven't refactor to
getHookHandler
directly due to its different implementation, I've marked with TODO to align in a follow up.vite/packages/vite/src/node/plugins/index.ts
Lines 123 to 125 in 7950636
Additionally, The implementation of object style hook in Rollup is:
https://github.com/rollup/rollup/blob/fbf806aceffd822d43e4603b664c54165c72cf36/src/utils/PluginDriver.ts#L322
Comparing the two implementations of Vite, which one is the best?
If someone wrote a plugin like this, Vite will act different with Rollup, should we keep this
'handler' in hook
logic?What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).