Skip to content

Commit

Permalink
Fixes runWasmJs (#2063)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz authored Dec 15, 2023
1 parent b8bd99b commit 0dc2cc3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
31 changes: 16 additions & 15 deletions buildSrc/src/main/kotlin/korlibs/korge/gradle/targets/js/Wasm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import korlibs.korge.gradle.targets.*
import korlibs.korge.gradle.targets.wasm.*
import korlibs.korge.gradle.util.*
import org.gradle.api.*
import org.gradle.api.tasks.TaskAction
import org.jetbrains.kotlin.gradle.targets.js.npm.*
import java.io.*

Expand All @@ -16,26 +17,25 @@ fun Project.configureWasm(projectType: ProjectType, binaryen: Boolean = false) {

if (projectType.isExecutable) {

project.tasks.createThis<Task>("wasmCreateIndex") {
doFirst {
wasmCreateIndex(project)
}
val wasmJsCreateIndex = project.tasks.createThis<WasmJsCreateIndexTask>("wasmJsCreateIndex") {
}
project.tasks.findByName("wasmBrowserDevelopmentRun")?.apply {
dependsOn("wasmCreateIndex")
doFirst { wasmCreateIndex(project) }
//:compileDevelopmentExecutableKotlinWasmJs
project.tasks.findByName("wasmJsBrowserDevelopmentRun")?.apply {
dependsOn(wasmJsCreateIndex)
}
val task = project.tasks.createThis<Task>("runWasm") {
dependsOn("wasmRun")
project.tasks.createThis<Task>("runWasmJs") {
dependsOn("wasmJsRun")
}
}
}

fun wasmCreateIndex(project: Project) {
val compilation = project.kotlin.wasm().compilations["main"]!!
val npmDir = compilation.npmProject.dir
File(npmDir, "kotlin/index.html").also { it.parentFile.mkdirs() }.writeText(
"""
open class WasmJsCreateIndexTask : DefaultTask() {
private val npmDir: File = project.kotlin.wasm().compilations["main"]!!.npmProject.dir

@TaskAction
fun run() {
File(npmDir, "kotlin/index.html").also { it.parentFile.mkdirs() }.writeText(
"""
<html>
<script type = 'module'>
import module from "./${npmDir.name}.mjs"
Expand All @@ -44,5 +44,6 @@ fun wasmCreateIndex(project: Project) {
</script>
</html>
""".trimIndent()
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ fun isWasmEnabled(project: Project?): Boolean = true
fun Project.configureWasm(executable: Boolean, binaryen: Boolean = false) {
kotlin {
wasmJs {
if (binaryen) applyBinaryen()

if (executable) {
binaries.executable()
}
Expand All @@ -30,6 +28,8 @@ fun Project.configureWasm(executable: Boolean, binaryen: Boolean = false) {
// }
//}
}

if (binaryen) applyBinaryen()
}

sourceSets.maybeCreate("wasmJsTest").apply {
Expand Down
12 changes: 4 additions & 8 deletions buildSrc/src/main/kotlin/korlibs/root/RootKorlibsPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -508,16 +508,12 @@ object RootKorlibsPlugin {
rootProject.samples {
if (isWasmEnabled(project)) {
configureWasm(executable = true)
project.tasks.createThis<Task>("wasmCreateIndex") {
doFirst {
wasmCreateIndex(project)
}
val wasmJsCreateIndex = project.tasks.createThis<WasmJsCreateIndexTask>("wasmJsCreateIndex") {
}
project.tasks.findByName("wasmBrowserDevelopmentRun")?.apply {
dependsOn("wasmCreateIndex")
doFirst { wasmCreateIndex(project) }
project.tasks.findByName("wasmJsBrowserDevelopmentRun")?.apply {
dependsOn(wasmJsCreateIndex)
}
val task = project.tasks.createThis<Task>("runWasm") {
project.tasks.createThis<Task>("runWasmJs") {
dependsOn("wasmJsRun")
}
}
Expand Down

0 comments on commit 0dc2cc3

Please sign in to comment.