swc-project / swc

Rust-based platform for the Web

Home Page:https://swc.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Minifier removes function wrapper causing variables to become globals since 1.3.71

devongovett opened this issue · comments

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.

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.