Stefan Beyer

    Stefan Beyer

    2 years ago
    Hi 🙂 I tried 1.4-M1 with the new js ir backend this week and I realized, that the
    @JsExport
    annotation is throwning a wrench into my project. Maybe you folks have an idea on how to do this correctly? My goal is to have a Kotlin common sorce set, which should be compiled to js and jvm. This will be a common library, that will be used by the frontend (React with TypeScript) and the backend (Spring Boot with Kotlin) and will contain common data structures, validation logic and some particularly complex bits of business logic that need to be executed on both platforms. Now my train of thought was to just implement all of this in the commonMain source set and leave the jsMain and jvmMain source sets empty. This works fine for the jvm target: I get a .jar file with all the classes and functions defined in the common code. But for js, everything that should be exported needs to be annotated with
    @JsExport
    for it to actually being compiled to js. Does this mean I have to populate the jsMain source set with a wrapper for each class and function that exists in commonMain, just to be able to compile this to js? This does not sound very pragmatic. is there maybe a way to tell the compiler to always export classes and functions from a certain package?
    Big Chungus

    Big Chungus

    2 years ago
    just add @JsExport on your entities in commonSourceSet
    If there's no such annotation, just bring it in with
    expect annotation class JsExport
    and typealias it on js sourceSet
    Something similar how @Test annotations work in common source set
    Stefan Beyer

    Stefan Beyer

    2 years ago
    yes, I tried to use the annotation in the common module, but it is not available there. but the expect way of doing it may work. I am going to try this right away. thanks for the suggestion 🙂
    Big Chungus

    Big Chungus

    2 years ago
    e.g.:
    //Common
    expect annotation class JsExport
    
    //JVM
    actual annotation class JsExport
    
    //JS
    actual typealias JsExport = kotlin.js.JsExport
    it probably shuld be part of stdlib-common, though
    Ilya Goncharov [JB]

    Ilya Goncharov [JB]

    2 years ago
    cc @bashor
    Stefan Beyer

    Stefan Beyer

    2 years ago
    pulling in JsExport via expect/actual worked! I only needed a minor adjustment:
    //Common
    expect annotation class Export()
    //JVM
    actual annotation class Export
    //JS
    actual typealias Export = JsExport
    without the explicit empty constructor, there is an error at commonMain use sites:
    russhwolf

    russhwolf

    2 years ago
    Can also add
    @OptionalExpectation
    to the declaration in common and then you don't need anything in jvm (or any other platform you add later)
    Big Chungus

    Big Chungus

    2 years ago
    @russhwolf since when this is a thing? 😮 Very convenient!
    russhwolf

    russhwolf

    2 years ago
    It's been around a long time. 1.2.60 according to a quick check through old blog posts https://blog.jetbrains.com/kotlin/2018/08/kotlin-1-2-60-is-out/
    Only for annotations though which is why you may not have encountered it
    b

    bashor

    2 years ago
    For the orignal issue you can vote or star this issue to be notfied about updates https://youtrack.jetbrains.com/issue/KT-35966