【问题标题】:prevent replacing ES6 Object spread notation in Terser防止在 Terser 中替换 ES6 对象扩展符号
【发布时间】:2020-06-24 07:59:44
【问题描述】:
!((
    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 应该认识到这一点并且不再内联。 (请记住,扩展运算符无论如何都会进行浅拷贝。因此需要在嵌套对象中发生更改以反映在 inputprocessed 参数中。)
  • Tnx,这是指向答案的指针;我在所有更简洁的配置选项中都进行了错误而漫长的搜寻。将我的补充复制粘贴到答案中,我可以将其标记为已回答。

标签: ecmascript-6 spread-syntax terser


【解决方案1】:

Terser 会处理您的代码的当前版本。现在,您将一个常量参数传递给一个函数,因此 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
});

【讨论】:

    猜你喜欢
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    相关资源
    最近更新 更多