Compiling Kotlin to both Java and Javascript from the same module

Hi there!

I’ve trying out my idea of sharing some simple enums classes between our client code (Javascript) and server code (Java).

The project is setup using Maven3.

I’ve created a new “kotlin module” which both the server side java code and client side kotlin code depends on.
The shared kotlin module I’ve tried setup to compile to both Java and Javascript. Using maven I see that both “java” and javascript is compiled under the target directory.

However when compiling using IntelliJ then it fails with “uknown symbol” for the shared kotlin class.
It also seems difficult to compile one “kotlin module” to both java and javascript as the javascript compilation complains about ‘kotlin-stdlib’ is not a valid javascript library.

I did not want to mix both Java and Kotlin in one module because I just want certain classes to be shared between client and server.

Any ideas on how to solve these problems? Has anyone compiled one module successfully to both Java and Javascript?

Thanks,

Jørund

3 Likes

Right now this is unfortunately not supported. However hacky this may sound, the best approach for today is to symlink the same source directory into separate Java and JS modules with different Gradle builds.

Of course, we plan to provide much nicer support for that in a future release of Kotlin.

OK. Thanks for the quick answer. I will then wait and see what the future brings :slight_smile:

I guess in time passed we now have a solution. At least one I think good to use and not hackee for Gradle users.

Create 3 modules: shared, targeting JVM and targeting JS.

The shared module should not include any dependencies specific for JVM and/or JS.
Each of other modules should include in the build.gradle file the sourceSets parameter appending the source directory from the shared module.

For example check my simple project using this technic: GitHub - C06A/KUrlet: Kotlin support for URI Template (https://tools.ietf.org/html/rfc6570)

@cabman, how would you solve issue with Jackson annotations? Every POJO has it.