Webpack原理

2020.11

Tapable

Tapable中文文档
Tapable是一个用于事件发布订阅执行的插件架构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 公开的方法
void apply(plugins: Plugin...)
void plugin(names: string|string[], handler: Function)

// 受保护的方法
void applyPlugins(name: string, args: any...)
any applyPluginsWaterfall(name: string, init: any, args: any...)
void applyPluginsAsync(
name: string,
args: any...,
callback: (err?: Error) -> void
)
any applyPluginsBailResult(name: string, args: any...)
applyPluginsAsyncWaterfall(
name: string,
init: any,
callback: (err: Error, result: any) -> void
)
applyPluginsAsyncSeries(
name: string,
args: any...,
callback: (err: Error, result: any) -> void
)
applyPluginsParallel(
name: string,
args: any...,
callback: (err?: Error) -> void
)
applyPluginsParallelBailResult(
name: string,
args: any...,
callback: (err: Error, result: any) -> void
)

Webpack 核心模块 tapable 解析(转)
Webpack 本质上是一种事件流的机制,它的工作流程就是将各个插件串联起来,而实现这一切的核心就是 tapable,Webpack 中最核心的,负责编译的 Compiler 和负责创建 bundles 的 Compilation 都是 tapable 构造函数的实例。

1
2
3
4
5
6
7
8
9
10
11
12
13
// 引入 tapable 如下
const {
SyncHook,
SyncBailHook,
SyncWaterfallHook,
SyncLoopHook,
AsyncParallelHook,
AsyncParallelBailHook,
AsyncSeriesHook,
AsyncSeriesBailHook,
AsyncSeriesWaterfallHook
} = require("tapable");
复制代码

上面的实现事件流机制的 “钩子” 大方向可以分为两个类别,“同步” 和 “异步”,“异步” 又分为两个类别,“并行” 和 “串行”,而 “同步” 的钩子都是串行的。

knowledge is no pay,reward is kindness
0%