Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
danieldiekmeier opened this issue Apr 3, 2023 · 1 comment · Fixed by #7200
Assignees
Labels
Milestone

Comments

@danieldiekmeier
Copy link

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

  • The __PURE__ magic comment
  • The collapse_vars and unused options for compress. (When they both are true, the error happens.)

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

@kdy1 kdy1 added this to the Planned milestone Apr 3, 2023
@kdy1 kdy1 self-assigned this Apr 4, 2023
@kdy1 kdy1 closed this as completed in #7200 Apr 5, 2023
kdy1 added a commit that referenced this issue Apr 5, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.47 Apr 10, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented May 10, 2023

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.

@swc-project swc-project locked as resolved and limited conversation to collaborators May 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants