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

WASM support #289

Merged
merged 6 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ buildscript {
classpath(libs.plugin.ktlint)
classpath(libs.plugin.maven)
classpath(libs.plugin.multiplatform.compose)
classpath(libs.plugin.atomicfu)
}
}

Expand Down
18 changes: 18 additions & 0 deletions buildSrc/src/main/kotlin/Setup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.gradle.kotlin.dsl.withType
import org.gradle.kotlin.dsl.getByType
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

private fun BaseExtension.setupAndroid() {
Expand Down Expand Up @@ -69,6 +70,7 @@ fun Project.setupModuleForAndroidxCompose(
fun Project.setupModuleForComposeMultiplatform(
withKotlinExplicitMode: Boolean = true,
fullyMultiplatform: Boolean = false,
enableWasm: Boolean = true,
iosPrefixName: String = "ios" // only used in ios sample
) {
plugins.withType<org.jetbrains.kotlin.gradle.plugin.KotlinBasePluginWrapper> {
Expand All @@ -92,6 +94,10 @@ fun Project.setupModuleForComposeMultiplatform(
js(IR) {
browser()
}
if (enableWasm) {
@OptIn(ExperimentalWasmDsl::class)
wasmJs { browser() }
}
macosX64()
macosArm64()
ios(iosPrefixName)
Expand Down Expand Up @@ -126,6 +132,13 @@ fun Project.setupModuleForComposeMultiplatform(
}

if (fullyMultiplatform) {
val commonWebMain by creating {
dependsOn(commonMain)
}

val jsMain by getting
jsMain.dependsOn(commonWebMain)

val nativeMain by creating {
dependsOn(commonMain)
}
Expand All @@ -145,6 +158,11 @@ fun Project.setupModuleForComposeMultiplatform(
val iosSimulatorArm64Main = getByName(iosPrefixName + "SimulatorArm64Main").apply {
dependsOn(iosMain)
}

if (enableWasm) {
val wasmJsMain by getting
wasmJsMain.dependsOn(commonWebMain)
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ kotlin.native.ignoreIncorrectDependencies=true
android.defaults.buildfeatures.buildconfig = false

org.jetbrains.compose.experimental.macos.enabled=true
org.jetbrains.compose.experimental.uikit.enabled=true
org.jetbrains.compose.experimental.jscanvas.enabled=true
org.jetbrains.compose.experimental.wasm.enabled=true

kotlinx.atomicfu.enableJvmIrTransformation=true
kotlinx.atomicfu.enableNativeIrTransformation=true
kotlinx.atomicfu.enableJsIrTransformation=true

kotlin.mpp.androidSourceSetLayoutVersion=2

Expand Down
8 changes: 5 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
plugin-android = "8.1.1"
plugin-ktlint = "11.5.1"
plugin-maven = "0.25.3"
plugin-multiplatform-compose = "1.5.11"
plugin-multiplatform-compose = "1.6.0-alpha01"
plugin-binaryCompatibilityValidator = "0.13.2"
plugin-atomicfu = "0.23.1"

coroutines = "1.7.3"
coroutines = "1.8.0-RC"
kotlin = "1.9.21"
kodein = "7.20.2"
koin = "3.4.3"
Expand All @@ -24,7 +25,7 @@ rxjava = "3.1.5"

junit = "5.10.0"

multiplatformUuid = "0.8.1"
multiplatformUuid = "0.8.2"

[libraries]
plugin-android = { module = "com.android.tools.build:gradle", version.ref = "plugin-android" }
Expand All @@ -33,6 +34,7 @@ plugin-ktlint = { module = "org.jlleitschuh.gradle:ktlint-gradle", version.ref =
plugin-maven = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "plugin-maven" }
plugin-hilt = { module = "com.google.dagger:hilt-android-gradle-plugin", version.ref = "hilt" }
plugin-multiplatform-compose = { module = "org.jetbrains.compose:compose-gradle-plugin", version.ref = "plugin-multiplatform-compose" }
plugin-atomicfu = { module = "org.jetbrains.kotlinx:atomicfu-gradle-plugin", version.ref = "plugin-atomicfu" }

leakCanary = { module = "com.squareup.leakcanary:leakcanary-android", version.ref = "leakCanary" }
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
Expand Down
14 changes: 14 additions & 0 deletions samples/multiplatform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ import org.jetbrains.compose.desktop.application.dsl.TargetFormat.Dmg
import org.jetbrains.compose.desktop.application.dsl.TargetFormat.Msi
import org.jetbrains.compose.desktop.application.tasks.AbstractNativeMacApplicationPackageTask
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

plugins {
kotlin("multiplatform")
id("com.android.application")
id("org.jetbrains.compose")
}

kotlin {
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
moduleName = "composeApp"
browser {
commonWebpackConfig {
outputFileName = "composeApp.js"
}
}
binaries.executable()
}
}

setupModuleForComposeMultiplatform(
fullyMultiplatform = true,
withKotlinExplicitMode = false
Expand Down
8 changes: 8 additions & 0 deletions samples/multiplatform/src/wasmJsMain/kotlin/main.wasmJs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.CanvasBasedWindow
import cafe.adriel.voyager.sample.multiplatform.SampleApplication

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
CanvasBasedWindow(canvasElementId = "ComposeTarget") { SampleApplication() }
}
12 changes: 12 additions & 0 deletions samples/multiplatform/src/wasmJsMain/resources/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Compose App</title>
<script type="application/javascript" src="skiko.js"></script>
<script type="application/javascript" src="composeApp.js"></script>
Comment on lines +6 to +7
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put these scripts before </body> tag? Loading scripts inside head tag is not recommended.

</head>
<body>
<canvas id="ComposeTarget"></canvas>
</body>
</html>
3 changes: 2 additions & 1 deletion voyager-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id("com.android.library")
id("org.jetbrains.compose")
id("com.vanniktech.maven.publish")
id("kotlinx-atomicfu")
}

setupModuleForComposeMultiplatform(fullyMultiplatform = true)
Expand Down Expand Up @@ -36,7 +37,7 @@ kotlin {
implementation(libs.lifecycle.viewModelCompose)
}
}
val jsMain by getting {
val commonWebMain by getting {
dependencies {
implementation(libs.multiplatformUuid)
}
Expand Down
5 changes: 4 additions & 1 deletion voyager-kodein/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ plugins {
id("com.vanniktech.maven.publish")
}

setupModuleForComposeMultiplatform(fullyMultiplatform = true)
setupModuleForComposeMultiplatform(
fullyMultiplatform = true,
enableWasm = false, // https://github.com/kosi-libs/Kodein/issues/447
)

android {
namespace = "cafe.adriel.voyager.kodein"
Expand Down
5 changes: 4 additions & 1 deletion voyager-koin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ plugins {
id("com.vanniktech.maven.publish")
}

setupModuleForComposeMultiplatform(fullyMultiplatform = true)
setupModuleForComposeMultiplatform(
fullyMultiplatform = true,
enableWasm = false, // https://github.com/InsertKoinIO/koin/issues/1634
)

android {
namespace = "cafe.adriel.voyager.koin"
Expand Down