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

オープン
#1,973 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
javascript

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
JavaScript
スター
3.8k
フォーク
635
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rollup/plugins のほかの issue

rollup/plugins の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。