8000 Minifier produces suboptimal output if 2 or more properties of a constant object are referenced · Issue #10630 · swc-project/swc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Minifier produces suboptimal output if 2 or more properties of a constant object are referenced #10630
Open
@dmichon-msft

Description

@dmichon-msft

Describe the bug

If the constants object contains exactly one property, it gets inlined. However, as soon as there are two or more properties, references are preserved as property accesses, and the property accesses are assumed to have side effects, such that code that only reads the values has the reads left in.

Input code

var constants = {
    first: 1,
    second: 2
}
export function isConstant(x) {
    return x === constants.first || x === constants.second;
}
var y = constants.second;

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2020",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": true,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": true,
        "comparisons": true,
        "computed_props": true,
        "conditionals": true,
        "dead_code": true,
        "directives": true,
        "drop_console": false,
        "drop_debugger": true,
        "evaluate": true,
        "expression": true,
        "hoist_funs": false,
        "hoist_props": true,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": true,
        "keep_classnames": false,
        "keep_fargs": true,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": true,
        "negate_iife": true,
        "properties": true,
        "reduce_funcs": true,
        "reduce_vars": true,
        "side_effects": true,
        "switches": true,
        "typeofs": true,
        "unsafe": true,
        "unsafe_arrows": true,
        "unsafe_comps": true,
        "unsafe_Function": true,
        "unsafe_math": true,
        "unsafe_symbols": true,
        "unsafe_methods": true,
        "unsafe_proto": true,
        "unsafe_regexp": true,
        "unsafe_undefined": true,
        "unused": true,
        "const_to_let": true,
        "pristine_globals": true,
        "passes": 0
      },
      "mangle": {
        "toplevel": false,
        "keep_classnames": false,
        "keep_fnames": false,
        "keep_private_props": false,
        "ie8": false,
        "safari10": false
      }
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.12.1&code=H4sIAAAAAAAAAytLLFJIzs8rLknMKylWsFWo5uVSAIK0zKLiEisFQx0ItzgVqCbFSsGIl6uWlyu1oiC%2FqEQhrTQvuSQzP08hs9gZaoJGhSbMhKLUktKiPIUKBVtbW4QNemCDFWpqMCQgVliDLSgDOqpSAYssAN9YjT%2BuAAAA&config=H4sIAAAAAAAAA42VzW7bMAyA73mKwOcesh6GoQ%2Bw255BUCzKUSZLhki5MYq8e2nFztyGCnYJYn6i%2BE997Pb75oxt87b%2F4L%2F8MeiEkO7fLMEpkL6wpIG219gmN1DzstIzzshqj1BE1xtpSKcOqGjh6%2BH1sGg0PkaEVWOR9S44O21ttrEfEiBuZCzlK3MPgWYxpQwvW5TiuyA%2FxuhBhydEaVQuEHSQ8Ktfiyfe6wFBjToJt8yO6uQwSiZmmAmMGlIcRB6MIxcD23ykBrRRbTQgIJegJTeCpMa2WC0ghyfEU7CBY%2B66UuZv2jBqnzUJNuFSKsLePrJTdEjK5iBl8AYrKbjBJbffNZ1VCSgnweI5ulApyV8AToDXiEH3IN1bTljuppq2farpguWGpUng3N1SlAE6zqlyzgqJnTMDiZxUzAQmtzBntq1TOQ3oDCiwlhtFou%2BO2pNkkqYBohUAV1dL%2Ft%2FkqjaBC55noU5%2Fc4AkttZyoNd0qkKc%2BmOURmhVBjpFU%2BdcAYpVmng1XIYqzsEA9wMY6URGST5PJymKypcN%2BdAPPBJ8oep8PIqbYeDeLqU7LLLrfR33OnRl7jdrk%2BLgYQRf6%2Bf%2FmJWnlB0e5%2FZeR%2FxhjOGXIOXk8eL8cfjyenAou%2FW3BNX00eRNQKU9b6%2FKz%2BbfofUBuRtpHP5ZNefs7a6fz%2FEX0esGAAA%3D

SWC Info output

No response

Expected behavior

export function isConstant(n) {
    return n === 1 || n === 2;
}

constant values should be inlined, and the last line should be discarded entirely has having no side effects.

Actual behavior

var constants = {
    first: 1,
    second: 2
};
export function isConstant(n) {
    return n === constants.first || n === constants.second;
}
constants.second;

The constants object remained, and the last line is assuming that reading constants.second has side effects.

Version

1.12.1

Additional context

Happened upon this while examining the compressed output of react-device-detect to figure out why SWC produced significantly larger output than Terser.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0