Skip to content

Commit fae1dac

Browse files
authored
Add wasmJs support (#41)
* Add wasm support * Update build.gradle.kts * implementation(kotlin("test-wasm-js"))
1 parent 5214f71 commit fae1dac

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

build.gradle.kts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import io.gitlab.arturbosch.detekt.DetektPlugin
44
import io.gitlab.arturbosch.detekt.extensions.DetektExtension
55
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
66
import org.gradle.api.tasks.testing.logging.TestLogEvent
7+
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
8+
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
9+
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
10+
import org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask
711

812
@Suppress("DSL_SCOPE_VIOLATION")
913
plugins {
@@ -107,3 +111,32 @@ allprojects {
107111
parent?.subprojects?.forEach { dependsOn(it.tasks.withType(taskClass)) }
108112
}
109113
}
114+
115+
/**
116+
* [Reference](https://github.com/square/okio/blob/55b7210fb3d52de07f4bc1122c5062e38df576d9/build.gradle.kts#L227-L248).
117+
*
118+
* Select a NodeJS version with WASI and WASM GC.
119+
* https://github.com/Kotlin/kotlin-wasm-examples/blob/main/wasi-example/build.gradle.kts
120+
*/
121+
plugins.withType<NodeJsRootPlugin> {
122+
extensions.getByType<NodeJsRootExtension>().apply {
123+
if (DefaultNativePlatform.getCurrentOperatingSystem().isWindows) {
124+
// We're waiting for a Windows build of NodeJS that can do WASM GC + WASI.
125+
nodeVersion = "21.4.0"
126+
} else {
127+
// Reference:
128+
// https://github.com/drewhamilton/Poko/blob/72ec8d24cf48a74b3d1125c94f0e625ab956b93f/build.gradle.kts#L17-L19
129+
// WASM requires a canary Node.js version. This is the last v21 canary, and has both
130+
// darwin-arm64 and darwin-x64 artifacts:
131+
nodeVersion = "21.0.0-v8-canary20231024d0ddc81258"
132+
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
133+
}
134+
}
135+
// Suppress an error because yarn doesn't like our Node version string.
136+
// warning You are using Node "21.0.0-v8-canary20231024d0ddc81258" which is not supported and
137+
// may encounter bugs or unexpected behavior.
138+
// error karma@6.4.2: The engine "node" is incompatible with this module.
139+
tasks.withType<KotlinNpmInstallTask>().all {
140+
args += "--ignore-engines"
141+
}
142+
}

channel-event-bus/build.gradle.kts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,26 @@ kotlin {
2727
}
2828
}
2929
}
30+
3031
js(IR) {
31-
compilations.all {
32-
kotlinOptions {
33-
sourceMap = true
34-
moduleKind = "commonjs"
32+
moduleName = property("POM_ARTIFACT_ID")!!.toString()
33+
compilations.configureEach {
34+
compilerOptions.configure {
35+
sourceMap.set(true)
36+
moduleKind.set(org.jetbrains.kotlin.gradle.dsl.JsModuleKind.MODULE_COMMONJS)
3537
}
3638
}
3739
browser()
3840
nodejs()
3941
}
42+
@OptIn(org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl::class)
43+
wasmJs {
44+
// Module name should be different from the one from JS
45+
// otherwise IC tasks that start clashing different modules with the same module name
46+
moduleName = property("POM_ARTIFACT_ID")!!.toString() + "Wasm"
47+
browser()
48+
nodejs()
49+
}
4050

4151
// According to https://kotlinlang.org/docs/native-target-support.html
4252
// Tier 1: macosX64, macosArm64, iosSimulatorArm64, iosX64
@@ -95,6 +105,12 @@ kotlin {
95105
}
96106
}
97107

108+
val wasmJsTest by getting {
109+
dependencies {
110+
implementation(kotlin("test-wasm-js"))
111+
}
112+
}
113+
98114
jvmTest {
99115
dependencies {
100116
implementation(kotlin("test-junit"))

0 commit comments

Comments
 (0)