Comments (8)
Tapable is small module that allows you to add and apply plugins to a javascript module. It can be inherited or mixed in to other modules.
Provides two kind of functions
plugin(name,handler)
- This function registers a plugin under a hash ofname
.apply*(...)
- The consumer can apply all the plugins under a particular hash using these functions.
The different apply functions cover the following use cases -
- Plugins can run serially
- Plugins can run in parallel
- Plugins can run one after the other but taking input from the previous plugin (waterfall)
- Plugins can run asynchronously
For eg - Webpack has something called a Compiler, which compiles all the webpack config and converts it into a compilation instance. When the compilation instance runs, it creates the required bundles.
Now Compiler needs to register and execute plugins on itself. It may do this in the following manner using Tapable
var Tapable = require("tapable");
function Compiler() {
Tapable.call(this);
}
Compiler.prototype = Object.create(Tapable.prototype);
Now to write a plugin on the compiler,
compiler.plugin('emit', pluginFunction);
The compiler executes the plugin at the appropriate point in its lifecycle by
this.apply*("emit",options) // will fetch all plugins under 'emit' name and run them.
from webpack.js.org.
Awesome start @pksjce !!!! Id love To see What other consumers
do webpack provide. Maybe even mentioning that this is a fundamental piece of webpacks functionality. Love what you have so far.
from webpack.js.org.
Thanks for the feedback! How about something like.
Tapable is fundamental to the working of webpack. This is because all the moving components of webpack like Compilation
, Compile
, Parser
use this to push all its side effects to plugins. Webpack is structured such that its internal modules read the configuration, and define a lifecycle. The lifecycle or state machine expects different behaviour from the plugin, some require aysnc runs, parallel runs or waterfall (like middleware) kind of runs. [this will need a diagram. I have something in mind, will update when done] One can register plugins at every checkpoint in the lifecycle. When the module component(ie like Compiler) changes its state, it invokes the registered plugins with appropriate state parameters through Tapable integration.
The whole of webpack runs as a set of state machines along with all logic pushed to internal Plugins. This framework allows webpack to be very flexible. One can achieve almost any transformation on the build files at any of the numerous lifecycle points by registering a plugin at the right lifecycle event.
from webpack.js.org.
One thing is missing -- the apply
method of the Tapable
class.
And the explanation is a little misleading:
Provides two kind of functions
plugin(name,handler)
- This function registers a plugin under a hash of name.apply*(...)
- The consumer can apply all the plugins under a particular hash using these functions.
@pksjce , i borrow some concepts from yours. Here is another version of my understanding:
Tapable
is a variant of EventEmitter
conceptually.
It doesn't have full implementation as the EventEmitter
in node.js. It just focuses on event emission manipulation.
Basically, it has four groups of member functions:
plugin(name:string, handler:function)
- This function registers a plugin under a hash of name.
This acts as the same ason()
ofEventEmitter
, for registering a handler/listener to do something when the signal/event happens.apply(...pluginInstances: (AnyPlugin|function)[])
-AnyPlugin
should be subclass of AbstractPlugin, or a class (or object, rare case) hasapply
method, or just a function with some registration code inside(also rare case, never seen, but works theoretically) .
This method is just to apply plugins' definition, so that the real event listeners can be registered into registry. Mostly the apply method of aAnyPlugin
is the main place to place extension logic.applyPlugins*(name:string, ...)
- The consumer can apply all the plugins under a particular hash using these functions.
These group of method act likeemit()
ofEventEmitter
, to control the event emission meticulously with various strategy for various use cases.mixin(pt: Object)
- a simple method to extend Tapable's prototype. Only for highly customization. Rarely used.
The different applyPlugins*
methods cover the following use cases:
- Plugins can run serially
- Plugins can run in parallel
- Plugins can run one after the other but taking input from the previous plugin (waterfall)
- Plugins can run asynchronously
- Quit runing th plugins on bail: that is once one plugin returns non-
undefined
, jump out of the run flow and return the return of that plugin. This sounds likeonce()
ofEventEmitter
but is totally different.
from webpack.js.org.
Could someone aggregate the ideas of this thread into a PR? Good to review there.
from webpack.js.org.
Wow it had been awhile since I had reviewed this content I'll start adding this to my pr this week.
from webpack.js.org.
Alright created a new set of documents #361 This should allow us once merged to tackle individual tapable instances etc.
from webpack.js.org.
It seems this is complete with the /content/api/plugins
pages which include a tapable page. If there's anything left it probably tackled with more targeted issues/prs. One thing we do need to do is make this section more visible (i.e. the only way to get to it now is from the /api/index.md
link but that can be addressed in #438, #1153 and #980.
from webpack.js.org.
Related Issues (20)
- Use composite action for webpack persistent caching
- Bug: Broken Link to CommonJS Specification HOT 3
- Documentation change for clarification in asset management HOT 1
- Document webpack change: feat: support for un-managed snapshot path
- Document webpack change: add node-module option for node.__file/dirname
- Document webpack change: feat: move css exportsOnly to css generator options
- Document webpack change: fix: `Server` and `Dirent` types HOT 1
- webpack breaking issue HOT 1
- splitChunks maxSize/minSize/maxRequest priority HOT 1
- Document webpack change: feat: add warning for asyncModule
- Why `optimization.removeAvailableModules` is default to `false`?
- Update webpack-dev-server documentation HOT 1
- Document webpack change: feat: support css local ident name HOT 1
- Document webpack change: fix: use LZW to compress css head meta
- Document webpack change: fix: resolve's plugins types
- Document webpack change: feat: allow to customize the stage of BannerPlugin
- meta[name=theme-color]' is not supported by Firefox, Firefox for Android.
- Document webpack change: feat(css): allow to use default and named export
- > It is already implemented, `webpack-cli init`, soon we will implement more templates HOT 1
- Document webpack change: feat: support `node:` prefix for Node.js core modules in runtime code
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from webpack.js.org.