Is there a way to have javascript code generated w...
# javascript
a
Is there a way to have javascript code generated with normal function names without using @JsName? I have a large project and in most all cases I just want the javascript function name to be the same as kotlin one.
t
IR
or
legacy
?
a
legacy, although open to IR if that is better. Adding IR generates an almost empty .js file so I'd need to fix that too.
t
In
IR
if export instance you must mark it with
@JsExport
. Exported instances have original names (like in Kotlin)
Adding IR generates an almost empty .js file so I’d need to fix that too.
@JsExport
fix this problem
You can check IR export here
a
that worked... I need to add @JsExport to every class then?
t
For those, which names you want to save
AFAIK in
legacy
original names - default behaviour
a
legacy should have original names? That is what im using now and got this for example. prototype.square_za3lpa$ = function (n)
Just trying on a small simple project, but I have a larger one with about 100 classes so was trying to avoid needing to add annotations to everything
t
Do you need to save names for public classes only?
a
yes
t
Custom compiler plugin can solve this problem too :)
a
How can I use that?
s
Names are mangled, primarily, because JS doesn’t have function overloads, which are very frequent in Kotlin. If you need to use Kotlin code from JS, then you would want to use @JsExport explicitly, because it adds some restrictions, like prohibiting overloads, to make sure that everything looks nice from JS side.
a
okay good to know, would be nice if it was non-mangled names by default then mangle overloads out of necessity. Will experiment a bit with the JsExport, it sounds like you just add that to the class then fix things it complains about, if there are any?
s
… would be nice if it was non-mangled names by default then mangle overloads out of necessity.
At the first glance it would be nice, but it would make naming unstable, e.g. when you add a function overload (possibly in other module), some function that was previously non-mangled suddenly becomes mangled. In new compilation scheme we hope that JS users of Kotlin library would not be exposed to mangled names because only @JsExport-marked declarations will be visible outside.
… it sounds like you just add that to the class then fix things it complains about, if there are any?
Pretty much it. This feature is currently under development, expect some bumps on the road.