Skip to content

Minifier does not remove unused classes. #10425

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

Open
iclanton opened this issue Apr 30, 2025 · 6 comments
Open

Minifier does not remove unused classes. #10425

iclanton opened this issue Apr 30, 2025 · 6 comments
Assignees
Labels
Milestone

Comments

@iclanton
Copy link

Describe the bug

SWC's minifier does not remove unused classes in some cases. For example, a class with a static member that is the result of a function without side-effects.

Input code

export const foo = 6;

function baz() {
  return 5;
}

class Bar {
  static x = baz();
}

class Ban {
  static x = 5;
}

Config

{
  "jsc": {
    "minify": {
      "compress": true,
      "mangle": true,
      "module": true
    }
  }
}

Link to the code that reproduces this issue

https://github.com/iclanton/swc-minification-issue

SWC Info output

    Operating System:
        Platform: linux
        Arch: x64
        Machine Type: x86_64
        Version: #31~22.04.1-Ubuntu SMP Thu Mar 20 04:12:50 UTC 2025
        CPU: (32 cores)
            Models: AMD EPYC 7763 64-Core Processor

    Binaries:
        Node: 22.14.0
        npm: 10.9.2
        Yarn: 1.22.22
        pnpm: 10.6.3

    Relevant Packages:
        @swc/core: N/A
        @swc/helpers: N/A
        @swc/types: N/A
        

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

    Next.js info:
        output: N/A

Expected behavior

The Bar class is removed. This is the output from feeding the same code into Terser:

export const foo=6;

Actual behavior

The minification output of that input code is:

export const foo=6;class o{static x=5}

Note that feeding this output into SWC's minifier again, its output matches Terser's output.

Version

1.11.24

Additional context

No response

@iclanton iclanton added the C-bug label Apr 30, 2025
@kdy1 kdy1 added this to the Planned milestone Apr 30, 2025
@kdy1
Copy link
Member

kdy1 commented May 6, 2025

My guess is that DCE is applied before the optimizer, but DCE cannot remove unused classes with side effects in the initializer.

@dmichon-msft
Copy link

Isn't the passes property supposed to allow all of the layers to run multiple times? It's strange that that setting has no effect compared to running the minifier on its own output.

@kdy1
Copy link
Member

kdy1 commented May 8, 2025

Applying DCE again was useless for almost all inputs, so I decided to not run it again

@kdy1
Copy link
Member

kdy1 commented May 8, 2025

But I'm not sure if this case is worth an optimization code, to be honest.

@dmichon-msft
Copy link

When using swc to minify code in webpack, although most of the time we get better results (and it is a lot faster), we've encountered a number of chunks where a ton of dead code is left over after minification. This is because webpack leaves unused exports present but unexported and relies on the minifier to perform DCE.

@kdy1
Copy link
Member

kdy1 commented May 12, 2025

Ah. Then it makes sense to implement this rule.

@kdy1 kdy1 self-assigned this May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants