Hi. Is it possible somehow to get rid of those str...
# javascript
a
Hi. Is it possible somehow to get rid of those strings with original class names in the final js production library distribution? Do we have any compiler option to say that we would like to mangle class names or something?
Copy code
initMetadataForClass(AuthTokenResponse, 'AuthTokenResponse');
I really don't understand how to say, for example, terser plugin to mangle/obfuscate those strings. After additional mangling code with terser I got this result, but the strings are still visible.
Copy code
Oi(Yu,"AuthTokenResponse")
We’re worried that using the original names makes reverse engineering significantly easier.
e
Not sure if something like that exists as a Webpack plugin, but in case I'll just use a custom plugin to replace problematic strings.
t
DefinePlugin
can be used for example
a
Will look at DefinePlugin, thanks. Guys, maybe you know of some Kotlin IR compiler plugin that can obfuscate class/method names before target compilation (in my case it's js)? It seems like it can be done with a compiler plugin, so I'm wondering if it exists.
e
I would always delegate the obfuscation part to a JS bundler/minifier. Not to the Kotlin pipeline.
t
@Arthur Krukowski Do you use
whole-program
granularity?
a
No, we don't use that option in our project.
t
It seems like it can be done with a compiler plugin
I set special names via plugin for lazy loading
👀 1
a
But the way just adding JsName annotation will not rename those strings too.
Copy code
@JsName("MyCustomClassName")
class AuthTokenProvider {...}

// It will change this:
initMetadataForClass(AuthTokenResponse, 'AuthTokenResponse');
// to this:
initMetadataForClass(MyCustomClassName, 'AuthTokenProvider');
Anyway, thank you for information. I'll try to analyze it to find the better solution for us.
e
You would have to investigate where the metadata is useful in the outputted code. https://github.com/JetBrains/kotlin/blob/0825797d33743aa948384c3fb92ef058f098a1d1/libraries/stdlib/js/runtime/metadataUtils.kt#L40 For example, the name you see there is used to retrieve
KClass::simpleName
So be careful in changing those values.
👌 1
b
Correct, those string are used in reflection. While the Kotlin compiler certainly could do a much better job of eliminating the
initMetadataForClass
calls for classes that don't participate in reflection (and we're planning to tackle this), for now the only solution I can suggest is post-processing the generated JS files.
gratitude thank you 1
o
@broadway_lamb is it possible to modify class names with IR plugin?
b
Yes, it's possible. However, I'm not aware of such plugins for Kotlin Multiplatform, so you'll have to write it yourself.
1