I just created a kotlin multiplatform library for ...
# ktor
s
I just created a kotlin multiplatform library for android, ios and Javascript which uses the Ktor client for all three. 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)
    }


// Front-End react app dependencies
    implementation(kotlin("stdlib-js"))

    //React, React DOM + Wrappers (chapter 3)
    implementation("org.jetbrains:kotlin-react:16.13.0-pre.94-kotlin-1.3.70")
    implementation("org.jetbrains:kotlin-react-dom:16.13.0-pre.94-kotlin-1.3.70")
    implementation(npm("react", "16.13.1"))
    implementation(npm("react-dom", "16.13.1"))

    //Kotlin Styled (chapter 3)
    implementation("org.jetbrains:kotlin-styled:1.0.0-pre.94-kotlin-1.3.70")
    implementation(npm("styled-components"))
    implementation(npm("inline-style-prefixer"))

    //Video Player (chapter 7)
    implementation(npm("react-player"))

    //Share Buttons (chapter 7)
    implementation(npm("react-share"))

    // Shared Library: Nautilus
    implementation(project(":MarianaKit"))

    //Coroutines (chapter 8)
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.7")
When I import the javascript library into the Jetbrains starter React app, the dev distribution size 27MiB and the production distribution size is 1.5 MiB. Is anyone else having such large distribution sizes? Is this normal? Or am I doing something on my end to cause this?
j
wow, that's huge!
s
My goal was to use Ktor as the http client that makes requests for all three platforms, but the javascript distribution is so much larger than writing one in pure javascript.
j
i just write JS for the HTTP posts / gets, in ES6 it's almost a one-liner these days
👍 1
The serialization library from Kotlin is quite handy, but adding Ktor on the web seems like overkill, probably useful on Android / iOS
s
The goal was to write the common library with one architecture and have libraries for all three. Similar to firebase libraries. But I guess it wont be that straight forward.
j
the generated JS blows up very quickly as you're adding Kotlin code. I'm writing my frontends in TypeScript + lit-html (no other dependencies) and just adding the Kotlin models / state. State controls business logic, triggers UI repaint if values change (my own change detection) and the models are for serialization, this seems to be a good balance of Kotlin code re-use and generated file size To do a POST, i make use of Kotlin's
window.fetch
which is a replica of ES6's window.fetch whcih goes in the commonJS part of the project. You can always add a platform specific implementation for your POST / GET, then use Ktor for everything except for the JS side
💯 1
btw, which version of Kotlin are you on? i've found that 1.4 M3 generates significantly smaller JS output compared to 1.3.72
s
Yeah, my version is 1.3.72. I use libraries that are only compatible with Kotlin 1.3.* so I may have to wait a while, but I am looking forward to using Kotlin 1.4 and the new toolchain improvements to get the js target bundle size smaller.