Shared Code (JVM and JS)


#1

In GWT I used to have 3 folders for modules:

client/ (aka compile-to-js)
server/ (aka jvm)
shared/

The shared thing was one of GWT’s key selling points. As of June 2017, what is the best way to achieve this using IntelliJ?


#2

At this moment it still is either:

  • Copying the code from one module to another.
  • Creating a symlink in one module to the code in the other module.

Multi-platform support is in development: 1 module with the common code, and 1 module per platform with the platform-specific implementations and extensions. It works for the most part, but IntelliJ IDEA support is lagging: Lots of errors in the editor, and building and running have to be delegated to Gradle.

Code that is exactly the same on all platforms can simply be copied, but you need to use new constructs for functions and types that have platform-specific implementations.

I created a test project to figure out how to create multi-platform modules. It provides examples of what you can do at this moment: https://github.com/jstuyts/kotlin-multiplatform-recipes/


#3

This will force to build to update shared code in some modules which is not so nice in dev since code is not always compilable.

Idea actually dereferences symlinks to some extent. I had to create symlinks in non root to project folder to make idea think it is different sources. Also sometimes ide prefers data from *.class files and you have to rebuild.

Either way this all are IDE problems, with gradle build made as dependencies for java part and srcDirs+= for js part everything works ok, no symlinks or source copy required.

I was told reason of all this glitches is IDEA in its core architecture does not support single source to be included in multiple modules/projects. Guess objectiveC/C/C++ and multiple other languages supported by jetbrains IDEs use another one.


#4

Note that Scala / ScalaJS does cross builds using SBT and IntelliJ supports it without problems.
The same methodology should be possible to apply in Gradle as well?


#5

I am no expert, but I think the Gradle model does not support it. This question has been asked multiple times. JetBrains has been working on multi-platform builds for a while now, so I assume that it requires a number of changes in Gradle and/or IntelliJ IDEA for it to work properly.

Note that with the current multi-platform modules a module that must be available on the JVM and in JavaScript, must be set up as 3 separate Gradle projects: common, JVM and JavaScript.


#6

It is good to know that a solution might come soon. Once you get a taste of shared code it is hard going back.