IDE, wizards: update Kotlin/JS support in jvm server / js client
Configure js browser sub target to use new test and application runners. Rework js linking and packaging: - get rid of required.js - use builtin jsBrowserWebpack task, embed js bundle into jvm jar, remove gradle boilerplate - ktor server: serve js bundle resource, remove boilerplate #KT-31695 Fixed #KT-31099 Fixed
This commit is contained in:
-10
File diff suppressed because one or more lines are too long
+22
-54
@@ -49,10 +49,6 @@ class KotlinGradleWebMultiplatformModuleBuilder : KotlinGradleAbstractMultiplatf
|
|||||||
val jvmResources = jvmRoot.createChildDirectory(this, "resources")
|
val jvmResources = jvmRoot.createChildDirectory(this, "resources")
|
||||||
val logBack = jvmResources.createChildData(this, "logback.xml").bufferedWriter()
|
val logBack = jvmResources.createChildData(this, "logback.xml").bufferedWriter()
|
||||||
|
|
||||||
val jsRoot = src.findChild(jsSourceName)!!
|
|
||||||
val jsResources = jsRoot.createChildDirectory(this, "resources")
|
|
||||||
val requireMinJs = jsResources.createChildData(this, "require.min.js").bufferedWriter()
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
commonMain.write(
|
commonMain.write(
|
||||||
"""
|
"""
|
||||||
@@ -93,19 +89,6 @@ class KotlinGradleWebMultiplatformModuleBuilder : KotlinGradleAbstractMultiplatf
|
|||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
embeddedServer(Netty, port = 8080, host = "127.0.0.1") {
|
embeddedServer(Netty, port = 8080, host = "127.0.0.1") {
|
||||||
val currentDir = File(".").absoluteFile
|
|
||||||
environment.log.info("Current directory: ${"$"}currentDir")
|
|
||||||
|
|
||||||
val webDir = listOf(
|
|
||||||
"web",
|
|
||||||
"../src/jsMain/web",
|
|
||||||
"src/jsMain/web"
|
|
||||||
).map {
|
|
||||||
File(currentDir, it)
|
|
||||||
}.firstOrNull { it.isDirectory }?.absoluteFile ?: error("Can't find 'web' folder for this sample")
|
|
||||||
|
|
||||||
environment.log.info("Web directory: ${"$"}webDir")
|
|
||||||
|
|
||||||
routing {
|
routing {
|
||||||
get("/") {
|
get("/") {
|
||||||
call.respondHtml {
|
call.respondHtml {
|
||||||
@@ -113,22 +96,17 @@ class KotlinGradleWebMultiplatformModuleBuilder : KotlinGradleAbstractMultiplatf
|
|||||||
title("Hello from Ktor!")
|
title("Hello from Ktor!")
|
||||||
}
|
}
|
||||||
body {
|
body {
|
||||||
+"${"$"}{hello()} from Ktor. Check me value: ${"$"}{Sample().checkMe()}"
|
+"${'$'}{hello()} from Ktor. Check me value: ${'$'}{Sample().checkMe()}"
|
||||||
div {
|
div {
|
||||||
id = "js-response"
|
id = "js-response"
|
||||||
+"Loading..."
|
+"Loading..."
|
||||||
}
|
}
|
||||||
script(src = "/static/require.min.js") {
|
script(src = "/static/$name.js") {}
|
||||||
}
|
|
||||||
script {
|
|
||||||
+"require.config({baseUrl: '/static'});\n"
|
|
||||||
+"require(['/static/$name.js'], function(js) { js.sample.helloWorld('Hi'); });\n"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static("/static") {
|
static("/static") {
|
||||||
files(webDir)
|
resource("$name.js")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.start(wait = true)
|
}.start(wait = true)
|
||||||
@@ -173,11 +151,15 @@ class KotlinGradleWebMultiplatformModuleBuilder : KotlinGradleAbstractMultiplatf
|
|||||||
val message = "${"$"}salutation from Kotlin.JS ${"$"}{hello()}, check me value: ${"$"}{Sample().checkMe()}"
|
val message = "${"$"}salutation from Kotlin.JS ${"$"}{hello()}, check me value: ${"$"}{Sample().checkMe()}"
|
||||||
document.getElementById("js-response")?.textContent = message
|
document.getElementById("js-response")?.textContent = message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
document.addEventListener("DOMContentLoaded", {
|
||||||
|
helloWorld("Hi!")
|
||||||
|
})
|
||||||
|
}
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
)
|
)
|
||||||
|
|
||||||
requireMinJs.write(requireMinJsContent)
|
|
||||||
|
|
||||||
commonTest.write(
|
commonTest.write(
|
||||||
"""
|
"""
|
||||||
package sample
|
package sample
|
||||||
@@ -226,7 +208,7 @@ class KotlinGradleWebMultiplatformModuleBuilder : KotlinGradleAbstractMultiplatf
|
|||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
)
|
)
|
||||||
} finally {
|
} finally {
|
||||||
listOf(commonMain, commonTest, jvmMain, jvmTest, jsMain, jsTest, logBack, requireMinJs).forEach(BufferedWriter::close)
|
listOf(commonMain, commonTest, jvmMain, jvmTest, jsMain, jsTest, logBack).forEach(BufferedWriter::close)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +219,10 @@ class KotlinGradleWebMultiplatformModuleBuilder : KotlinGradleAbstractMultiplatf
|
|||||||
|
|
||||||
kotlin {
|
kotlin {
|
||||||
jvm()
|
jvm()
|
||||||
js()
|
js {
|
||||||
|
browser {
|
||||||
|
}
|
||||||
|
}
|
||||||
sourceSets {
|
sourceSets {
|
||||||
$commonSourceName {
|
$commonSourceName {
|
||||||
dependencies {
|
dependencies {
|
||||||
@@ -277,32 +262,15 @@ class KotlinGradleWebMultiplatformModuleBuilder : KotlinGradleAbstractMultiplatf
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def webFolder = new File(project.buildDir, "../src/jsMain/web")
|
jvmJar {
|
||||||
def jsCompilations = kotlin.targets.js.compilations
|
dependsOn(jsBrowserWebpack)
|
||||||
|
from(jsBrowserWebpack.outputPath)
|
||||||
task populateWebFolder(dependsOn: [jsMainClasses]) {
|
|
||||||
doLast {
|
|
||||||
copy {
|
|
||||||
from jsCompilations.main.output
|
|
||||||
from kotlin.sourceSets.jsMain.resources.srcDirs
|
|
||||||
jsCompilations.test.runtimeDependencyFiles.each {
|
|
||||||
if (it.exists() && !it.isDirectory()) {
|
|
||||||
from zipTree(it.absolutePath).matching { include '*.js' }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
into webFolder
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
jsJar.dependsOn(populateWebFolder)
|
task run(type: JavaExec, dependsOn: [jvmJar]) {
|
||||||
|
group = "application"
|
||||||
task run(type: JavaExec, dependsOn: [jvmMainClasses, jsJar]) {
|
main = "sample.SampleJvmKt"
|
||||||
main = "sample.Sample${jvmTargetName.capitalize()}Kt"
|
classpath(configurations.jvmRuntimeClasspath, jvmJar)
|
||||||
classpath { [
|
|
||||||
kotlin.targets.jvm.compilations.main.output.allOutputs.files,
|
|
||||||
configurations.jvmRuntimeClasspath,
|
|
||||||
] }
|
|
||||||
args = []
|
args = []
|
||||||
}
|
}
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
|
|||||||
Reference in New Issue
Block a user