Webpack 在打包的时候,会使用 Terser Plugin 进行 JavaScript 代码压缩,它内部使用了 Terser 这个库。
Terser
Node API 使用方法
const { minify } = require("terser");
let code = "function add(first, second) { return first + second }";
let result = await minify(code, { sourceMap: true });
minify options 的结构如下:
{
parse: {
// parse options
},
compress: {
// compress options
},
mangle: {
// mangle options
properties: {
// mangle property options
}
},
format: {
// format options (can also use `output` for backwards compatibility)
},
sourceMap: {
// source map options
},
ecma: 5, // specify one of: 5, 2015, 2016, etc.
// ...
}
compress options 有哪些呢?
- drop_console 干掉 console
- drop_debugger