What is Tapable?
Tapable is a small library that webpack uses to create hooks. These hooks are extension points where plugins can register callbacks to execute at specific times.Hook Types
Tapable provides several hook types, each with different execution patterns:Sync Hooks
SyncHook
The simplest hook - calls all taps sequentially.
SyncBailHook
Like SyncHook, but stops executing if any tap returns a non-undefined value.
SyncWaterfallHook
Passes the return value of each tap to the next tap.
SyncLoopHook
Keeps looping through all taps while any tap returns a non-undefined value.
Async Hooks
AsyncSeriesHook
Executes async taps in series.
AsyncSeriesBailHook
Like AsyncSeriesHook, but stops if a tap returns a non-undefined value.
AsyncSeriesWaterfallHook
Like SyncWaterfallHook but async.
AsyncParallelHook
Executes async taps in parallel.
AsyncParallelBailHook
Like AsyncParallelHook, but bails when any tap returns a result.
Hook Maps
HookMap allows you to create multiple hooks with different keys.Tap Methods
.tap() - Synchronous
.tapAsync() - Callback-based
.tapPromise() - Promise-based
Tap Options
name
Required. Identifies your plugin.
stage
Controls execution order. Lower numbers run first.
before
Run before specific plugins.
after
Run after specific plugins.
Interceptors
Interceptors allow you to monitor and control hook behavior.Context
Share data between taps using context.Complete Example: Custom Hook System
Best Practices
1. Always Name Your Taps
2. Use Appropriate Hook Type
3. Handle Errors in Async Hooks
4. Use Stage for Ordering
5. Return Boolean from Bail Hooks
See Also
- Tapable GitHub - Official repository
- Compiler Hooks - Hooks in the compiler
- Compilation Hooks - Hooks in compilation