-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Invalid output for static private fields #1131
Comments
Thanks for the heads up. I can't reproduce this with the Side note, mostly for me: this is a known problem with TypeScript code. The TypeScript compiler itself also has this problem. I haven't fixed that yet since I have been waiting to see how the TypeScript team resolves this. The reason is due to the |
Oh, interesting — I’ll try again and see if I can figure out what other factor might be in play. (Aside: I was hoping I could just get everything to pass through as-is, at least in terms of syntax features esbuild understands, i.e. in this case my goal was only to bundle. Is it expected that esbuild would sometimes end up using the |
I don't think it's expected, no. The point of the One exception is when the input is TypeScript, since the TypeScript compiler still applies transforms in this case, so esbuild does as well. Although it sounds like that might be changing in the near future? At least according to microsoft/TypeScript#42663. But I don't want to make that change in esbuild yet because that would be a breaking change and TypeScript hasn't released that change yet. Another exception is when bundling is enabled. Some transforms are required that convert class statements to class expressions to be able to pull out the class variable into another scope in certain scenarios. There may be other exceptions too. I forget, sorry. It should be more clear if you can communicate how to reproduce your exact issue. |
Thanks for the explanation. I figured there might be special cases where a transform was either necessary or exceedingly complex to avoid, so wasn’t sure if “it’s transforming these members at all” would or would not be considered a bug. I’m able to repro the result for the source in the first post with this script: import esbuild from 'esbuild';
esbuild.build({
bundle: true,
entryPoints: [ 'in.mjs' ],
format: 'esm',
outfile: 'out.mjs',
target: 'esnext'
}); When I set
Esbuild as reported in package lock:
|
Thanks! Looks like it bundling is the key. That makes sense for the reason described above. I'll make sure that works too. That reminds me: another reason that top-level class declarations are transformed into expressions when bundling is that some JavaScript engines have severe performance issues when many class declarations are all put into the same scope (which happens during bundling): #478. |
Ah, it hadn’t clicked for me that this was particular to declarations within the (final) scope. That’s great to know because it appears I can avoid the transformation by putting the class declaration into a block scope or function body in the meantime. |
It appears references to already-initialized private static members can get lifted out of the lexical scope where their names exist.
Input
Output
The condition for this seems to be the presence of a reference to
Foo
(orthis
) in a static initializer (which may or may not be private). I think it’s the same set of scenarios where it’s altering the value ofFoo.name
.The text was updated successfully, but these errors were encountered: