I’ve just started my from-the-sketch project in IDEA. I picked the Kotlin > JS Client and JVM Server | Gradle wizard and created my project skeleton.
It generates a “Hello world” client/server app files into the project so I tried it out by:
gradlew.bat -t run
It build and starts. It tells:
2019-10-11 10:11:17.956 [main] INFO ktor.application - Responding at http://127.0.0.1:8080
I opened the link and the page displays, but the async call fails, keeping the following page:
Hello from JVM from Ktor. Check me value: 42
Loading...
When I check the console, I found the following error messages:
The script from “http://127.0.0.1:8080/static/piggy-bank.js” was loaded even though its MIME type (“”) is not a valid JavaScript MIME type.
Loading failed for the <script> with source “http://127.0.0.1:8080/static/piggy-bank.js”. [127.0.0.1:8080:8:1](http://127.0.0.1:8080/)
I dug up the build directory, and found that the requested file (piggy-bank.js) is built and there in
build\js\packages\piggy-bank\kotlin\piggy-bank.js
What should I configure to “link” the src/jsMain files to the webserver static?
Some more info:
I converted the build script to Kotlin DSL, and added the suggestions of this post:
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack
buildscript {
repositories {
jcenter()
}
}
plugins {
id("org.jetbrains.kotlin.multiplatform") version "1.3.50"
}
repositories {
jcenter()
maven( "https://dl.bintray.com/kotlin/ktor" )
mavenCentral()
}
val ktor_version = "1.1.3"
val logback_version = "1.2.3"
kotlin {
js {
browser { }
}
jvm {
compilations.named("main") {
tasks.getByName<Copy>(processResourcesTaskName) {
dependsOn("jsBrowserWebpack")
tasks.named<KotlinWebpack>("jsBrowserWebpack") {
from(entry.name, destinationDirectory)
}
}
}
}
sourceSets {
commonMain {
dependencies {
implementation(kotlin("stdlib-common"))
}
}
commonTest {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
named("jvmMain") {
dependencies {
implementation( kotlin("stdlib-jdk8"))
implementation( "io.ktor:ktor-server-netty:$ktor_version")
implementation( "io.ktor:ktor-html-builder:$ktor_version")
implementation( "ch.qos.logback:logback-classic:$logback_version")
}
}
named("jvmTest") {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-testng"))
}
}
named("jsMain") {
dependencies {
implementation( kotlin("stdlib-js"))
}
}
named("jsTest") {
dependencies {
implementation( kotlin("test-js"))
}
}
}
}
tasks.register<JavaExec>("run") {
dependsOn("jvmJar")
group = "application"
main = "sample.SampleJvmKt"
val t = tasks.named<Jar>("jvmJar")
classpath(configurations.named("jvmRuntimeClasspath"), t.get() )
}
With this, the webpack js is copied into the jar, but with the name: piggy-bank-unspecified.js
(I guess the unspecified stands in place of the version number.)
But even with this changes, I was not able to tell the Netty server to find the JS files.