https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
s

Sean Najera

07/08/2020, 10:25 PM
I just created a kotlin multiplatform library for android, ios and Javascript which uses the Ktor client for all networking. These are my dependencies for the javascript library
Copy code
// MPP - JS & common dependencies

    sourceSets["commonMain"].dependencies {
        implementation(kotlin("stdlib-common", Versions.KOTLIN))
        implementation(Deps.Ktor.COMMON_CORE)
        implementation(Deps.Ktor.COMMON_JSON)
        implementation(Deps.Coroutines.COMMON)
        implementation(Deps.MP_SETTINGS)
        implementation(Deps.Ktor.COMMON_SERIALIZER)
        implementation(Deps.Serialization.COMMON)
        implementation(Deps.Stately.COMMON)
        implementation(Deps.Stately.CONCURRENCY)
    }    

    sourceSets["jsMain"].dependencies {
        implementation((kotlin("stdlib-js", Versions.KOTLIN)))
        implementation(Deps.Ktor.JS_CORE)
        implementation(Deps.Ktor.JS_JSON)
        implementation(Deps.Coroutines.JS)
        implementation(Deps.Ktor.JS_SERIALIZER)
        implementation(Deps.Serialization.JS)
    }
My goal is to write the repository layer as a library for the three platforms which can use it to request proprietary data, similar to Firebase Realtime database. But when I import the JS library into the Jetbrains starter React app my javascript binary is 27MiB for dev and 1MiB for prod distributions. Should I expect ktor & coroutines JS libraries to be that heavy for a JS library? Or am I doing something wrong?
r

Robert Jaros

07/08/2020, 10:33 PM
You can check your
build/js/packages/[project]/kotlin-dce
directory - there you will find all kotlin js files used by your project (already processed by DCE). You can compare sizes of different libs.
s

Sean Najera

07/08/2020, 10:33 PM
thanks again
r

Robert Jaros

07/08/2020, 10:34 PM
Those files are used by webpack, combined with npm JS dependencies, bundled and once again minified with Terser plugin. Your ~1MB is the result.
Never used ktor client for JS, but for my projects e.g. coroutines-core.js is larger than the whole kotlin.js (stdlib).
s

Sean Najera

07/08/2020, 10:38 PM
That’s unfortunate. It would be really nice to use coroutines as a unified architecture for asynchronous programming in common code. I guess I might just have to figure out a way to abstract it away for JS
r

Robert Jaros

07/08/2020, 10:42 PM
My largest Kotlin/JS project is 3,2MB bundle size. And it's usable 😉 Fortunately after gzip it's only ~600KB 😉
j

Jurriaan Mous

07/09/2020, 6:24 AM
Kotlin 1.4 is said to dramatically decrease JS bundle size. You could try the M3 release.
s

Sean Najera

07/09/2020, 3:14 PM
Unfortunately, I have deps that are not 1.4 M3 compatible. But good to know, thank you
j

Jurriaan Mous

07/09/2020, 3:52 PM
This image was shared at the 1.4 keynote to get an impression how big the improvement is.
❤️ 1
🙏 1
r

Robert Jaros

07/09/2020, 4:20 PM
From my tests the improvement is not as big as shown on slides (I'v got only about 30% reduction of the bundle size). It probably very much depends on your libraries.