压缩代码

使用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)}))};

更多的api参考:https://terser.nodejs.cn/docs/api-reference/

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部