Under the hood webpack: core library behind the event-driven architecture

bogdanlyashenko

Bohdan Liashenko

Posted on July 29, 2020

Under the hood webpack: core library behind the event-driven architecture

Read full interactive version of this post on my blog.

Webpack architecture is heavily based on events. Each webpack plugin is basically a set of listeners hooked on different events during compilation phases. Under the hood, webpack uses a library called tapable to encapsulate "publish-subscribe" implementation.

Tapable provides different "hooks" classes (SyncBailHook, AsyncParallelHook, etc.) to "hook" on events with some extra rich functionality (e.g. interceptions or cross-listeners integration).

For example, DefinePugin (used to define environment variables, e.g. NODE_ENV) and SizeLimitsPlugin (reports oversized chunks, e.g. size > 250kb) tap into compiler instance hooks: the first one listens to compilation event in order to insert extra variables and the latter to afterEmit event - to proceed with assets evaluation once they were emitted.
Alt Text

Let's have a quick look under the hood of webpack at SizeLimitsPlugin integration.
Alt Text

1) SizeLimitsPlugin is instantiated and assigned to compiler in WebpackOptionsApply if performance option is enabled from webpack config.
2) Then SizeLimitsPlugin taps on afterEmit event and will sit still until most of the compilation flow is done and that particular event is called.
Alt Text

Once the event is called the plugin will do its job (collecting oversized chunks in our case 2, 3).

Indeed, all plugins are assigned similarly, so then you have a pipeline of plugins performing different operations but staying very loosely coupled.

Read full interactive version of this post on my blog.

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
bogdanlyashenko
Bohdan Liashenko

Posted on July 29, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About