I'd like to write some of my code in Kotlin such that it can be used by both a JVM-based backend and a JS-based frontend.
To do the former (with the new IR complier), the classes have to be annotated with @JsExport. However, if I then try to use build the same file for a JVM, it breaks because that annotation is not recognized.
(These are separate projects with independent gradle configs but linking to the same Kotlin source tree. It's not a single "multiplatform" build.)
How can I export Kotlin/JS while still being compatible with Kotlin/Java?
To achieve that seperation and reusability, you need three gradle submodules that would depend on each other
Your gradle project structure should look something like this
shared/build.gradle.ktsshould look like thisand then
backend/build.gradle.ktsand then
backend/build.gradle.ktsand then
settings.gradle.ktsWith this sort of arrangement, You can write your shared code inside
shared/src/commonMain/kotlinand environment specific code on their respectivebackendorfrontendsubmodulesNOTE: The gradle configs above have been minimised to narrow down the explanation