Skip to content

Commit

Permalink
- explore wasm compat
Browse files Browse the repository at this point in the history
- add wasm module
- force wasm for library
- prepare sample app for wasm
  • Loading branch information
mikepenz committed Nov 4, 2023
1 parent 834ef6d commit c1445ee
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 13 deletions.
21 changes: 14 additions & 7 deletions aboutlibraries-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,27 @@ kotlin {
iosSimulatorArm64()
macosX64()
macosArm64()
wasm {
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 +114,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
56 changes: 56 additions & 0 deletions app-wasm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig

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

// 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 {
wasm {
moduleName = "aboutlibraries"
browser {
commonWebpackConfig {
devServer = (devServer ?: KotlinWebpackConfig.DevServer()).copy(
static = (devServer?.static ?: mutableListOf()).apply {
add(project.rootDir.path)
add(project.rootDir.path + "/shared/")
add(project.rootDir.path + "/nonAndroidMain/")
add(project.rootDir.path + "/webApp/")
},
)
}
}
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)
}
}
}
}

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/wasmMain/kotlin/Main.wasm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import androidx.compose.ui.window.CanvasBasedWindow
import kotlin.wasm.unsafe.*

fun main() {
CanvasBasedWindow("Test") {

}
}
104 changes: 104 additions & 0 deletions app-wasm/src/wasmMain/resources/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<!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>Image Viewer (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="ComposeTarget"></canvas>

<div id="warning">
⚠️ This page using <b>experimental Kotlin/Wasm</b> which requires enabling experimental features in your browser.
<ul>
<li>For <b>Chrome</b> 110 or newer:
<ol>
<li>Open <span class="code">chrome://flags/#enable-webassembly-garbage-collection</span> in the browser.</li>
<li>Enable <strong>WebAssembly Garbage Collection</strong>.</li>
<li>Relaunch the browser.</li>
</ol>
or with Chrome 109 or newer, run the browser with the <span class="code">--js-flags=--experimental-wasm-gc</span> command line argument.</li>
<li>For <b>Edge</b> 109 or newer: run the browser with the <span class="code">--js-flags=--experimental-wasm-gc</span> command line argument.</li>
<li>For <b>Firefox Nightly</b> 112 or newer:
<ol>
<li>Open <span class="code">about:config</span> in the browser.</li>
<li>Enable <strong>javascript.options.wasm_function_references</strong> and <strong>javascript.options.wasm_gc</strong>.</li>
<li>Refresh this page.</li>
</ol>
</li>
</ul>
</div>
<div id="footer">
<div style="float: left; width: 50%; padding-right: 5px">
<span style="padding-left: 20px">Don't hesitate to share your feedback and ask questions:</span>
<ul style="">
<li>In the Kotlin Slack <code><a href="https://slack-chats.kotlinlang.org/c/compose-web">#compose-web</a></code> channel (if you’re not a member, <a href="https://kotl.in/slack">apply here</a>).</li>
<li>In <a href="https://twitter.com/bashorov/status/1646487415043792897">the original Twitter thread</a></li>
</ul>
</div>
<div style="float: left; width: 45%;">
<span style="padding-left: 20px;">For future news</span>
<ul>
<li>Join <code><a href="https://slack-chats.kotlinlang.org/c/webassembly">#webassembly</a> & <a href="https://slack-chats.kotlinlang.org/c/compose-web">#compose-web</a></code> channels in Kotlin Slack (if you’re not a member, <a href="https://kotl.in/slack">apply here</a>).</li>
<li>Follow <a href="https://twitter.com/bashorov/">@bashorov</a> on Twitter</li>
</ul>
</div>
<div id="close" onclick="document.getElementById('footer').style.display='none'"></div>
</div>
</body>
</html>
16 changes: 16 additions & 0 deletions app-wasm/src/wasmMain/resources/load.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { instantiate } from './imageviewer.uninstantiated.mjs';

await wasmSetup;

let te = null;
try {
await instantiate({ skia: Module['asm'] });
} catch (e) {
te = e;
}

if (te == null) {
document.getElementById("warning").style.display="none";
} else {
throw te;
}
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));
})();
7 changes: 3 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ buildscript {
mavenLocal()
mavenCentral()
google()
maven {
setUrl("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
maven { setUrl("https://maven.pkg.jetbrains.space/public/p/compose/dev") }
maven { setUrl("https://maven.pkg.jetbrains.space/kotlin/p/wasm/experimental") }
}
dependencies {
classpath(libs.gradle.build)
Expand All @@ -30,7 +29,7 @@ allprojects {
mavenCentral()
google()
maven { setUrl("https://androidx.dev/storage/compose-compiler/repository") }
// maven { setUrl("https://maven.pkg.jetbrains.space/public/p/compose/dev") }
maven { setUrl("https://maven.pkg.jetbrains.space/public/p/compose/dev") }
maven { setUrl("https://maven.pkg.jetbrains.space/kotlin/p/wasm/experimental") }
// maven { setUrl("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev") }
}
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ android.enableJetifier=false
org.gradle.unsafe.configuration-cache=false
org.jetbrains.compose.experimental.macos.enabled=true
org.jetbrains.compose.experimental.uikit.enabled=true
org.jetbrains.compose.experimental.jscanvas.enabled=true

android.suppressUnsupportedCompileSdk=34
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ dokka = "1.9.10"
kotlinCore = { require = "1.9.20" }
kotlinCoroutines = { require = "1.7.3" }
kotlinxSerialization = "1.6.0"
kotlinxCollections = "0.3.6"
kotlinxCollections = "0.4-wasm1"
# compose
compose = "1.5.4"
composeUi = "1.5.4" # foundation / material
composeCompiler = "1.5.4-dev-k1.9.20-50f08dfa4b4"
composejb = "1.5.10" # "1.4.0-dev-wasm09"
composejb = "1.5.10-dev-wasm02" # "1.4.0-dev-wasm09"
composeCompilerJb = "1.5.3"
# androidx
activity = "1.8.0"
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ include ':aboutlibraries-compose'

include ':app'
include ':app-desktop'
include ':app-wasm'

includeBuild("plugin-build") {
dependencySubstitution {
Expand Down

0 comments on commit c1445ee

Please sign in to comment.