compiler文档:compiler 钩子
compilation文档:Compilation Object
manifest-webpack-plugin.js
// Webpack每次打包结束后,自动产生一个打包文件清单,文件上记录文件名,文件数量等信息。module.exports = class ManifestWebpackPlugin {// constructor(options) {// console.log(options);// }apply(compiler) {// 在assets输出前,添加manifest清单compiler.hooks.emit.tapAsync("ManifestWebpackPlugin", (compilation, cb) => {const names = compilation.getAssets().map(item => item.name)const arr = [];arr.push(`文件的数量:${names.length}`)names.forEach(name => {arr.push(` ${name}`)})// 添加manifest清单const result = arr.join('\n')compilation.emitAsset('manifest.json', {source: function () {return result},size: function () {return result.length;},});cb();});}}
使用,
const manifestWebpackPlugin = require("./myPlugins/manifest-webpack-plugin");module.exports = {// ...plugins: [new manifestWebpackPlugin(),],// ...}
