使用terser来做这件事,对标的库还有swc
压缩代码一般包含:
- 注释去除
- 多余字符串去除
- 压缩变量名
- 合并声明
- 预编译
源代码:
//这是注释
function sayhello() {
console.log("hello world");
}
const a = 1;
const b = 2;
const fun = () => {
setTimeout(() => {
console.log(a + b);
})
}
npx terser index.js -o output.js –compress –mangle toplevel
压缩后代码:
function o(){console.log("hello world")}const l=1,e=2,n=()=>{setTimeout((()=>{console.log(3)}))};