Every tapable hook compiles its own call method at runtime
Just a little module for plugins.
At a glance
- What is it?
- tapable is the hook primitive webpack is built on, and its speed comes from generating specialised code per hook at run time. The ordering options fail silently on a typo, the promise tap throws rather than warns, and the project's own benchmark script runs with the optimiser disabled and the garbage collector pinned so the numbers measure the generated code.
- Who is it for?
- tapable is worth reading as an example of what a plugin interface costs, because it makes its trade-offs explicit: it generates code rather than dispatching through a loop, and it lets you order plugins by stage or by name. Two things to know before you depend on it.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 2 days ago.
- What is it written in?
- Mainly JavaScript, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 14, 2026, and from our analysis. They are not legal advice.
Star history
- webpack/tapable3,865 ★ · +4
| Repository | 2026-09-24 | 2026-11-14 |
|---|---|---|
| webpack/tapable | 3.9k | 3.9k |
4 Stars in the last 51 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
The hook compiles its own call method
The mechanism that makes this library fast is stated in a paragraph rather than buried: the hook compiles a method with the most efficient way of running your plugins, generating code at run time. Five inputs decide which code comes out. How many plugins are registered, counted as none, one or many. What kind they are, synchronous, callback based or promise based. Which call method the declaring class uses. How many arguments there are. And whether interception is in play. So a hook with one synchronous plugin and no interception gets one small compiled body, while the same hook with several mixed plugins gets something else entirely, and both were written after your application started running. The class that declares the hooks is told to call them, and the library decides what that call actually executes.
The constructor takes one optional argument, a list of argument names as strings, and those names exist for the generator rather than for runtime checks:
const hook = new SyncHook(["arg1", "arg2", "arg3"]);Which is also why the count of arguments is one of the five inputs above. Every combination of the inputs produces a different compiled body, so the library is really a small source generator with a plugin-facing API on top, and the API is deliberately narrow: three registration methods, three ordering fields, and a call method per hook.
A before reference with a typo changes nothing
Ordering is controlled by an options object on each registration, and one of its three fields fails quietly. The name is required and identifies the tap for debugging, for interceptors and for the third field. The stage is a number defaulting to zero, where lower runs earlier and equal stages run in registration order. The before field takes a single name or a list, and it wins over the stage for the taps it actually targets, while everything else stays ordered by stage. Then the failure mode: unknown names in before are ignored. A plugin name that does not match anything produces no error, no warning and no change in ordering. If your plugin depends on running before another one, that dependency is expressed by a string and a typo in it is invisible, so ordering bugs in this library tend to arrive as an intermittent one.
withOptions hands out a hook you cannot call
There is a convenience for libraries that add many taps and want them ordered the same way. Calling withOptions on a hook returns a facade whose registration methods merge the given options into every tap, so a library can offer plugins a hook where everything registers late, or early, or before a named plugin, without repeating itself. Two details make it safe to use that way. The facade does not expose the call methods at all, so a plugin handed one cannot trigger the hook itself. And per-tap options still win, so a plugin that registers its own stage overrides the facade's. The documentation includes a runnable example that builds three facades from one hook, one for each end of the order, and calls the hook once to show the resulting sequence.
The promise tap throws when you return the wrong thing
There are three registration methods and the hook type decides which are legal. A plain tap registers a synchronous callback. A callback style tap takes a function whose last argument is a node-style error and result callback. A promise tap takes a function that returns something thenable, and if it returns anything else the hook throws. That last one is worth internalising, because a promise tap that forgets to return its promise fails loudly at call time rather than resolving to undefined and leaving you to find the missing value later. For synchronous hooks, the plain tap is the only valid method, so mixing the styles is a missing-method error rather than a rejected call. Every registration also needs a name, because that string is what identifies the tap in debugging output and in the profiling hooks other tools attach.
The benchmark runs with the optimiser switched off
The repository ships a benchmark and its run script is unusual enough to read closely. It pins the hash seed and the random seed, turns the optimiser off, fixes the garbage collection schedule, asks for interpreted frames and native stacks, allows native syntax in the flags, exposes the collector, disables concurrent sweeping, and caps the heap at four gigabytes. Every one of those is there to make two runs comparable. The cost is that the numbers are not what production would see, since the generated code is measured unoptimised and single threaded in its collection behaviour. There is a benchmarking service in the development dependencies as well, which suggests the same figures are tracked over time in continuous integration rather than only on a laptop.
The manifest remaps a Node built-in for browsers
Two fields in the manifest describe an audience that is not Node. The browser field maps the platform's own utility module to a file inside the library, so a bundler resolving that module for a browser target gets the shim instead of the built-in, which is how a library with Node-shaped internals gets into a browser bundle without the consumer configuring anything. And the published file list is two entries: the compiled library directory and one type declaration file, which sits at the repository root rather than beside the code. Two smaller things sit alongside. The repository address in the manifest is written over plain HTTP while the homepage field next to it is not, and funding is declared through a collective rather than in the readme.
The hook types section stops mid-sentence
The documentation on hook types begins by classifying them, and the first definition is the useful one: a basic hook is any hook whose name does not contain waterfall, bail or loop, and it simply calls every function it tapped in a row. Then the entry for the waterfall family begins and the visible text stops partway through the first word. What can be read is the shape of the design, though, because the export list at the top of the file enumerates nine classes whose names encode exactly those behaviours, five asynchronous and four synchronous. The release history adds a small note of its own: two of the three most recent releases were published on the same day, and the newest tag is from April while the default branch has moved since October.
Editorial conclusion
tapable is worth reading as an example of what a plugin interface costs, because it makes its trade-offs explicit: it generates code rather than dispatching through a loop, and it lets you order plugins by stage or by name. Two things to know before you depend on it. The ordering option that references another plugin by name ignores names it does not recognise, so a typo changes nothing and nothing complains, which is a bug you will find by observation rather than by error. And the hook type is fixed at construction, so a synchronous hook accepts only synchronous taps and the mistake shows up as a missing method rather than a rejected call. Check the published file list before you rely on it: the tarball carries the compiled library and one type declaration, with no source.
Frequently asked questions
What is tapable?
In this repository it is a small module exposing hook classes used to build plugin systems: synchronous, asynchronous and promise variants, with waterfall, bail and loop behaviours encoded in the class names. It is the plugin primitive webpack is built on, installs with one command, and its published tarball contains the compiled library and a single type declaration file.
How do I register a plugin in tapable?
With one of three methods on a hook, and which ones are legal depends on the hook type: a synchronous tap, a callback-style tap whose last argument is a node-style callback, or a promise tap, which throws if the function returns something that is not thenable. Every registration needs a name, which is also what other tools use for profiling and error messages.
How do I control plugin ordering in tapable?
A registration can take an options object with a name, a numeric stage that defaults to zero where lower runs earlier, and a before option taking one name or a list, which wins over the stage for the taps it names while other taps stay ordered by stage. Names in before that match nothing are ignored rather than rejected, so a typo changes nothing and reports nothing.
What is tapable's withOptions for?
It returns a facade around a hook whose registration methods merge the given options into every tap, so a library can hand plugins a hook that always registers at a particular stage or before a particular plugin. The facade does not expose the call methods, which is what makes it safe to pass to plugins, and options on an individual tap still override the facade's.
Why is the tapable library fast?
Because each hook compiles a method at run time, generating code specialised on how many plugins are registered, whether they are synchronous, callback or promise based, which call method is used, how many arguments there are, and whether interception is used. The repository's benchmark script pins seed values, disables the optimiser and fixes the collection schedule so the numbers measure the generated code rather than the just-in-time compiler.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/webpack-tapable)Star history chart for your README
[](https://hysenlabs.com/projects/webpack-tapable)Was this useful?