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

The --keep-names option still removes the name from classes #1437

Closed
yoshikiohshima opened this issue Jul 9, 2021 · 1 comment
Closed

The --keep-names option still removes the name from classes #1437

yoshikiohshima opened this issue Jul 9, 2021 · 1 comment

Comments

@yoshikiohshima
Copy link

yoshikiohshima commented Jul 9, 2021

I have a project where classes need to retain their names. The --keep-names works almost, but not quite for my purposes.

If I have code that looks like:

export class TestClass {
    say() {
        console.log("hello");
    }
}

It is translated to:

var TestClass = class {
  init() {
    console.log("hello");
  }
};
__name(TestClass, "TestClass");
...

where __name calls defineProperty. (With options --bundle --keep-names --format=esm)

While this works (i.e. returns "TestClass") when thatClass.name is accessed, if thatClass.toString() is called and then result is eval()'ed , the resulting class object is anonymous as toString() does not insert the name after the class keyword.

It'd be great if the result produced with --keep-names is:

var TestClass = class TestClass {
  init() {
    console.log("hello");
  }
};

, or with the __name() line added.

Otherwise I really like this project!

@evanw
Copy link
Owner

evanw commented Oct 15, 2021

Using toString() on functions and classes with esbuild is not supported: https://esbuild.github.io/content-types/#function-tostring. This is problematic in more cases than just --keep-names so changing how that feature is implemented is still a brittle solution and may randomly break in the future, since this is still an unsupported use case and esbuild uses helper functions for other things too. If you still want to use esbuild, you should instead build the source code in a separate build step and then insert it as a string into your code that needs the string.

Closing since this is an unsupported use case.

@evanw evanw closed this as completed Oct 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants