Open zxdfe opened 1 year ago
module.exports = function(input) { const callback = this.async(); // webpack5内置了loaderUtils.getOption,用this.query就能接收参数 console.log(this.query) const result = source.replace('webpack5', this.query.name) // 同步用this.callback 返回 this.callback(null, result) callback(null, input + input); };
Plugin 本质上是一个类,或者说构造函数,基于tapable库来控制钩子函数的发布和订阅。
扩展插件,webpack运行的各个阶段,都会广播出对应的事件,插件去监听对应的事件。
插件名称就是类名
plugin必须要有一个apply方法,接收compiler对象,在compiler对象的hooks(生命周期)上绑定要处理的逻辑
module.exports = class MyPlugin { constructor(options) { this.options = options; } apply(compiler) { compiler.hooks.done.tap('MyPlugin', (compilation, callback) => { ... callback() }) } };
Loader
Plugin
Plugin 本质上是一个类,或者说构造函数,基于tapable库来控制钩子函数的发布和订阅。
扩展插件,webpack运行的各个阶段,都会广播出对应的事件,插件去监听对应的事件。
插件名称就是类名
plugin必须要有一个apply方法,接收compiler对象,在compiler对象的hooks(生命周期)上绑定要处理的逻辑