方法
1. 安装babel-plugin-transform-remove-console
修改 babel.config.js 文件
推荐
let transformRemoveConsolePlugin = []
if (process.env.NODE_ENV === 'production') {
transformRemoveConsolePlugin = ['transform-remove-console']
}
module.exports = {
plugins: [
...transformRemoveConsolePlugin
]
}
2.安装terser-webpack-plugin
修改 babel.config.js 文件
const TerserPlugin = require('terser-webpack-plugin')
module.exports = {
configureWebpack: config => {
config
.optimization = {
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: true
}
}
})
]
}
}
}
-魔搜
生产环境去除console.log及代码压缩

本文介绍如何在生产环境中使用Babel和Webpack插件去除console.log日志,并通过TerserWebpackPlugin进行代码压缩,以减少应用体积,提高加载速度。
3564

被折叠的 条评论
为什么被折叠?



