Search code examples
gradlekotlinktorkotlin-multiplatform

Serve kotlin multiplatform javascript from ktor


I have set up a multi-module gradle project with a common module and a ktor module.

The common module is a kotlin multiplatform library.

I would like to be able to serve the javascript files generated from the common library from the ktor server, when I run it from Intellij.

Currently ktor is set up to serve resources as static content:

    static("/static") {
        resources()
    }

The ktor module has a dependency on the common module:

dependencies {
    compile project(':common')
    ...
}

I would assume when running in Intellij to be able to browse to http://localhost:8080/static/common.js to retrieve the outputs of the common module JS build (written to common\build\classes\kotlin\js\main) but this does not work.


Solution

  • Fixed this by including a copy task. Note that the kotlin full stack mpp here (https://github.com/ktorio/ktor-samples/tree/master/mpp/fullstack-mpp) has an example that uses webpack outputs.

    kotlin {
        jvm() {
            task copyJsToJvm(type: Copy) {
                from("$buildDir/classes/kotlin/js/main")
                include '*.*'
                into "$buildDir/classes/kotlin/jvm/main"
            }
            compilations.main {
                tasks.getByName(processResourcesTaskName) {
                    dependsOn(copyJsToJvm)
                }
            }
        }
    ...
    }