设置Netty(或Spark)以将kotlin js模块的输出设置为静态

设置Netty(或Spark)以将kotlin js模块的输出设置为静态,kotlin,kotlin-multiplatform,kotlin-js,Kotlin,Kotlin Multiplatform,Kotlin Js,我尝试使用Kotlin创建一个完整的堆栈项目。由于多平台项目在Kotlin中处于试验阶段,因此没有太多可用信息,因此我尝试从IDEA项目向导的项目框架(Kotlin>JS客户端和JVM服务器)开始。它生成基本代码,甚至添加“hello world”类型的示例代码 但是,当我构建项目并启动它(gradle run)时,网页控制台告诉我kotlin js包不可用: The script from “http://127.0.0.1:8080/static/piggy-bank.js” was loa

我尝试使用Kotlin创建一个完整的堆栈项目。由于多平台项目在Kotlin中处于试验阶段,因此没有太多可用信息,因此我尝试从IDEA项目向导的项目框架(Kotlin>JS客户端和JVM服务器)开始。它生成基本代码,甚至添加“hello world”类型的示例代码

但是,当我构建项目并启动它(gradle run)时,网页控制台告诉我kotlin js包不可用:

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/)
它在
static
文件夹中查看编译后的js代码,但是gradle任务不会创建,也不会将生成的文件复制到static文件夹中

通过一些重构,我成功地使示例工作。首先,我找到了所需的代码,并在
build/js/packages/piggy bank/kotlin
中找到了它,因此我更改了静态配置:

val buildDir = System.getProperty("user.dir")+"/build"
val jsDir = "$buildDir/js/packages/piggy-bank/kotlin"
val jsImpDir = "$buildDir/js/packages_imported/kotlin/1.3.50"

embeddedServer(Netty, port = 8090, host = "127.0.0.1") {
    routing {
        get("/") {
            call.respondHtml {
                head {
                    title("Hello from Ktor!")
                }
                body {
                    +"${hello()} from Ktor. Check me value: ${Sample().checkMe()}"
                    div {
                        id = "js-response"
                        +"Loading..."
                    }
                    // Note, that I had to add Kotlin system js file manually!
                    script(src = "/static/kotlin.js") {}
                    script(src = "/static/piggy-bank.js") {}
                }
            }
        }
        static("static") {
            // Here comes the new references
            files( "$jsDir")
            files( "$jsImpDir")
        }
    }
}.start(wait = true)
这暂时解决了问题,但只在IDEA中起作用(直接引用build文件夹)。正确的解决方案应该是,gradle构建脚本创建一个自包含、完全可操作的代码

这是我(也生成)的gradle文件(我已从Groovy迁移到Kotlin DSL):

import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack
构建脚本{
存储库{
jcenter()
}
}
插件{
id(“org.jetbrains.kotlin.multiplatform”)版本“1.3.50”
}
存储库{
jcenter()
马文(“https://dl.bintray.com/kotlin/ktor" )
mavenCentral()
}
val ktor_version=“1.1.3”
val logback_version=“1.2.3”
科特林{
js{
浏览器{}
}
虚拟机{
汇编。命名为(“主”){
tasks.getByName(processResourcesTaskName){
dependsOn(“jsBrowserWebpack”)
tasks.named(“jsBrowserWebpack”){
println(this.outputs)
from(entry.name,destinationDirectory)
}
}
}
}
源集{
普通人{
依赖关系{
实施(kotlin(“stdlib通用”))
}
}
普通测试{
依赖关系{
实现(kotlin(“测试通用”))
实现(kotlin(“测试注释通用”))
}
}
命名(“jvmMain”){
依赖关系{
实施(kotlin(“stdlib-jdk8”))
实现(“io.ktor:ktor服务器净值:$ktor_版本”)
实现(“io.ktor:ktor html生成器:$ktor_版本”)
实现(“ch.qos.logback:logbackclassic:$logback\u版本”)
}
}
命名(“jvmTest”){
依赖关系{
实施(kotlin(“测试”))
实现(kotlin(“testng”))
}
}
命名为(“jsMain”){
依赖关系{
实施(kotlin(“stdlib js”))
}
}
命名(“jsTest”){
依赖关系{
实现(kotlin(“测试js”))
}
}
}
}
任务。注册(“运行”){
dependsOn(“jvmJar”)
group=“应用程序”
main=“sample.SampleJvmKt”
val t=tasks.named(“jvmJar”)
类路径(configurations.named(“jvmRuntimeClasspath”),t.get()
}

如何更改生成文件以生成正确的代码?

我用
org.jetbrains.kotlin.multiplatform“1.3.50
重现了该问题,但用
1.3.61
重现失败。你能和你的项目核对一下吗

val buildDir = System.getProperty("user.dir")+"/build"
val jsDir = "$buildDir/js/packages/piggy-bank/kotlin"
val jsImpDir = "$buildDir/js/packages_imported/kotlin/1.3.50"

embeddedServer(Netty, port = 8090, host = "127.0.0.1") {
    routing {
        get("/") {
            call.respondHtml {
                head {
                    title("Hello from Ktor!")
                }
                body {
                    +"${hello()} from Ktor. Check me value: ${Sample().checkMe()}"
                    div {
                        id = "js-response"
                        +"Loading..."
                    }
                    // Note, that I had to add Kotlin system js file manually!
                    script(src = "/static/kotlin.js") {}
                    script(src = "/static/piggy-bank.js") {}
                }
            }
        }
        static("static") {
            // Here comes the new references
            files( "$jsDir")
            files( "$jsImpDir")
        }
    }
}.start(wait = true)
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") {
                     println(this.outputs)
                     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() )
}