swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.21k stars 1.23k forks source link

SWC creates code resulting in an `Uncaught ReferenceError` with default minify/compress settings #7194

Closed danieldiekmeier closed 1 year ago

danieldiekmeier commented 1 year ago

Describe the bug

Hello! We're trying to use SWC as an alternative to Terser, but in a specific scenario, it produces invalid code.

The input code below is the result of compiling this TypeScript code, which defines an enum, with Babel:

enum MyEnum {
  First = 'first',
  Second = 'second',
}

Babel Playground →

This seems to be due to the combination of

Input code

function example() {
  var MyEnum = /*#__PURE__*/function (MyEnum) {
    MyEnum["First"] = "first";
    MyEnum["Second"] = "second";
    return MyEnum;
  }(MyEnum || {});

  return MyEnum
}

example()

// Output Code:

(function() {
    var MyEnum1;
    return (MyEnum1 = MyEnum || {})["First"] = "first", MyEnum1["Second"] = "second", MyEnum1;
    //                ^^^^^^ This variable is never defined, 
    //                       which leads to an Uncaught ReferenceError
})();

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es5",
    "loose": false,
    "minify": {
      "compress": {
        "collapse_vars": true,
        "unused": true,

        "arguments": false,
        "arrows": false,
        "booleans": false,
        "booleans_as_integers": false,
        "comparisons": false,
        "computed_props": false,
        "conditionals": false,
        "dead_code": false,
        "directives": false,
        "drop_console": false,
        "drop_debugger": false,
        "evaluate": false,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": false,
        "hoist_vars": false,
        "if_return": false,
        "join_vars": false,
        "keep_classnames": false,
        "keep_fargs": false,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": false,
        "negate_iife": false,
        "properties": false,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": false,
        "switches": false,
        "typeofs": false,
        "unsafe": false,
        "unsafe_arrows": false,
        "unsafe_comps": false,
        "unsafe_Function": false,
        "unsafe_math": false,
        "unsafe_symbols": false,
        "unsafe_methods": false,
        "unsafe_proto": false,
        "unsafe_regexp": false,
        "unsafe_undefined": false,
        "const_to_let": false,
        "pristine_globals": false
      },
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.3.44&code=H4sIAAAAAAAAA0srzUsuyczPU0itSMwtyEnV0FSo5lJQKEssUvCtdM0rzVWwVdDXUo6PDwgNco2P19JPg2nQgMhD1CtAVUcruWUWFZcoxQK1KaWBmdYo0sGpyfl5KRD5YggboqAotaS0KA%2BqDiRUC7VAoaZGobpW05qLC00RVy0XF9zVXACM67WcyQAAAA%3D%3D&config=H4sIAAAAAAAAA32UPXLjMAyFe5%2FCo3rbTZEDpNszcGgSlOkVCQ0BOtZkfPdAf46TQOokfA%2BC%2BAjg43A8NhdyzevxQx7lpbeFoDzeJUJDZnuTSAMuWXIl9tz8WemFRhRsRzCF7jNp2JYWeMqiv4u86RAJVvkSSzHHMDwXdJj6AkRPMYnK92qCzPQ9f2EF3zVwQuzA5j1kLJmYGVoomsxh19mewFztxLnU7ziJY5FQrTHSyuBNX7DXBdlHjpglrmAP1huHHjQWCziOV1ATpZ4kZpIzbmEPp9q2013%2F5HC1XbWspcJtuhv5ZQWeMRKbUFUzZrjlxEwXk3%2FCGEwBrkWrecGYt%2FL%2BA4gNnSXKNqlGTYogvbUNd1NjDtK%2FPChcml09aYZWrDUxBs3f0R8oHNWSBXx1MBrsdvCGGRQ9GAhBukbF75HdWS3LQw8YNCI3bdVjzMBsDubCxwHZwW9yUNZbbVEky%2BdtSkM6oTpZazrwGf2OQK6DcRsXWRu3fpvX7EEaBLwqqTSBXyslyyQwmm7an78bRAZFPmnaDk9Pa2NR3B%2BbOdncfk3%2FvJwPi6BJ6OsEl7U%2FXvG8rF%2BaL9G6mh%2F%2F0ET6t2aOv324fwIfWlHuQgYAAA%3D%3D

Expected behavior

I'd expect this to produce code along these lines:

(function() {
    var MyEnum;
    //  ^^^^^^ Nice, the variable is defined!
    var MyEnum1 = ((MyEnum = MyEnum1 || {})["First"] = "first", MyEnum["Second"] = "second", MyEnum);
    return MyEnum1;
})();

(Which it does, if I remove the __PURE__ comment or disable either one of the collapse_vars or unused options. (Although the code does of course look slightly different then.))

Actual behavior

No response

Version

1.3.44

Additional context

No response

swc-bot commented 1 year ago

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.