Skip to content

Add wasmJs support #41

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

Merged
merged 3 commits into from
Mar 16, 2024
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
33 changes: 33 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import io.gitlab.arturbosch.detekt.DetektPlugin
import io.gitlab.arturbosch.detekt.extensions.DetektExtension
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
import org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask

@Suppress("DSL_SCOPE_VIOLATION")
plugins {
Expand Down Expand Up @@ -107,3 +111,32 @@ allprojects {
parent?.subprojects?.forEach { dependsOn(it.tasks.withType(taskClass)) }
}
}

/**
* [Reference](https://github.com/square/okio/blob/55b7210fb3d52de07f4bc1122c5062e38df576d9/build.gradle.kts#L227-L248).
*
* Select a NodeJS version with WASI and WASM GC.
* https://github.com/Kotlin/kotlin-wasm-examples/blob/main/wasi-example/build.gradle.kts
*/
plugins.withType<NodeJsRootPlugin> {
extensions.getByType<NodeJsRootExtension>().apply {
if (DefaultNativePlatform.getCurrentOperatingSystem().isWindows) {
// We're waiting for a Windows build of NodeJS that can do WASM GC + WASI.
nodeVersion = "21.4.0"
} else {
// Reference:
// https://github.com/drewhamilton/Poko/blob/72ec8d24cf48a74b3d1125c94f0e625ab956b93f/build.gradle.kts#L17-L19
// WASM requires a canary Node.js version. This is the last v21 canary, and has both
// darwin-arm64 and darwin-x64 artifacts:
nodeVersion = "21.0.0-v8-canary20231024d0ddc81258"
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}
}
// Suppress an error because yarn doesn't like our Node version string.
// warning You are using Node "21.0.0-v8-canary20231024d0ddc81258" which is not supported and
// may encounter bugs or unexpected behavior.
// error [email protected]: The engine "node" is incompatible with this module.
tasks.withType<KotlinNpmInstallTask>().all {
args += "--ignore-engines"
}
}
24 changes: 20 additions & 4 deletions channel-event-bus/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,26 @@ kotlin {
}
}
}

js(IR) {
compilations.all {
kotlinOptions {
sourceMap = true
moduleKind = "commonjs"
moduleName = property("POM_ARTIFACT_ID")!!.toString()
compilations.configureEach {
compilerOptions.configure {
sourceMap.set(true)
moduleKind.set(org.jetbrains.kotlin.gradle.dsl.JsModuleKind.MODULE_COMMONJS)
}
}
browser()
nodejs()
}
@OptIn(org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl::class)
wasmJs {
// Module name should be different from the one from JS
// otherwise IC tasks that start clashing different modules with the same module name
moduleName = property("POM_ARTIFACT_ID")!!.toString() + "Wasm"
browser()
nodejs()
}

// According to https://kotlinlang.org/docs/native-target-support.html
// Tier 1: macosX64, macosArm64, iosSimulatorArm64, iosX64
Expand Down Expand Up @@ -95,6 +105,12 @@ kotlin {
}
}

val wasmJsTest by getting {
dependencies {
implementation(kotlin("test-wasm-js"))
}
}

jvmTest {
dependencies {
implementation(kotlin("test-junit"))
Expand Down