chore: add PluginWithRequiredHook
type & extract getHookHandler
function
#14845
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
).