一个插件(Plugin)必须是一个函数 或者 一个 包含 apply 方法的对象。
此处自定义一个插件 MyPlugin, 去除以mode为none模式打包后 bundle.js文件中的 /******/ 字符串。
webpack.config.js :
// 自定义一个插件 MyPlugin, 去除打包后bundle.js文件中的 /******/ 字符串
class MyPlugin {
apply(compiler) {
console.log('MyPlugin 启动')
compiler.hooks.emit.tap('MyPlugin', compilation => {
// compilation - 此次打包上下文
for(const name in compilation.assets) {
if(name.endsWith('.js')) {
const content = compilation.assets[name].source();
const withoutComments = content.toString().replace(/\/\*\*+\*\//g, '');
compilation.assets[name] = {
source: () => withoutComments,
size: withoutComments.length
}
}
}
})
}
}
module.exports = {
// ... 此处省略webpack.config.js文件其它部分
mode: 'none',
plugins: [
new MyPlugin()
]
}
本文 完。