我使用的是unijs3;我想在webpack的chainWebpack中,等到打包完成抛出哪个钩子函数啊
时间: 2023-06-11 15:08:32 浏览: 55
你可以在webpack的`afterEmit`钩子函数中等待打包完成后抛出事件。具体实现可以参考以下代码:
```javascript
// webpack.config.js
module.exports = {
// ...省略其他配置...
configureWebpack: {
plugins: [
// ...省略其他插件...
new CustomPlugin(),
],
},
chainWebpack: (config) => {
config
.plugin('customPlugin')
.use('CustomPlugin')
.tap((options) => {
options.afterEmitHook = () => {
// 等待打包完成后抛出事件
process.nextTick(() => {
options.emit('afterEmitComplete');
});
};
return options;
});
},
};
// CustomPlugin.js
class CustomPlugin {
constructor() {
this.afterEmitHook = null;
}
apply(compiler) {
compiler.hooks.afterEmit.tap('CustomPlugin', () => {
if (this.afterEmitHook) {
this.afterEmitHook();
}
});
}
}
module.exports = CustomPlugin;
```
这里我们定义了一个`CustomPlugin`插件,通过`afterEmitHook`属性设置等待打包完成后的回调函数。在`chainWebpack`中将该插件注入webpack配置中,并在`afterEmit`钩子中执行`afterEmitHook`回调函数。最后,在自定义事件中监听`afterEmitComplete`事件即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)