Remove console.logs with Webpack & Uglify

前端 未结 8 1430
谎友^
谎友^ 2020-11-28 23:59

I am trying to remove console.logs with Webpack\'s Uglify plugin but it seems that Uglify plugin that comes bundled with Webpack doesn\'t have that option, its

相关标签:
8条回答
  • 2020-11-29 00:04

    For uglifyjs-webpack-plugin, wrap options inside an uglifyOptions object:

        plugins: [
        new UglifyJSPlugin({
            uglifyOptions: {
                compress: {
                    drop_console: true
                }
            }
        })
    ]
    
    0 讨论(0)
  • 2020-11-29 00:07

    I have added a comprehensive answer for webpack v4 with debug configuration

    const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
    var debug = process.env.NODE_ENV !== "production";
    
    .....
    optimization: {
            minimizer: !debug ? [
                new UglifyJsPlugin({
    
                        // Compression specific options
                        uglifyOptions: {
                            // Eliminate comments
                            comments: false,
    
                            compress: {
                                // remove warnings
                                    warnings: false,
    
                                // Drop console statements
                                    drop_console: true
                            },
                        }
    
                    })
                ]
                : []
        }
    

    My scripts in package.json are like so:

    "webpackDev": "npm run clean && export NODE_ENV=development && npx webpack",
    "webpackProd": "npm run clean && export NODE_ENV=production && npx webpack -p"
    
    0 讨论(0)
  • 2020-11-29 00:10

    With UglifyJsPlugin we can handle comments, warnings, console logs but it will not be a good idea to remove all these in development mode. First check whether you are running webpack for prod env or dev env, if it is prod env then you can remove all these, like this:

    var debug = process.env.NODE_ENV !== "production";
    
    plugins: !debug ? [
       new webpack.optimize.UglifyJsPlugin({
    
         // Eliminate comments
            comments: false,
    
        // Compression specific options
           compress: {
             // remove warnings
                warnings: false,
    
             // Drop console statements
                drop_console: true
           },
        })
    ]
    : []
    

    Reference: https://github.com/mishoo/UglifyJS2#compressor-options

    UPDATE 2019 Need to use terser plugin now for ES6 support in webpack v4 https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions

    webpack.config.js

    module.exports = {
      optimization: {
        minimizer: [
          new TerserPlugin({
            sourceMap: true, // Must be set to true if using source-maps in production
            terserOptions: {
              compress: {
                drop_console: true,
              },
            },
          }),
        ],
      },
    };
    
    0 讨论(0)
  • 2020-11-29 00:12

    this is what I've done to remove alert() and console.log() from my codes. global_defs => replace alerts with console.log then drop_console removes all console.logs and now nothing shows up in my browser console

         new UglifyJsPlugin({
          uglifyOptions: {
            compress: {
              global_defs: {
                "@alert": "console.log",
              },
              drop_console: true
            }
          }
        }),
    

    plugin versions:

    "webpack":3.12.0,
    "webpack-cli": "^3.0.3",
    "uglifyjs-webpack-plugin": "^1.2.5",
    

    Right now uglifyjs-webpack-plugin v1.2.6 has been released and I used latest documentations for this one, So I suppose there wont be any problem with latest plugin too.

    0 讨论(0)
  • 2020-11-29 00:14

    Use this is better and works const UglifyEsPlugin = require('uglify-es-webpack-plugin')

    module.exports = {
    plugins: [
            new UglifyEsPlugin({
                compress:{
                    drop_console: true
                }
            }),
    ] 
    }
    
    0 讨论(0)
  • 2020-11-29 00:15

    This is the new syntax for Webpack v4:

    optimization: {
      minimizer: [
        new UglifyJSPlugin({
          uglifyOptions: {
            compress: {
              drop_console: true
            },
            output: {
              comments: false
            }
          },
        }),
      ],
    },
    
    0 讨论(0)
提交回复
热议问题