https://kotlinlang.org logo
Title
g

goku

01/31/2023, 9:54 PM
Hi team, is it possible to use Kotlin JS without
kotlin-stdlib
? I have a KMM project for JVM, iOS, and JS. And I need to reduce the size of the JS package.
t

turansky

01/31/2023, 10:21 PM
Do you mean concrete packages/classes?
g

goku

01/31/2023, 10:32 PM
Yes. stdlib adds a lot of overhead and increases the package size. The clients are avoiding to adopt it. I understand stdlib provides all the convenience of what makes Kotlin, but this is a small JSON serializer project and I’m wondering if I can do it in a way that is minimal.
t

turansky

01/31/2023, 10:40 PM
Do you use IR compiler?
g

goku

01/31/2023, 11:01 PM
Yes
a

Adam S

01/31/2023, 11:35 PM
if you just want to share TypeScript interfaces so clients can decode JSON, then you could consider using Kotlinx Serialization and kxs-ts-gen to generate TypeScript interfaces from Kotlin code
g

goku

01/31/2023, 11:58 PM
This would defeat the purpose of the library. I provide interfaces that takes a json string, performs some operation on them, and then returns back a json string. If there was a way to optimize what is included stdlib that would be super beneficial. There is a lot of functionality that gets exported but not used.
v

Vampire

02/01/2023, 12:19 AM
If you are using IR compiler and building for production target, afaik dead-code-elimination should be built-into the compiler and throw out the unused stuff, doesn't it?
a

Adam S

02/01/2023, 12:47 AM
another alternative: you could try excluding the stdlib completely and copy+paste only what you need from the stdlib https://kotlinlang.org/docs/gradle-configure-project.html#dependency-on-the-standard-library
kotlin.stdlib.default.dependency=false
g

goku

02/01/2023, 12:56 AM
You are right, upon further analyzing by removing/adding pieces I can see that stdlib only exports what’s being used. I think the only way to further optimize is to replace kotlinx Json with JSON.parser and avoid using certain types like Map.
j

jessewilson

02/13/2023, 9:38 AM
I’ve been down this road and it’s possible! You’ll need to avoid List and Map and RegEx, which gets pretty onerous
You’ll probably want to make a Gradle task to scan your generated JS files for uses of RegEx and List and fail your build if you accidentally re-introduce them
Turns out that calls to functions like String.split drag in RegEx
And that brings in List, which brings in all the collections and the game is lost
Also when I did this I ended up creating a custom StringMap and JsArray types that were backed by basic JavaScript objects on JS, and proper collections on other platforms
This really sucked
g

goku

02/13/2023, 9:46 AM
Thanks for the insights Jesse! I was gonna actually post an update on this. I did play around by removing/replacing code and reduced it to
227KB
, and
40KB
after compressing. I removed
kolinx.serialization,json
and used JSON.parser. I didn’t remove the List/Map/RegEx but at that point, we just decided to rewrite it in Javascript. Either way we have two different implementations. KMM would be really handy for an internal tool but for a product we ship where the size important, it made more sense to rewrite it in JS.