menuEvent Channels
Tutorialkeyboard_arrow_right
Concepts and Featureskeyboard_arrow_right
How Tokeyboard_arrow_right

Event Channels

Plugins in Katnip can listen to different events, each corresponding to a specific event channel. Each channel maps to a particular export from the plugin. For example, to run code when Katnip is executed from the command line, the code should be placed in the katnip-project-hooks export. Functions exported through this channel will be triggered by events that match the function name. For instance, if we want to run some code when the development server starts (i.e., when the katnip dev command is executed), we should hook into the dev event.

To make this concrete, let's create a plugin that hooks into the dev event of the katnip-project-hooks channel. The steps are as follows:

  • In the package.json of your plugin, add the export for ./katnip-project-hooks, pointing to a file within the plugin (e.g., ./myprojecthooks.js). Be sure to also include the katnip-plugin keyword.
{   
    keywords: ["katnip-plugin"],
    exports: {
        "./katnip-project-hooks": "./myprojecthooks.js"
}

In the file referenced by ./katnip-project-hooks (e.g., ./myprojecthooks.js), add the code to listen for the dev event:

export async function dev(ev) {
    // code goes here...
}