https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

apomelov

07/31/2018, 7:59 AM
Hi. Got stuck a bit with the terminology (common-platform-regular modules). I currently have two modules in my project: server and client (web). Server is written in kotlin and being built like a regular kotlin jvm application. Client is written in javascript and being build with webpack using node.js gradle plugin. Now I'd like to share some models and validation logic between client and server. I need a "common module" for this. I have no platform specific stuff there. So are my original modules platform or regular? What kind of dependencies should be there? Building a common module probably should produce some JS code to be used in client, but I can't see corresponding gradle tasks after applying platform-common plugin (only compile to JVM is available). What am I missing here?
Maybe I always need platform modules for each platform (even empty). Add expectedBy dependency to common module and then add compile dependencies from regular modules to platform ones.
I'm confused because common module produces JVM classes and I can depend directly on it from my regular module. So I'm expecting the same behavior for a JS counterpart
r

russhwolf

07/31/2018, 12:05 PM
Yes, you need platform modules, even if they have no code, so that Gradle knows how to produce the correct build artifact for the platform.
👍 1
a

apomelov

07/31/2018, 12:13 PM
Why can't I just add both kotlin-jvm and kotlin2js plugins to the common module, generate both artifacts (jar and js), add some "populateNodeModules" task for js and make a compile dependency from server to common? What does multiplatform approach gives to me?
i

ilya.gorbunov

07/31/2018, 2:00 PM
Mainly IDE and tooling support. You can't have the same source files analyzed in both JVM and JS contexts in IDEA.
😕 1
common module produces JVM classes
There's something wrong with the common module then, it shouldn't produce JVM classes. Have you applied
kotlin-platform-common
plugin there (and only that one)?
a

apomelov

08/01/2018, 5:23 AM
seems yes. let me double check it..
@ilya.gorbunov I have a module named "model-common". It doesn't have own build script, the top-level script says:
Copy code
allprojects {
    repositories {
        jcenter()
    }
    if (name.endsWith("-common")) {
        apply(plugin = "kotlin-platform-common")
        dependencies {
            "compile"("org.jetbrains.kotlin:kotlin-stdlib-common:1.2.51")
        }
    }
    ...
Hm... now everything is clear. Only metadata is being generated
As usual, you just need to complain and a problem goes away 😃
😕 1
@ilya.gorbunov please correct my understanding. When I'm developing multiplatform, can I expect from IDEA that I can ask for "go to definition" in JS code and get to Kotlin code in the common project?
i

ilya.gorbunov

08/01/2018, 11:10 AM
Yes, that how it should work
a

apomelov

08/07/2018, 1:23 PM
@ilya.gorbunov I'm trying to assemble a multiplatform project at least without javascript code. I have a model-common module with a data class, model-js and model-jvm with
expectedBy(project(":model-common")
. All modules have proper stdlibs and plugins. Also there are
server
and
client
depending on
model-jvm
and
model-js
respectively. Problem is kotlin code in
client
can not access data class from
common
. And this case is not covered in the kotlinconf-app example.
i

ilya.gorbunov

08/07/2018, 1:26 PM
This looks like a missing dependency problem. Can you show your build files or the entire project?
a

apomelov

08/07/2018, 1:32 PM
I was cleaning up the project to show it and find a problem with imports and default packages. Seems now import works. The only question for me -- what is the easiest way to manage dependencies between js modules (asked it in this channel a while ago)
Who should copy build artifact from
model-js
to somewhere so
client
can use it within a final webpack build? Should I do it manually or with kotlin-frontend-plugin? Can I mix JS an Kotlin code in
client
? And many more questions 😃