手写 plugin
javascript
class firstPlugin {
constructor(options) {
this.options = options
}
apply(compiler) {
compiler.plugin('emit', (compilation, callback) => {
let str = ''
for (let filename in compilation.assets) {
str += `文件:${filename} 大小${compilation.assets[filename]['size']()}\n`
}
// 通过compilation.assets可以获取打包后静态资源信息,同样也可以写入资源
compilation.assets['fileSize.md'] = {
source: function () {
return str
},
size: function () {
return str.length
}
}
callback()
})
}
}
module.exports = firstPlugin
使用
javascript
const path = require('path')
const firstPlugin = require('webpack-firstPlugin.js')
module.exports = {
// 省略其他代码
plugins: [new firstPlugin()]
}