Controlling UglifyJS through Webpack
UglifyJS 的 mangling 功能,預設是開啟的,這個功能主要是讓 local function 和 variable 的 names 做最小化處理,通常會縮減到一個字母。透過設定,它也可以讓你把 properties 改寫成更簡潔的模式。
不過使用上還是要注意,不然你的程式碼很可能就壞掉了。
除了 mangling ,UglifyJS 還有其他功能可以設定:
new webpack.optimize.UglifyJsPlugin({
// Don't beautify output (enable for neater output)
beautify: false,
// Eliminate comments
comments: false,
// Compression specific options
compress: {
warnings: false,
// Drop `console` statements
drop_console: true
},
// Mangling specific options
mangle: {
// Don't mangle $
except: ['$'],
// Don't care about IE8
screw_ie8 : true,
// Don't mangle function names
keep_fnames: true
}
})
如果啟用了 mangling,最好設定 except: ['webpackJsonp'],避免在 Webpack runtime 進行 mangling。
移除
console(Dropping the console statements)也可以透過 Babel 的 plugin:babel-plugin-remove-console。 關於 Babel 會在稍後的 Configuring React 章節討論到。另一種最小化處理的方式可以透過 uglify-loader