1、terser-webpack-plugin
webpack 構建的項目中安裝使用
安裝:
npm install terser-webpack-plugin --save-dev
配置
在webpack.config.js文件中
new TerserPlugin({terserOptions: {output: {comments: false, // 去除注釋},warnings: false, // 去除黃色警告,compress: {drop_console: true,drop_debugger: true, // 特定情況需要利用debugger防止調試pure_funcs: ['console.log'], // 移除console.log 避免console.error},},
}),
2、
babel-plugin-transform-remove-console
安裝
npm install babel-plugin-transform-remove-console --save-dev
在babel.config.js文件中加入配置
module.exports = {plugins: ['transform-remove-console',],
};
如果只想在生產環境中使用,可以改成:
const prodPlugins = [];
if (process.en.NODE_ENV === 'production') {prodPlugins.push('transform-remove-console');
}
module.exports = {plugins: [...prodPlugins],
};