Feature: Custom Hooks
See original GitHub issueCustom hooks would be useful for plugin creators looking to extend an already existing plugin’s functionality. For example, let’s say I wanted to create a plugin that adds to (or modifies) the presets in unplugin-auto-import; I could use the “presets” hook to gain access to the presets map and modify it from there. Since webpack already allows plugins to create their own hooks, we can see some already existing use cases, like how the html-webpack-plugin lets other plugins modify the HTML output in various locations.
Proof of Concept
Here’s a rough design of what the custom hooks API could look like.
Creating hooks:
import { createUnplugin, SyncHook } from 'unplugin';
import { SyncHook } from 'unplugin/hooks'; // Maybe even separate hook types into another import
export const unplugin = createUnplugin((options) => {
return {
name: 'unplugin-auto-import',
hooks: {
presets: SyncHook, // Define the hook
},
buildStart() {
this.hooks.presets(currentPresets); // Run the hook
},
};
});
export const hooks = unplugin.hooks
Using hooks:
import { createUnplugin } from 'unplugin';
import UnpluginAutoImportHooks from 'unplugin-auto-import/hooks';
export const unplugin = createUnplugin((options) => {
return {
name: 'unplugin-auto-import-presets',
// Use hook from another unplugin
[ UnpluginAutoImportHooks.presets /* this is a symbol to ensure uniqueness */ ](presets) {
presets['my-preset'] = '...';
delete presets.react;
},
};
});
Issue Analytics
- State:
- Created a year ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Building Your Own Hooks
Custom Hooks offer the flexibility of sharing logic that wasn't possible in React components before. You can write custom Hooks that cover a...
Read more >How to create your own custom React Hooks
In React, a custom Hook is a function that starts with the word “use” and may call other Hooks. The “useWhatever” naming convention...
Read more >useHooks - Easy to understand React Hook recipes
Hooks are a feature in React that allow you use state and other React features without writing classes. This website provides easy to...
Read more >React Custom Hooks
Hooks are reusable functions. When you have component logic that needs to be used by multiple components, we can extract that logic to...
Read more >React: How to create a Custom Hook
Custom Hooks consist of built-in React Hooks or other custom Hooks. Therefore a custom Hook is always a new composition of one or...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
One way I think could be more generic is that we implement the Rollups’
api
props that basically allowing arbitrary objects to pass in, and let plugins define the interface (via bookable or plain values). https://rollupjs.org/guide/en/#inter-plugin-communication@antfu, I have created a new proposal that includes your suggestions and follows closer to Rollup’s API. I also included a method to create new plugin instances (if they do not already exist) (suggested in https://github.com/unjs/unplugin/issues/173).
Closing in favor of https://github.com/unjs/unplugin/issues/174.