防止替换Terser中的ES6对象排列表示法


!((
input,
processed = {
foo: 1,
...input
}
) => {
window.console.log(processed)
})({
bar: 2  // input configuration
})

被缩小为:

((t, e = {
foo: 1,
bar: 2
}) => {
window.console.log(e);
})();

我需要input参数,以便稍后配置

问题:如何保持原始模式?

Terser输出我需要:

((t, e = {
foo: 1,
...t
}) => {
window.console.log(e);
})({bar: 2});

评论后更新:

let input1 = { bar:2 }
!((
input,
processed = {
foo: 1,
...input
}
) => {
window.console.log(processed)
})( input1 )

输出:

((t, e = {
foo: 1,
...t
}) => {
window.console.log(e);
})({
bar: 2
});

Terser将负责您的代码的当前版本。现在,您正在将一个常量参数传递给一个函数,所以简洁器可以直接内联它,从而防止创建中间对象。

如果将来在函数内部(对于基元值(或函数外部(对于对象(更改此参数,则terser应该认识到这一点,不再内联。

令人惊讶的是,正如OP:所发现的那样,已经将参数声明为变量似乎为简洁提供了适当的提示

let input1 = { bar:2 }
!((
input,
processed = {
foo: 1,
...input
}
) => {
window.console.log(processed)
})( input1 )

将导致

((t, e = {
foo: 1,
...t
}) => {
window.console.log(e);
})({
bar: 2
});

最新更新