Compiling for both JVM & JS


#1

One of Kotlin's most attractive qualities, for me, is to be able to write code that I can use on the server side in my JVM setup, on the Android client side, and also on the web client side as JS.  My big use cases are model classes, and code (calculations etc.) that works on them.

Is it possible to achieve this?  IntellIj seems to want me to create either a Kotlin JS or Kotlin JVM module.  First that means I can’t just mix in Kotlin code with my existing files, as I can do with Groovy, as I have to create the module in a new directory.  And second I can’t generate both JVM and JS code from the same files.  Unless I go symlinks - is that the recommended route?

I also tried using the Kotlin compiler from the command line through File Watchers, but hit issues with Windows not supporting the extremely long command line resulting from the CLASSPATH, which I have to include as the model classes have annotations from JPA and JSR-349 validations.  Also, the approach feels a bit hacky, wouldn’t handle dependencies well, and I’d probably end up using a Makefile to sit alongside IntelliJ.  I’d rather have a true IntelliJ solution if possible.


#2

I should have had a proper search before... I see from https://devnet.jetbrains.com/message/5529436#5529436 and https://youtrack.jetbrains.com/issue/KT-6359 that I am not alone in wanting this.  I think that many people are looking for ways to avoid duplicating code in different languages all the time between server and client, and that this feature could be huge in driving uptake of the language.  It's what brought me to Kotlin in the first place, but until support for this is working, I'll need to look at alternatives like http://www.scala-js.org/ and Ceylon.


#3

You actually can simply build your project to both JS and JVM targets. You just need to run both compiler goals for JVM and JS. You can see example here: https://github.com/cy6erGn0m/html4k/blob/master/shared/pom.xml


#4

Hi Sergey, thank you for the reply.  I'm currently evaluating Scala-JS as an alternative, but if I find myself back on Kotlin I will give that a go.