The terser-webpack-plugin is a webpack plugin that uses Terser to minify JavaScript files. Terser is a JavaScript utility that helps reduce the size of JavaScript bundles by removing unnecessary whitespace, comments, and other non-essential code, as well as by performing advanced optimizations and transformations.
The following code snippet uses terser-webpack-plugin as the minimizer in the webpack configuration and specifies custom options to remove console statements from the JavaScript code.
Add or update the optimization object in webpack.config.js
.
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
...
optimization: {
minimizer: [
new TerserPlugin({
// Your custom options here
terserOptions: {
compress: {
drop_console: true,
},
},
}),
],
},
};
This custom option instructs Terser to drop all console statements (such as console.log
, console.warn
, console.error, etc.) during the compression phase and remove them from the minified JavaScript code.