Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes wasmJs running #2063

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading