class MyJsMinimizerPlugin {
apply(compiler) {
compiler.hooks.compilation.tap(MyJsMinimizerPlugin.name, compilation => {
// Access the chunkHash hooks of JavascriptModulesPlugin
const hooks =
compiler.webpack.javascript.JavascriptModulesPlugin.getCompilationHooks(
compilation,
);
// Since the JS chunk has been optimized and its content has changed, the chunk hash for the JS chunk needs to be updated
hooks.chunkHash.tap(MyJsMinimizerPlugin.name, (chunk, hash) => {
hash.update(`minimized by ${MyJsMinimizerPlugin.name}`);
});
// Optimize the JS chunk
compilation.hooks.processAssets.tap(MyJsMinimizerPlugin.name, assets => {
optimize(assets);
});
});
}
}