webpack中complier用法

tapable

tapbale暴露了很多钩子,可以为webpack插件创建时使用

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const {SyncHook,SyncBailHook,AsyncParallelHook,AsyncSeriesHook} = require('tapable')

class Lesson {
constructor(){
// 初始化hooks容器
// 数组中的参数为绑定钩子回调函数的字段描述
this.hooks = {
// 同步hooks,任务依次被执行
go:new SyncHook(['address']),
// 同步hooks,一旦其中一个有返回值,后面的hooks就停止执行
go1:new SyncBailHook(['address']),
// 异步钩子,并行执行
go2:new AsyncParallelHook(['name','age']),
// 异步钩子,并行串行
go3:new AsyncSeriesHook(['name','age']),
}
}
// 添加事件,可以同时绑定多个事件
tap(){
this.hooks.go1.tap('class',(address)=>{
console.log('class',address)
})
this.hooks.go1.tap('class1',(address)=>{
console.log('class1',address)
})

// 异步钩子的两种写法
this.hooks.go2.tapAsync('class3',(name,age,cb)=>{
setTimeout(() => {
console.log('class3',name,age);
cb()
},2000);
})

this.hooks.go2.tapPromise('class4',(name,age)=>{
return new Promise((resolve,reject)=>{
setTimeout(() => {
console.log('class4',name,age);
},1000);
})
})
}
//触发hooks
start(){
this.hooks.go1.call('触发时间时候传入的参数')
this.hooks.go2.callAsync('Gavin',18)
}
}

const l = new Lesson();
l.tap();
l.start();

Compiler

Compiler 模块是 webpack 的主要引擎,它通过 CLI 传递的所有选项, 或者 Node API,创建出一个 compilation 实例。 它扩展(extend)自 Tapable 类,用来注册和调用插件。 大多数面向用户的插件会首先在 Compiler 上注册。

在插件中使用不同的生命周期钩子来处理资源

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
class Plugin1 {
apply(compiler) {
compiler.hooks.initialize.tap('initialize',()=>{
console.log('编译器对象初始化')
})

compiler.hooks.emit.tapAsync(
'emit',
(compilation, callback) => {
setTimeout(() => {
console.log('出发emit事件');
callback();
}, 2000);
}
);
compiler.hooks.afterEmit.tapPromise(
'afterEmit',
(compilation, callback) => {
return new Promise((resolve,reject)=>{
setTimeout(() => {
console.log('出发afterEmit事件')
}, 1000);
})
}
);
}
}

module.exports = Plugin1;
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2025 SunZhiqi

此时无声胜有声!

支付宝
微信