question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: Custom Hooks

See original GitHub issue

Custom 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:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
antfucommented, Sep 23, 2022

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

0reactions
jwr12135commented, Oct 2, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found