Remove console.logs with Webpack & Uglify

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

提交回复
热议问题