@sewing-kit/hooks
Interfaces and types for sewing-kit
hooks.
Installation
yarn add @sewing-kit/hooks --dev
Overview
Hooks, along with plugins, form the functional heart of sewing-kit
. Inspired by webpack's tapable library (guide), hooks provide a way for plugins to tap into sewing-kit
tasks in order to impart functionality to your workspace's dev setup.
Calling hooks
The two types of hooks provided are a SeriesHook
and a WaterfallHook
. Both of these clases provide a hook
and run
method
import {SeriesHook} from '@sewing-kit/hooks';
const dog = {
breed: new SeriesHook<string>(),
};
dog.breed.hook((breed) => {
console.log('Received changes on the breed: ', breed);
});
dog.breed.run('American Bully');
SeriesHook vs WaterfallHooks
When run
is called on a SeriesHook
all the conainted hooks are resolved in sequential order. With a Waterfall
hook, the result of a hook is passed in as the argument for the next hook.
Usage
Adding hooks
A lot of your use cases can probably covered by the hooks provided by core sewing-kit
, but you can always add your own hooks via plugins and a task's configureHooks
hook (adding hooks with hooks). For example, if you add, say, a plugin that introduces Jest to your workspace's testing setup, that plugin can also add a hook to let other plugins hook into its Jest configuration.
hooks.configureHooks.hook(
addHooks<JestWorkspaceHooks>(() => ({
jestSetupEnv: new WaterfallHook(),
jestSetupTests: new WaterfallHook(),
jestWatchPlugins: new WaterfallHook(),
jestConfig: new WaterfallHook(),
jestFlags: new WaterfallHook(),
})),
);
Configuring/tapping into hooks
Following from the previous example, another plugin further down might then do something like:
createWorkspaceTestPlugin('CustomPlugin', ({hooks}) => {
hooks.configure.hook((hooks) => {
hooks.jestSetupEnv.hook((oldJestSetupEnv) => {
});
hooks.jestConfig.hook((oldJestConfig) => {
});
});
});
When you run sk test
, sewing-kit
will pick up your configs and plugins and run Jest with your specified option/config values.