Remove console.logs with Webpack & Uglify

前端 未结 8 1431
谎友^
谎友^ 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:17

    You can use terser-webpack-plugin compress option pure_funcs parameter to selectively drop console functions and keep the ones that you want such as console.error.

    I was using "webpack": "^4.39.3" and "terser-webpack-plugin": "^2.3.2".

    Steps:
    1. npm install terser-webpack-plugin --save-dev
    2. modify your webpack.config to set TerserPlugin as a minimizer for optimization option.

    const TerserPlugin = require('terser-webpack-plugin');
    
    module.exports = (env) => {
        return [{
            entry: '...',
            mode: 'production',
            output: {...},
            plugins: [...],
            optimization: {
                'minimize': true,
                minimizer: [new TerserPlugin({
                    terserOptions: { 
                        compress: { 
                            pure_funcs: [
                                'console.log', 
                                'console.info', 
                                'console.debug', 
                                'console.warn'
                            ] 
                        } 
                     }
                })],
            },
            module: {...}
        }];
    };
    
    0 讨论(0)
  • Try drop_console:

    plugins: [
        new Webpack.optimize.UglifyJsPlugin({
          compress: {
            drop_console: true,
          }
        }
    ]
    

    Update: For webpack v4 it has changed a little:

    const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
    
    ...
    
    optimization: {
      minimizer: [
        new UglifyJSPlugin({
          uglifyOptions: {
            compress: {
              drop_console: true,
            }
          }
        })
      ]
    }
    

    I personally think console logs (especially console.error) are very useful on prod and think it's a good idea to keep them. If you really want to drop specific console functions such as just console.log you should use pure_funcs option e.g. pure_funcs: ['console.log'] and this will drop console.log since pure functions do not produce side effects then Uglify can discard ones not assigned to anything.

    optimization: {
      minimizer: [
        new UglifyJSPlugin({
          uglifyOptions: {
            compress: {
              // Drop only console.logs but leave others
              pure_funcs: ['console.log'],
            },
            mangle: {
              // Note: I'm not certain this is needed.
              reserved: ['console.log']
            }
          }
        })
      ]
    }
    

    For TerserJS (Uglify is deprecated and does not support new JS features you should NOT use it)

    optimization: {
      minimizer: [
        new TerserPlugin({
          terserOptions: {
            compress: {
                drop_console: true
            }
          }
        })
      ]
    }
    

    As discussed you can also use pure_funcs alternatively.

    0 讨论(0)
提交回复
热议问题