Skip to content

Commit fd85f79

Browse files
committed
Adopt multi-module compose wasm compilation
1 parent d17cdee commit fd85f79

File tree

6 files changed

+54
-196
lines changed

6 files changed

+54
-196
lines changed

examples.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -144,45 +144,48 @@ You can use Compose Wasm.
144144
<div class="kotlin-code" data-target-platform="compose-wasm">
145145

146146
```kotlin
147-
import androidx.compose.ui.ExperimentalComposeUiApi
148-
import androidx.compose.ui.window.CanvasBasedWindow
149147
import androidx.compose.animation.AnimatedVisibility
150-
import androidx.compose.foundation.Image
151148
import androidx.compose.foundation.layout.Column
149+
import androidx.compose.foundation.layout.fillMaxSize
152150
import androidx.compose.foundation.layout.fillMaxWidth
153151
import androidx.compose.material.Button
154152
import androidx.compose.material.MaterialTheme
155153
import androidx.compose.material.Text
156-
import androidx.compose.runtime.Composable
157-
import androidx.compose.runtime.getValue
158-
import androidx.compose.runtime.mutableStateOf
159-
import androidx.compose.runtime.remember
160-
import androidx.compose.runtime.setValue
154+
import androidx.compose.runtime.*
161155
import androidx.compose.ui.Alignment
156+
import androidx.compose.ui.ExperimentalComposeUiApi
162157
import androidx.compose.ui.Modifier
158+
import androidx.compose.ui.window.ComposeViewport
159+
import kotlinx.browser.document
163160

164161
//sampleStart
165162
@OptIn(ExperimentalComposeUiApi::class)
166163
fun main() {
167-
CanvasBasedWindow { App() }
164+
ComposeViewport(viewportContainer = document.body!!, content = {
165+
App()
166+
})
168167
}
169168

170169
@Composable
171170
fun App() {
172171
MaterialTheme {
173-
var greetingText by remember { mutableStateOf("Hello World!") }
174-
var showImage by remember { mutableStateOf(false) }
175-
var counter by remember { mutableStateOf(0) }
176-
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
177-
Button(onClick = {
178-
counter++
179-
greetingText = "Compose: ${Greeting().greet()}"
180-
showImage = !showImage
181-
}) {
182-
Text(greetingText)
172+
var showContent by remember { mutableStateOf(false) }
173+
Column(
174+
modifier = Modifier
175+
.fillMaxSize(),
176+
horizontalAlignment = Alignment.CenterHorizontally,
177+
) {
178+
Button(onClick = { showContent = !showContent }) {
179+
Text("Click me!")
183180
}
184-
AnimatedVisibility(showImage) {
185-
Text(counter.toString())
181+
AnimatedVisibility(showContent) {
182+
val greeting = remember { Greeting().greet() }
183+
Column(
184+
modifier = Modifier.fillMaxWidth(),
185+
horizontalAlignment = Alignment.CenterHorizontally,
186+
) {
187+
Text("Compose: $greeting")
188+
}
186189
}
187190
}
188191
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"copy-examples": "node utils/copy-examples",
8181
"release:ci": "rm -rf dist && npm run build:all && $NPM_TOKEN=%env.NPM_TOKEN% npm publish",
8282
"start": "webpack-dev-server --port 9002",
83-
"start-with-local-compiler": "webpack-dev-server --port 9002 --env webDemoUrl='//localhost:8080' webDemoResourcesUrl='//localhost:8081'",
83+
"start-with-local-compiler": "webpack-dev-server --port 9002 --env webDemoUrl='http://localhost:8080' webDemoResourcesUrl='http://localhost:8081'",
8484
"lint": "eslint . --ext .ts",
8585
"fix": "eslint --fix --ext .ts .",
8686
"test": "npm run build:all && npm run test:run",

src/config.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ export const RUNTIME_CONFIG = { ...getConfigFromElement(currentScript) };
1111
* @type {{COMPILE: string, COMPLETE: string, VERSIONS: string, JQUERY: string, KOTLIN_JS: string}}
1212
*/
1313
export const API_URLS = {
14-
server: (RUNTIME_CONFIG.server || __WEBDEMO_URL__).replace(/\/$/, ''),
15-
composeServer: 'https://compose-stage.sandbox.intellij.net'.replace(
14+
server: (__WEBDEMO_URL__ || RUNTIME_CONFIG.server).replace(/\/$/, ''),
15+
composeServer: (__WEBDEMO_URL__ || 'https://compose-stage.sandbox.intellij.net').replace(
1616
/\/$/,
1717
'',
1818
),
19+
composeResources: (__WEBDEMO_RESOURCES_URL__ || 'https://compose-stage.sandbox.intellij.net').replace(
20+
/\/$/, ''
21+
),
1922

2023
COMPILE(platform, version) {
2124
let url;
@@ -60,21 +63,6 @@ export const API_URLS = {
6063
get VERSIONS() {
6164
return `${this.server}/versions`;
6265
},
63-
RESOURCE_VERSIONS() {
64-
return `${this.composeServer}/api/resource/compose-wasm-versions`;
65-
},
66-
SKIKO_MJS(version) {
67-
return `${this.composeServer}/api/resource/skiko-${version}.mjs`;
68-
},
69-
SKIKO_WASM(version) {
70-
return `${this.composeServer}/api/resource/skiko-${version}.wasm`;
71-
},
72-
STDLIB_MJS(hash) {
73-
return `${this.composeServer}/api/resource/stdlib-${hash}.mjs`;
74-
},
75-
STDLIB_WASM(hash) {
76-
return `${this.composeServer}/api/resource/stdlib-${hash}.wasm`;
77-
},
7866
get JQUERY() {
7967
return `https://cdn.jsdelivr.net/npm/jquery@1/dist/jquery.min.js`;
8068
},

src/executable-code/executable-fragment.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -412,24 +412,15 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {
412412
targetPlatform,
413413
compilerVersion,
414414
);
415-
const additionalRequests = [];
416-
if (targetPlatform === TargetPlatforms.COMPOSE_WASM) {
417-
if (this.jsExecutor.stdlibExports) {
418-
additionalRequests.push(this.jsExecutor.stdlibExports);
419-
}
420-
}
421415

422-
Promise.all([
423-
WebDemoApi.translateKotlinToJs(
416+
WebDemoApi.translateKotlinToJs(
424417
this.getCode(),
425418
compilerVersion,
426419
targetPlatform,
427420
args,
428421
hiddenDependencies,
429-
),
430-
...additionalRequests,
431-
]).then(
432-
([state, ...additionalRequestsResults]) => {
422+
).then(
423+
(state) => {
433424
state.waitingForOutput = false;
434425
const jsCode = state.jsCode;
435426
const wasm = state.wasm;
@@ -454,7 +445,6 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {
454445
outputHeight,
455446
theme,
456447
onError,
457-
additionalRequestsResults,
458448
compilerVersion,
459449
)
460450
.then((output) => {

src/js-executor/execute-es-module.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1+
import {API_URLS} from "../config";
2+
13
export async function executeWasmCode(container, jsCode, wasmCode) {
24
const newCode = prepareJsCode(jsCode);
35
return execute(container, newCode, wasmCode);
46
}
57

6-
export async function executeWasmCodeWithSkiko(container, jsCode) {
7-
return executeJs(container, prepareJsCode(jsCode));
8-
}
9-
10-
export async function executeWasmCodeWithStdlib(container, jsCode, wasmCode) {
11-
return execute(container, prepareJsCode(jsCode), wasmCode);
12-
}
13-
148
function execute(container, jsCode, wasmCode) {
159
container.wasmCode = Uint8Array.from(atob(wasmCode), c => c.charCodeAt(0));
1610
return executeJs(container, jsCode);
1711
}
1812

19-
export function executeJs(container, jsCode) {
13+
function executeJs(container, jsCode) {
2014
return container.eval(`import(/* webpackIgnore: true */ '${'data:text/javascript;base64,' + btoa(jsCode)}');`)
2115
}
2216

2317
function prepareJsCode(jsCode) {
18+
const re = /instantiateStreaming\(fetch\(new URL\('([^']*)',\s*import\.meta\.url\)\.href\),\s*importObject\s*,\s*\{\s*builtins\s*:\s*\[''\]\s*\}\s*\)\)\.instance;/g;
19+
2420
return `
2521
class BufferedOutput {
2622
constructor() {
@@ -30,12 +26,20 @@ function prepareJsCode(jsCode) {
3026
export const bufferedOutput = new BufferedOutput()
3127
` +
3228
jsCode
29+
.replaceAll(
30+
"await import('./",
31+
"await import('" + API_URLS.composeResources + "/"
32+
)
33+
.replaceAll(
34+
"%3",
35+
"%253"
36+
)
3337
.replace(
3438
"instantiateStreaming(fetch(wasmFilePath), importObject)).instance;",
3539
"instantiate(window.wasmCode, importObject)).instance;\nwindow.wasmCode = undefined;"
3640
)
3741
.replace(
38-
"instantiateStreaming(fetch(new URL('./playground.wasm',import.meta.url).href), importObject)).instance;",
42+
re,
3943
"instantiate(window.wasmCode, importObject)).instance;\nwindow.wasmCode = undefined;"
4044
)
4145
.replace(

0 commit comments

Comments
 (0)