Search code examples
javakotlingradlekotlin-multiplatformkotlin-js

Gradle kotlin js module depending on multiplatform module


I have a multiplatform kotlin project for jvm/js. I'd like to extract some common code that's not compile target specific to reuse in both applications.

Following is my project structure:

├── api
│   ├── build.gradle
│   └── src
├── server
│   ├── build.gradle
│   └── src 
├── client
│   ├── build.gradle
│   └── src 
├── build.gradle
├── gradle.properties
└── settings.gradle

api/build.gradle:

plugins {
    id 'org.jetbrains.kotlin.multiplatform'
}

group 'io.codegoblin'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

kotlin {
    jvm()
    js {
        useCommonJs()
        browser()
    }

    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
            }
        }
    }
}

client/build.gradle

plugins {
    id("org.jetbrains.kotlin.js")
}

repositories {
    mavenCentral()
    maven { url = "https://dl.bintray.com/kotlin/kotlin-eap" }
    maven { url = "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-js-wrappers" }
    maven { url = "https://kotlin.bintray.com/kotlin-js-wrappers/" }
}

kotlin {
    js {
        browser {
            commonWebpackConfig {
                cssSupport.enabled = true
            }
        }
        binaries.executable()
    }
}

dependencies {
    implementation project(":api")

    implementation("org.jetbrains:kotlin-react:17.0.2-pre.154-kotlin-$kotlin_version")
    implementation("org.jetbrains:kotlin-react-dom:17.0.2-pre.154-kotlin-$kotlin_version")
    implementation(npm("react", "$react_version"))
    implementation(npm("react-dom", "$react_version"))
}

tasks.register("stage") {
    dependsOn("build")
}

kotlin_version is 1.5.0

gradle is 7.1

With this setup project compiles the the api server and client modules with appropriate compile target, however the code from :api is not visible in the :client module nor it is present in the compiled :client js jar. I suppose I need to set up this dependency at the javascript level?

How do I make multiproject module dependency work in a javascript module?


Solution

  • Your client module should be multiplatform too, it can have only one platform (js), e.g.:

    1. add kotlin("multiplatform") plugin
    2. declare dependencies inside kotlin.sourceSets.jsMain