Skip to content

Commit

Permalink
Support KMP for wasm (#230)
Browse files Browse the repository at this point in the history
* Support KMP for wasm

* Add sample for wasm

* Minimize sample codes for wasm

* Remove unnecessary dependencies
  • Loading branch information
fornewid authored Jun 22, 2024
1 parent 0e1e299 commit 2ed9d42
Show file tree
Hide file tree
Showing 12 changed files with 3,156 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.getByType
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import soup.compose.material.motion.buildlogic.configureKotlin

class KotlinMultiplatformConventionPlugin : Plugin<Project> {
Expand All @@ -28,6 +29,11 @@ class KotlinMultiplatformConventionPlugin : Plugin<Project> {
macosX64()
macosArm64()

@OptIn(ExperimentalWasmDsl::class)
wasmJs {
browser()
}

configureKotlin()
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ kotlin {
commonMain.dependencies {
implementation(compose.animation)
implementation(compose.ui)
implementation(libs.androidx.annotation)
implementation(libs.jetbrains.annotation)
}
androidInstrumentedTest.dependencies {
implementation(libs.androidx.test.core)
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ kotlin.mpp.androidSourceSetLayoutVersion=2
kotlin.mpp.enableCInteropCommonization=true

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

# Publish
SONATYPE_HOST=S01
Expand Down
3 changes: 1 addition & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ compose-plugin = "1.6.10"
navigation = "2.7.0-alpha07"

# AndroidX
androidx-annotation = "1.7.0"
androidx-activity = "1.9.0"
androidx-tracing = "1.1.0"

Expand All @@ -26,12 +25,12 @@ compose-foundation = { module = "androidx.compose.foundation:foundation", versio
compose-ui-testJunit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "compose" }
compose-ui-testManifest = { module = "androidx.compose.ui:ui-test-manifest", version.ref = "compose" }

jetbrains-annotation = { module = "org.jetbrains.compose.annotation-internal:annotation", version.ref = "compose-plugin" }
jetbrains-navigation-compose = { module = "org.jetbrains.androidx.navigation:navigation-compose", version.ref = "navigation" }

# AndroidX

androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity" }
androidx-annotation = { module = "androidx.annotation:annotation", version.ref = "androidx-annotation" }
androidx-tracing = { module = "androidx.tracing:tracing", version.ref = "androidx-tracing" }

# Test
Expand Down
3,044 changes: 3,044 additions & 0 deletions kotlin-js-store/yarn.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion sample/android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ android {
}

dependencies {
implementation(projects.core)
implementation(projects.sample.shared)
implementation(libs.androidx.activity.compose)
}
21 changes: 21 additions & 0 deletions sample/shared/src/wasmJsMain/kotlin/BackHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2024 SOUP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import androidx.compose.runtime.Composable

@Composable
actual fun BackHandler(enabled: Boolean, onBack: () -> Unit) {
// do nothing
}
34 changes: 34 additions & 0 deletions sample/web-wasm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig

plugins {
id("org.jetbrains.kotlin.multiplatform")
id("mmc.compose")
}

kotlin {
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
moduleName = "sample"
browser {
commonWebpackConfig {
outputFileName = "sample.js"
devServer = (devServer ?: KotlinWebpackConfig.DevServer()).apply {
static = (static ?: mutableListOf()).apply {
// Serve sources to debug inside browser
add(project.projectDir.path)
}
}
}
}

binaries.executable()
}

sourceSets {
commonMain.dependencies {
implementation(projects.sample.shared)
implementation(compose.ui)
}
}
}
26 changes: 26 additions & 0 deletions sample/web-wasm/src/wasmJsMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2024 SOUP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.ComposeViewport
import kotlinx.browser.document
import soup.compose.material.motion.shared.App

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
ComposeViewport(document.body!!) {
App()
}
}
12 changes: 12 additions & 0 deletions sample/web-wasm/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">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sample</title>
<link type="text/css" rel="stylesheet" href="styles.css">
<script type="application/javascript" src="sample.js"></script>
</head>
<body>
</body>
</html>
7 changes: 7 additions & 0 deletions sample/web-wasm/src/wasmJsMain/resources/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
4 changes: 3 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pluginManagement {
}

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
//https://youtrack.jetbrains.com/issue/KT-68533/Kotlin-2.0-WasmJs-error-when-using-RepositoriesMode.FAILONPROJECTREPOS
//repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
Expand All @@ -21,3 +22,4 @@ include ':core'
include ':sample:android'
include ':sample:desktop'
include ':sample:shared'
include ':sample:web-wasm'

0 comments on commit 2ed9d42

Please sign in to comment.