Hi :slightly_smiling_face: I tried 1.4-M1 with th...
# multiplatform
s
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
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
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
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
cc @bashor
s
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
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
@russhwolf since when this is a thing? ๐Ÿ˜ฎ Very convenient!
r
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
For the orignal issue you can vote or star this issue to be notfied about updates https://youtrack.jetbrains.com/issue/KT-35966
๐Ÿ‘ 1