Remove console.logs with Webpack & Uglify
Remove console.logs with Webpack & Uglify
回答1
With UglifyJsPlugin
we can handle comments, warnings, console logs but it will not be a good idea to remove all these in development mode. First check whether you are running webpack
for prod env or dev env
, if it is prod env
then you can remove all these, like this:
var debug = process.env.NODE_ENV !== "production";
plugins: !debug ? [
new webpack.optimize.UglifyJsPlugin({
// Eliminate comments
comments: false,
// Compression specific options
compress: {
// remove warnings
warnings: false,
// Drop console statements
drop_console: true
},
})
]
: []
Reference: https://github.com/mishoo/UglifyJS2#compressor-options
UPDATE 2019 Need to use terser plugin now for ES6 support in webpack v4 https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
webpack.config.js
module.exports = {
optimization: {
minimizer: [
new TerserPlugin({
sourceMap: true, // Must be set to true if using source-maps in production
terserOptions: {
compress: {
drop_console: true,
},
},
}),
],
},
};
Try drop_console:
plugins: [
new Webpack.optimize.UglifyJsPlugin({
compress: {
drop_console: true,
}
}
]
Update: For webpack v4 it has changed a little:
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
...
optimization: {
minimizer: [
new UglifyJSPlugin({
uglifyOptions: {
compress: {
drop_console: true,
}
}
})
]
}
I personally think console logs (especially console.error
) are very useful on prod and think it's a good idea to keep them. If you really want to drop specific console functions such as just console.log
you should use pure_funcs
option e.g. pure_funcs: ['console.log']
and this will drop console.log
since pure functions do not produce side effects then Uglify can discard ones not assigned to anything.
optimization: {
minimizer: [
new UglifyJSPlugin({
uglifyOptions: {
compress: {
// Drop only console.logs but leave others
pure_funcs: ['console.log'],
},
mangle: {
// Note: I'm not certain this is needed.
reserved: ['console.log']
}
}
})
]
}
For TerserJS (Uglify is deprecated and does not support new JS features you should NOT use it)
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: true
}
}
})
]
}
As discussed you can also use pure_funcs
alternatively.
webpack 系列四:优化包体积
用户通过浏览器访问网页(JS、CSS资源),当文件体积越大时,网页的加载时间就越长,使用的流量就越大,用户的体验就越糟糕,所以我们需要对代码进行压缩。
目前比较成熟的压缩工具有两种:
- UglifyJsPlugin:通过封装 UglifyJS 实现压缩
- ParallelUglifyPlugin:多进程并行处理压缩
他们都会分析 JavaScript 代码语法树,理解代码含义,从而能做到诸如去掉无效代码、去掉日志输出代码、缩短变量名等优化,但相对于 UglifyJsPlugin 是单线程, ParallelUglifyPlugin 插件实现了多线程压缩,ParallelUglifyPlugin 会开启多个子进程,把对多个文件的压缩工作分配给多个子进程去完成,每个子进程其实还是通过 UglifyJS 去压缩代码,但是变成了并行执行。 所以 ParallelUglifyPlugin 能更快的完成对多个文件的压缩工作。
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-12-23 onedrive忽略子文件夹
2019-12-23 git filter-repo
2015-12-23 Hosting Your Own NuGet Feeds
2015-12-23 NuGet学习笔记
2015-12-23 软件版本的处理