Skip to content

Instantly share code, notes, and snippets.

@rupeshtiwari
Last active September 21, 2021 12:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rupeshtiwari/4bc44953d00670a3980b5e13113d159d to your computer and use it in GitHub Desktop.
Save rupeshtiwari/4bc44953d00670a3980b5e13113d159d to your computer and use it in GitHub Desktop.
remove console.log & comments from code webpack

Remove Console Log From Prod Bundle using Webpack

This will help you to remove console.log and comments from typescript or javascript files using webpack 4

Install uglifyjs-webpack-plugin

webpack remove console.log

npm i npm install uglifyjs-webpack-plugin --save-dev

create bs-module-app\webpack\webpack-optimization.config.js

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const optimization = {
  minimizer: [
    new UglifyJsPlugin({
      uglifyOptions: {
        output: {
          // removing comments
          comments: false,
        },
        compress: {
          // remove warnings
          warnings: false,
          // remove console.logs
          drop_console: true,
        },
      },
    }),
  ],
};

module.exports = optimization;

Go to webpack.config.js

Add optimization here.

const optimization = require('../webpack-optimization.config');

module.exports = {
  ...
  optimization
}
@TokaLazy
Copy link

npm i uglifyjs-webpack-plugin -D instead npm i npm install uglifyjs-webpack-plugin --save-dev

@carlosman
Copy link

I think the "npm i" and "npm install" parts are redundant. If I am not mistaken, it should read as follows:
npm install uglifyjs-webpack-plugin --save-dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment