首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置Netty (或Spark)以静态形式提供kotlin-js模块的输出

设置Netty (或Spark)以静态形式提供kotlin-js模块的输出
EN

Stack Overflow用户
提问于 2019-10-17 22:40:08
回答 1查看 142关注 0票数 0

我尝试使用Kotlin创建一个全栈项目。由于多平台项目在Kotlin中是实验性的,因此没有太多可用信息,因此我尝试从IDEA项目向导的项目框架开始(Kotlin > JS Client和JVM Server)。它生成基本代码,甚至添加"hello world“类型的示例代码。

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

代码语言:javascript
复制
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/)

该示例使用Netty作为嵌入式web服务器,预生成的代码为:

代码语言:javascript
复制
embeddedServer(Netty, port = 8091, 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..."
                    }
                    script(src = "/static/piggy-bank.js") {}
                }
            }
        }
        static("/static") {
            resource("piggy-bank.js")
        }
    }
}.start(wait = true)

它在static文件夹中查找已编译的js代码,但是gradle任务不会创建,也不会将生成的文件复制到静态文件夹中。

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

代码语言:javascript
复制
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):

代码语言:javascript
复制
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() )
}

我应该如何更改构建文件以构建正确的代码?

EN

回答 1

Stack Overflow用户

发布于 2020-01-16 20:16:20

我用"org.jetbrains.kotlin.multiplatform" 1.3.50重现了这个问题,但用1.3.61重现失败。你能把它和你的项目核对一下吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58435200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档