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

minify wrongly transform inner scope variable to global scope variable #6628

Closed
hardfist opened this issue Dec 12, 2022 · 8 comments · Fixed by #6659
Closed

minify wrongly transform inner scope variable to global scope variable #6628

hardfist opened this issue Dec 12, 2022 · 8 comments · Fixed by #6659
Assignees
Labels
Milestone

Comments

@hardfist
Copy link

Describe the bug

enable copress
image
disable compress
image

Input code

The code is too large to paste here, I will try to make make minimal demo asap

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es5",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "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": false,
        "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": false,
        "reduce_vars": false,
        "side_effects": true,
        "switches": true,
        "typeofs": true,
        "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,
        "unused": true,
        "const_to_let": true,
        "pristine_globals": true
      },
      "mangle": false
    },
    "externalHelpers": true,
    "transform": {
      "react": {
        "refresh": {}
      }
    }
  },
  "module": {
    "type": "commonjs"
  },
  "minify": true,
  "isModule": true
}

Playground link

No response

Expected behavior

should generate a var declaration instead of use global variable

Actual behavior

generate a global variable

Version

1.3.22

Additional context

when I disable compress it works correctly

@kdy1
Copy link
Member

kdy1 commented Dec 13, 2022

I think this is fixed by #6478.
IIFEs are not invoked anymore if there's eval
Can you try the main branch?

@hardfist
Copy link
Author

it not works for our case yet and it's not related to eval, we will try to make a minimal demo without eval.

@IWANABETHATGUY
Copy link
Contributor

Here is another demo:
https://play.swc.rs/?version=1.3.22&code=H4sIAAAAAAAAA31TTW%2FbMAy9%2B1eoxg4SahjNNVkPw87DiqHYJQgCJWVSb6pkyFK3YvV%2F36M%2FFKUpqoM%2FSL73SIqUh2j3oXFWSCX%2BFd93v2gf6gc6NJbuvGvJhxdJf1vnQ1eJcrul7pt7iIbKCuEC51mbSEsRfKSiV6siEW5HmAzaHylUQhujJszBefmsvbD6iURjB9f70jOYI2dFPmTjE3m9M5N0lTwIXzLhmiGbwcxp9cWcT6pmZIOgjgaYOXGpMh1PIXrUMkWtRr5R7aszBik7%2FzE2hX1aTPChT1x%2FconbSwoOoMwhSBJ8%2FUgykVMdO7oz8YguZhzoWIOm5dk0ByGb3JBfhAP4alGJDu%2BbSnA%2BVLcDbVcbssfwuILzs%2FB4XV%2Br1RmLE6%2BvWXwbu0d5rsOH72OJtC4cIw73eOlyLZfTLYU9c%2FVZAr0g09HH8kmheMvQVyckil5v8I9hkfMFYZB2ev%2F7%2FqWF%2BSr1l%2BZG0prqn5r7dbPBo8Q3NoOtX4yBYTFY8V0ya07HPTs33OJy1SxM7Q9s2XtzQdg4zm9IMk3QaQ7khMW67qktJ8J7PqCz9Oc0drKc5rpUVT6niEt%2FI3xeAHgGJsgrqf4DQEM32D4EAAA%3D&config=H4sIAAAAAAAAA21TQZLjIAz8i8%2B57mUesLWXfQNFQDhkAbkkkYkrlb%2BPSGwcJ%2BsTNC1ZarVuw5nd8HUbJksM1E48F7HX4WsAly07ipMMB6UpFGxiuB8GsTSCNAr%2F0reEyLC8HoYcSwxzy%2BQwTwTM7awRNUMR7jxLhN96Fap6OyImsOXjbiybWARGoC3UYUp2YjAXSz2i%2Fc1SZCw7qAp4MxFOL2jxUSIWTbZiHqw3Dj10IBI4iRfYKJpDKYW1sF7JA%2FRwrOPY1Hsy4WJTtdJzwfUhg%2F6xx50wsphQC79Bu0qf0LPJhRWDIZBKZeWcMZadDv8AtM5kmYvNsEU%2B8KBz2DPDf1ixBB2izB3VCW9VFRi1ORNj6B22qoEkbmoR%2BOqgdei25Au4a4ijBwMhqNo9mL%2BjuNOWTOYJMPSrqmbDNoPn1ax%2B2qPNAh%2Fgb61KXsex4NnK6R3jOR8xfaTIICf0H7AKIfgOkrr3Or2jtXhQmcG%2FPFRu125THb6gSW3VVp3VEBpjxoTHbl%2FdyGzL2G15b5YTIDX4H0gTbN4Q0pUKSLmtJIF18jwE9ad2frvrp8nQ15bs9hBe11xFzFjOPNy39V79yH8X9qOSH1xfKklQBAAA

image

The semantic of the code after minify is not the same as before, you could see.
e has been writen twice, the second time assignment cause e.plugins is undefined, which will lead the runtime error
image

@kdy1
Copy link
Member

kdy1 commented Dec 14, 2022

Oh we should abort more in case of eval

@kdy1
Copy link
Member

kdy1 commented Dec 14, 2022

Ah. We have to go up to the scope with any variable which may interact with eval.
But I think aborting IIFE invoker completely on eval is fine

@hardfist
Copy link
Author

It did fix my project, Thanks

@kdy1 kdy1 modified the milestones: Planned, 1.3.24 Dec 21, 2022
@swc-bot
Copy link
Collaborator

swc-bot commented Jan 20, 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 Jan 20, 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.

4 participants