Minification removes class identifiers causing observable runtime change in Class.name

Open
#1,973 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript

Research direction

Start with the linked StackBlitz reproduction and inspect dist/main.js to confirm how the named class is transformed and how its name is observed at runtime. Review @rollup/plugin-terser's handling of class names and the documented keep_classnames workaround; done means the production output no longer causes the reported runtime difference, with regression coverage for class and constructor names.

Written by the indexing model from the issue text.

Description

Description

When using @rollup/plugin-terser, named class declarations are transformed into anonymous class expressions during minification. This causes clazz.name and this.constructor.name to return an empty string ("") instead of the original class name. This results in observable runtime differences between development and production builds.

Behavior

Terser removes the class identifier and converts:

class MyClass {}

into:

class {}

This creates an anonymous class expression.

According to the ECMAScript specification:

ClassDeclaration → has a name
Anonymous ClassExpression → name === ""

Therefore:

clazz.name === ""
this.constructor.name === ""

Why This Is Problematic

This is not just cosmetic minification.

Class.name and constructor.name are frequently used for:

  • logging
  • debugging
  • dependency injection
  • metadata systems
  • serialization
  • runtime introspection

This creates:

  • different runtime behavior in dev vs prod
  • silent semantic changes
  • hard-to-debug production issues

Important Note

I understand that Terser does not guarantee preservation of Function.name.
However, this transformation introduces observable semantic differences between builds, not just identifier mangling.

Workaround

Setting:

terser({ keep_classnames: true })

preserves the expected behavior.

Dominant language
JavaScript
Stars
3.8k
Forks
635
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rollup/plugins

All issues in rollup/plugins

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.