删除资产预编译上的console.log是一种优化代码的方法,可以减少代码体积,提高程序运行效率。在前端开发中,console.log() 是一个常用的调试工具,但在生产环境中,这些调试信息可能会泄露敏感信息,或者增加代码体积,影响程序运行速度。因此,在生产环境中,我们需要删除这些console.log() 语句。
以下是一些常用的方法:
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
optimization: {
minimizer: [
new UglifyJsPlugin({
uglifyOptions: {
compress: {
drop_console: true
}
}
})
]
}
};
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: true
}
}
})
]
}
};
{
"plugins": [
["transform-remove-console", {
"exclude": ["error", "warn"]
}]
]
}
这样,在生产环境中,所有的console.log() 语句都会被删除,提高程序运行效率。同时,也可以避免敏感信息的泄露。
原引擎
云原生正发声
原引擎 | 场景实战系列
"中小企业”在线学堂
云+社区技术沙龙 [第30期]
原引擎 | 场景实战系列
“中小企业”在线学堂
领取专属 10元无门槛券
手把手带您无忧上云