From f4e05a5d24a7bfccf28018bf4d7ecc9bed55e414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petrus=20Nguy=E1=BB=85n=20Th=C3=A1i=20H=E1=BB=8Dc?= Date: Sat, 16 Mar 2024 17:45:38 +0700 Subject: [PATCH 1/3] Add wasm support --- channel-event-bus/build.gradle.kts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/channel-event-bus/build.gradle.kts b/channel-event-bus/build.gradle.kts index 16efe18..98609b8 100644 --- a/channel-event-bus/build.gradle.kts +++ b/channel-event-bus/build.gradle.kts @@ -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 From 1c38d01b314d44e1e5899a1e1b427c6aff09ba79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petrus=20Nguy=E1=BB=85n=20Th=C3=A1i=20H=E1=BB=8Dc?= Date: Sat, 16 Mar 2024 17:47:29 +0700 Subject: [PATCH 2/3] Update build.gradle.kts --- build.gradle.kts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/build.gradle.kts b/build.gradle.kts index b4f2d0a..b327b5c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 { @@ -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 { + extensions.getByType().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 karma@6.4.2: The engine "node" is incompatible with this module. + tasks.withType().all { + args += "--ignore-engines" + } +} From 91c3e43210f1d7612f168efe8e9169084810c684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petrus=20Nguy=E1=BB=85n=20Th=C3=A1i=20H=E1=BB=8Dc?= Date: Sat, 16 Mar 2024 18:07:25 +0700 Subject: [PATCH 3/3] implementation(kotlin("test-wasm-js")) --- channel-event-bus/build.gradle.kts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/channel-event-bus/build.gradle.kts b/channel-event-bus/build.gradle.kts index 98609b8..ecbce85 100644 --- a/channel-event-bus/build.gradle.kts +++ b/channel-event-bus/build.gradle.kts @@ -105,6 +105,12 @@ kotlin { } } + val wasmJsTest by getting { + dependencies { + implementation(kotlin("test-wasm-js")) + } + } + jvmTest { dependencies { implementation(kotlin("test-junit"))