Skip to content

Commit

Permalink
Merge pull request #940 from mikepenz/feature/explore_wasm_compatiblity
Browse files Browse the repository at this point in the history
Wasm compatiblity
  • Loading branch information
mikepenz committed Dec 17, 2023
2 parents f33b00e + f6c0e04 commit 27b6be0
Show file tree
Hide file tree
Showing 17 changed files with 3,161 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

## Latest releases 🛠

- (Next Gen) Kotlin && Multiplatform && Plugin | [v10.9.2](https://github.com/mikepenz/AboutLibraries/tree/v10.9.2)
- (Next Gen) Kotlin && Multiplatform && Plugin | [v10.9.2-wasm1](https://github.com/mikepenz/AboutLibraries/tree/v10.9.2-wasm1)
- Kotlin && Gradle Plugin | [v8.9.4](https://github.com/mikepenz/AboutLibraries/tree/v8.9.4)

## Gradle Plugin
Expand Down
30 changes: 20 additions & 10 deletions aboutlibraries-compose/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
@file:OptIn(ExperimentalWasmDsl::class)

import com.vanniktech.maven.publish.SonatypeHost
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

plugins {
kotlin("multiplatform")
Expand Down Expand Up @@ -37,13 +40,13 @@ android {
if (project.findProperty("composeCompilerReports") == "true") {
freeCompilerArgs += listOf(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=${project.buildDir.absolutePath}/compose_compiler"
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=${project.layout.buildDirectory.asFile.get().absolutePath}/compose_compiler"
)
}
if (project.findProperty("composeCompilerMetrics") == "true") {
freeCompilerArgs += listOf(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=${project.buildDir.absolutePath}/compose_compiler"
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=${project.layout.buildDirectory.asFile.get().absolutePath}/compose_compiler"
)
}
}
Expand All @@ -69,7 +72,7 @@ compose {
}

kotlin {
targetHierarchy.default()
applyDefaultHierarchyTemplate()

jvm()

Expand All @@ -82,22 +85,27 @@ kotlin {
iosSimulatorArm64()
macosX64()
macosArm64()
wasmJs {
nodejs()
}

sourceSets {
val commonMain by getting
val commonMain by getting {
dependencies {
implementation(compose.runtime)
implementation(compose.ui)
implementation(compose.foundation)
implementation(compose.material)
implementation(libs.kotlinx.collections)
}
}
val commonTest by getting
}
}

dependencies {
commonMainApi(project(":aboutlibraries-core"))

commonMainImplementation(compose.runtime)
commonMainImplementation(compose.ui)
commonMainImplementation(compose.foundation)
commonMainImplementation(compose.material)
commonMainImplementation(libs.kotlinx.collections)

debugImplementation(compose.uiTooling)
"androidMainImplementation"(compose.preview)

Expand All @@ -109,6 +117,8 @@ configurations.configureEach {
// https://github.com/chrisbanes/tivi/blob/5e7586465337d326a1f1e40e0b412ecd2779bb5c/build.gradle#L72
exclude(group = "androidx.appcompat")
exclude(group = "com.google.android.material", module = "material")


}

tasks.dokkaHtml.configure {
Expand Down
4 changes: 2 additions & 2 deletions aboutlibraries-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ android {
}

kotlin {
targetHierarchy.default()
applyDefaultHierarchyTemplate()

jvm()
js(IR) {
Expand All @@ -54,7 +54,7 @@ kotlin {
androidTarget {
publishAllLibraryVariants()
}
wasm {
wasmJs {
nodejs()
}

Expand Down
13 changes: 13 additions & 0 deletions app-wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Usage

### Generate Dependency Information

```bash
./gradlew app-wasm:exportLibraryDefinitions -PaboutLibraries.exportPath=src/wasmJsMain/resources/
```

### Run Wasm app

```
./gradlew :app-wasm:wasmJsRun
```
66 changes: 66 additions & 0 deletions app-wasm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig

plugins {
kotlin("multiplatform")
id("org.jetbrains.compose")
id("com.mikepenz.aboutlibraries.plugin")
}

// val copyWasmResources = tasks.create("copyWasmResourcesWorkaround", Copy::class.java) {
// from(project(":shared").file("src/commonMain/resources"))
// into("build/processedResources/wasm/main")
// }
//
// afterEvaluate {
// project.tasks.getByName("wasmProcessResources").finalizedBy(copyWasmResources)
// }

kotlin {
wasmJs {
moduleName = "aboutlibraries"
browser {
commonWebpackConfig {
devServer = (devServer ?: KotlinWebpackConfig.DevServer()).copy(
open = mapOf(
"app" to mapOf(
"name" to "google chrome canary",
"arguments" to listOf("--js-flags=--experimental-wasm-gc ")
)
),
static = (devServer?.static ?: mutableListOf()).apply {
add(project.rootDir.path)
add(project.rootDir.path + "/common/")
add(project.rootDir.path + "/nonAndroidMain/")
add(project.rootDir.path + "/web/")
},
)
}
}
binaries.executable()
}

sourceSets {
val wasmJsMain by getting {
dependencies {
implementation(compose.runtime)
implementation(compose.ui)
implementation(compose.foundation)
implementation(compose.material)
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.components.resources)

implementation(project(":aboutlibraries-core"))
implementation(project(":aboutlibraries-compose"))
}
}
}
}

compose.experimental {
web.application {}
}

compose {
kotlinCompilerPlugin.set(libs.versions.composeCompilerJb.get())
// kotlinCompilerPluginArgs.add("suppressKotlinVersionCompatibilityCheck=${libs.versions.kotlinCore.get()}")
}
8 changes: 8 additions & 0 deletions app-wasm/src/wasmJsMain/kotlin/Color.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import androidx.compose.ui.graphics.Color

val Green = Color(0xff09B3AF)
val GreenDark = Color(0xff008380)
val GreenLight = Color(0x5de6e1)

val Red200 = Color(0xfff297a2)
val Red800 = Color(0xffd00036)
40 changes: 40 additions & 0 deletions app-wasm/src/wasmJsMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.produceState
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.window.CanvasBasedWindow
import com.mikepenz.aboutlibraries.Libs
import com.mikepenz.aboutlibraries.ui.compose.LibrariesContainer
import com.mikepenz.aboutlibraries.ui.compose.util.StableLibs
import com.mikepenz.aboutlibraries.ui.compose.util.stable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.resource

@OptIn(ExperimentalComposeUiApi::class, ExperimentalResourceApi::class)
fun main() {
CanvasBasedWindow("AboutLibraries", canvasElementId = "aboutLibsCanvas") {
SampleTheme {
Scaffold(
topBar = { TopAppBar(title = { Text("AboutLibraries Compose Desktop Sample") }) }
) {
val libraries = produceState<StableLibs?>(null) {
value = withContext(Dispatchers.Default) {
Libs.Builder()
.withJson(resource("aboutlibraries.json").readBytes().decodeToString())
.build().stable
}
}

LibrariesContainer(
libraries.value,
Modifier.fillMaxSize()
)
}
}
}
}
42 changes: 42 additions & 0 deletions app-wasm/src/wasmJsMain/kotlin/Theme.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.MaterialTheme
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color

private val DarkColorPalette = darkColors(
primary = Green,
primaryVariant = GreenDark,
onPrimary = Color.White,
secondary = Green,
onSecondary = Color.White,
error = Red200
)

private val LightColorPalette = lightColors(
primary = Green,
primaryVariant = GreenDark,
onPrimary = Color.White,
secondary = Green,
secondaryVariant = GreenDark,
onSecondary = Color.White,
error = Red800
)

@Composable
fun SampleTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable() () -> Unit,
) {
val colors = if (darkTheme) {
DarkColorPalette
} else {
LightColorPalette
}

MaterialTheme(
colors = colors,
content = content
)
}
1 change: 1 addition & 0 deletions app-wasm/src/wasmJsMain/resources/aboutlibraries.json

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions app-wasm/src/wasmJsMain/resources/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- http://localhost:80 -->
<meta http-equiv="origin-trial" content="AjytAe1FWUIWH/RzqsOWlXYw7kGwt11ejbvR/ArmAU4QbXIPg69wzpniDhPByiOZ9pdNqsdMsXsoxggVtfGhwwMAAABOeyJvcmlnaW4iOiJodHRwOi8vbG9jYWxob3N0OjgwIiwiZmVhdHVyZSI6IldlYkFzc2VtYmx5R0MiLCJleHBpcnkiOjE2OTg5Njk1OTl9">
<!-- http://localhost:8080 -->
<meta http-equiv="origin-trial" content="AtSRFRWKTCcQHDFUUQL1VaVz9mjmsK1xm5k0S8etkmvMLa5TsiNeQsle/eDgdZVkm1PM1WmrSMx52kI8yAP2UgEAAABQeyJvcmlnaW4iOiJodHRwOi8vbG9jYWxob3N0OjgwODAiLCJmZWF0dXJlIjoiV2ViQXNzZW1ibHlHQyIsImV4cGlyeSI6MTY5ODk2OTU5OX0=">
<!-- http://localhost:8081 -->
<meta http-equiv="origin-trial" content="AspyK9xe4tO2aIvJWnr5HTRT9GqpGJID2qZjj+0OvhKHp9JrUP+92PF+Rt11Tyi7cKSEB7OFFGdeVs7Ns5TMhAUAAABQeyJvcmlnaW4iOiJodHRwOi8vbG9jYWxob3N0OjgwODEiLCJmZWF0dXJlIjoiV2ViQXNzZW1ibHlHQyIsImV4cGlyeSI6MTY5ODk2OTU5OX0=">
<!-- http://localhost:8082 -->
<meta http-equiv="origin-trial" content="AoPGqwBGEQW3twSDYvXt1JHz5zg7Z4o+Orwr8NxdVoXJ68zjwOi2C8/0w4xFYW6UUH8w97eIjykGOa/+jIBfTQMAAABQeyJvcmlnaW4iOiJodHRwOi8vbG9jYWxob3N0OjgwODIiLCJmZWF0dXJlIjoiV2ViQXNzZW1ibHlHQyIsImV4cGlyeSI6MTY5ODk2OTU5OX0=">
<!-- http://localhost:8083 -->
<meta http-equiv="origin-trial" content="AvV2HYiHMw7lpIVcmGMSVmm6xC0Gzu4W5xBnZExmfHtE5zyNKFszcyNGm5IfCFQ6swnMotbn16lS1ozkJ0QzEAEAAABQeyJvcmlnaW4iOiJodHRwOi8vbG9jYWxob3N0OjgwODMiLCJmZWF0dXJlIjoiV2ViQXNzZW1ibHlHQyIsImV4cGlyeSI6MTY5ODk2OTU5OX0=">

<title>JetSnack with Kotlin/Wasm</title>
<script type="application/javascript" src="skiko.js"></script>
<script type="module" src="load.mjs"></script>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
background-color: white;
overflow: hidden;
}

#warning {
position: absolute;
top: 100px;
left: 100px;
z-index: 100;
}
#warning li {
padding-bottom: 15px;
}

#warning span.code {
font-family: monospace;
}

ul {
margin-top: 0;
margin-bottom: 15px;
}

#footer {
position: fixed;
bottom: 0;
width: 100%;
background-color: white;
}

#close {
position: absolute;
top: 0;
right: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<canvas id="aboutLibsCanvas"></canvas>
</body>
</html>
10 changes: 10 additions & 0 deletions app-wasm/src/wasmJsMain/resources/load.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { instantiate } from './aboutlibraries.uninstantiated.mjs';

await wasmSetup;

let te = null;
try {
await instantiate({ skia: Module['asm'] });
} catch (e) {
te = e;
}
31 changes: 31 additions & 0 deletions app-wasm/webpack.config.d/cleanupSourcemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Replace paths unavailable during compilation with `null`, so they will not be shown in devtools
;
(() => {
const fs = require("fs");
const path = require("path");

const outDir = __dirname + "/kotlin/"
const projecName = path.basename(__dirname);
const mapFile = outDir + projecName + ".map"

const sourcemap = JSON.parse(fs.readFileSync(mapFile))
const sources = sourcemap["sources"]
srcLoop: for (let i in sources) {
const srcFilePath = sources[i];
if (srcFilePath == null) continue;

const srcFileCandidates = [
outDir + srcFilePath,
outDir + srcFilePath.substring("../".length),
outDir + "../" + srcFilePath,
];

for (let srcFile of srcFileCandidates) {
if (fs.existsSync(srcFile)) continue srcLoop;
}

sources[i] = null;
}

fs.writeFileSync(mapFile, JSON.stringify(sourcemap));
})();

0 comments on commit 27b6be0

Please sign in to comment.