-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path+page.svelte
46 lines (34 loc) · 1.38 KB
/
+page.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<script>
import { onMount } from 'svelte';
// Import wasm wasm component bytes as a url
// Make sure you first run:
// $ cargo component build --release
import wasmURL from '../../target/wasm32-wasi/release/demo.wasm?url';
// You could also use VIte's convenince loader to get imports as a string
// import importables from './importables.js?raw';
let whatSayYou = 'Standby, generating your bundle...';
let name = 'Also...';
onMount(async () => {
// You need to give the JavaScript code to `jco` so it can wire it up to the wasm component
let importableCode = `export function prnt(string) {
console.log('from importables func: ', string);
};`;
// Make sure you're in the Browser environment when importing the plugin
const { load } = await import('rollup-plugin-wit-component');
// Load the wasm component bytes as an array buffer
let wasmBytes = await fetch(wasmURL).then((res) => res.arrayBuffer());
let importables = [{ 'component:cargo-comp/imports': importableCode }];
// Load the wasm component + imports to get the exported module functions
let { hello, named } = await load(/* @vite-ignore */ wasmBytes, importables);
whatSayYou = hello('World');
console.log({ whatSayYou });
name = named.name();
});
</script>
<svelte:head>
<title>Rollup Plugin WIT Demo</title>
</svelte:head>
{#if whatSayYou}
<h1>{whatSayYou}</h1>
<span>{name}</span>
{/if}