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

Stefan Beyer

03/26/2020, 8:56 AM
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?
b

Big Chungus

03/26/2020, 9:01 AM
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
🤯 1
Something similar how @Test annotations work in common source set
s

Stefan Beyer

03/26/2020, 9:03 AM
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 🙂
b

Big Chungus

03/26/2020, 9:03 AM
e.g.:
Copy code
//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
i

Ilya Goncharov [JB]

03/26/2020, 9:11 AM
cc @bashor
s

Stefan Beyer

03/26/2020, 9:23 AM
pulling in JsExport via expect/actual worked! I only needed a minor adjustment:
Copy code
//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:
👍 2
r

russhwolf

03/26/2020, 12:05 PM
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)
🙌🏼 1
👍 1
👏 3
b

Big Chungus

03/26/2020, 12:09 PM
@russhwolf since when this is a thing? 😮 Very convenient!
r

russhwolf

03/26/2020, 12:18 PM
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

03/26/2020, 12:47 PM
For the orignal issue you can vote or star this issue to be notfied about updates https://youtrack.jetbrains.com/issue/KT-35966
👍 1