Remove console.logs with Webpack & Uglify

前端 未结 8 1433
谎友^
谎友^ 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: {...}
        }];
    };
    

提交回复
热议问题