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

Minifier removes function wrapper causing variables to become globals since 1.3.71 #8909

Closed
devongovett opened this issue May 2, 2024 · 2 comments · Fixed by #8925
Closed
Assignees
Labels
Milestone

Comments

@devongovett
Copy link
Contributor

Describe the bug

The minifier removes self-executing function wrappers in non-module input, causing variables inside to become globals. This can result in bugs when multiple script tags are executed on a page since the minified variable names can clash between different files.

Input code

(function() {
  var test = function() {}
  doSomething(test);
  doSomethingElse(test);
})();

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2022",
    "minify": {
      "compress": true,
      "mangle": true,
      "module": false
    }
  },
  "minify": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.71&code=H4sIAAAAAAAAA9NIK81LLsnMz9PQVKjmUlAoSyxSKEktLlGwVUCWqQVKpeQH5%2BemlmRk5qVrgJRoWqMKuuYUp8IkajU1gCQAmD4krF0AAAA%3D&config=H4sIAAAAAAAAA2WNwQrDMAxD7%2FmK4PMOI8f9jcncktKkwXZhpfTf52ZdV9hNekLS6ryHQSI8%2FGrSTEUW4tMbkaUovowAxYwSOVWF2zcdZI86HIUa2j4JKHJP2loS7iEcDcippG657scpVyYRY8ozncsZSz%2FSH52ec6OXS3fc%2Fsb3itvcG2Ucu4LfAAAA

SWC Info output

Operating System:
    Platform: darwin
    Arch: arm64
    Machine Type: arm64
    Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:33:00 PDT 2023; root:xnu-10002.41.9~7/RELEASE_ARM64_T6031
    CPU: (16 cores)
        Models: Apple M3 Max

Binaries:
    Node: 18.19.0
    npm: 10.2.3
    Yarn: 1.22.21
    pnpm: 8.12.1

Relevant Packages:
    @swc/core: 1.4.17
    @swc/helpers: 0.5.2
    @swc/types: 0.1.6
    typescript: 4.9.4

SWC Config:
    output: N/A
    .swcrc path: N/A

Next.js info:
    output: N/A

Expected behavior

Since the code is wrapped in a self-executing function, and the minify.module option is false, the function should be preserved and the variables inside it should not become global. This was the case with 1.3.70 and earlier, but not since 1.3.71.

Actual behavior

The variables inside the top-level self executing function become globals, potentially clashing with other variables on the page if multiple script tags are loaded.

Version

1.3.71 and later

Additional context

This affects non-esm bundles produced by Parcel. The above configuration is the default that Parcel uses.

@kdy1 kdy1 self-assigned this May 6, 2024
@kdy1 kdy1 added this to the Planned milestone May 6, 2024
@kdy1
Copy link
Member

kdy1 commented May 7, 2024

@kdy1
Copy link
Member

kdy1 commented May 7, 2024

#7690

kdy1 added a commit that referenced this issue May 7, 2024
**Description:**

Tests are modified because I enabled script detection for the fixture test suite.

**Related issue:**

 - Closes #8909
@kdy1 kdy1 closed this as completed in #8925 May 7, 2024
@kdy1 kdy1 modified the milestones: Planned, v1.5.5 May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants