This library provides two different approaches for loading and working with WebAssembly modules in modern web applications using Svelte 5. It's designed to be used as a git submodule in other projects.
The ModernWasm library supports two distinct WASM loading approaches:
ModernWasm/
├── wasi-loader.svelte.ts # WASI WASM loader (@wasmer/sdk)
├── wasm-loader.svelte.ts # Legacy Go runtime loader
├── wasm-exec.svelte.ts # Go wasm_exec.js wrapper
├── go-runtime.svelte.ts # Go runtime management
├── go-runtime.worker.ts # Web Worker for Go runtime
├── debugLogger.svelte.ts # Shared debug logging
└── README.md # This file
@wasmer/sdk
for WASI supportwasi-loader.svelte.ts
- Main WASI loader with Svelte 5 runesdebugLogger.svelte.ts
- Shared logging utilitiesimport { loadWasiWasm, callWasiFunction, wasiLoaderState } from './wasi-loader.svelte';
// Load a WASI WASM module
const success = await loadWasiWasm('/path/to/module.wasm');
if (success && wasiLoaderState.isReady) {
// Call exported functions
const result = callWasiFunction('YourExportedFunction', arg1, arg2);
// Access exports directly
const exports = wasiLoaderState.exports;
}
GOOS=wasip1 GOARCH=wasm go build -o module.wasm main.go
//go:wasmexport YourFunction
func YourFunction(param1, param2 int32) int32 {
// Your implementation
return result
}
wasm-loader.svelte.ts
- Main legacy loader with Svelte 5 runeswasm-exec.svelte.ts
- Go wasm_exec.js wrappergo-runtime.svelte.ts
- Go runtime lifecycle managementgo-runtime.worker.ts
- Web Worker for Go runtimedebugLogger.svelte.ts
- Shared logging utilitiesimport { loadWasm, wasmLoaderState } from './wasm-loader.svelte';
// Load a Go WASM module
const success = await loadWasm('/path/to/calculator.wasm');
if (success && wasmLoaderState.isReady) {
// Access functions via exports
const exports = wasmLoaderState.exports;
const result = exports.YourFunction(arg1, arg2);
// Or access data via globalThis (if exposed by Go code)
const data = (globalThis as any).yourData;
}
GOOS=js GOARCH=wasm go build -o calculator.wasm main.go
import "syscall/js"
func main() {
// Register functions on global object
js.Global().Set("YourFunction", js.FuncOf(yourFunctionWrapper))
// Keep the program running
select {}
}
{
"@wasmer/sdk": "^0.9.0"
}
Experimental Svelte 5 version of wasm_exec.js
. Simulates the Go runtime in the browser and does not require the actual wasm_exec.js
from your Go installation.
⚠️ Note: This approach is experimental and may not work correctly for all Go WASM modules. Considerable effort was spent attempting to get a modern Svelte 5-compatible version of wasm_exec.js
working, but results may vary and some features may be incomplete or unreliable.
Both approaches provide:
$state
Both loaders expose reactive state:
// WASI approach
import { wasiLoaderState } from './wasi-loader.svelte';
// Legacy approach
import { wasmLoaderState } from './wasm-loader.svelte';
// Both provide similar reactive interface
$effect(() => {
if (loaderState.isReady) {
console.log('WASM module loaded successfully');
console.log('Available exports:', Object.keys(loaderState.exports));
}
if (loaderState.hasError) {
console.error('WASM loading failed:', loaderState.error);
}
});
To use this library in another project:
Add as submodule:
git submodule add <repo-url> src/lib/ModernWasm
Install dependencies:
# For WASI approach
npm install @wasmer/sdk
# For legacy approach, copy wasm_exec.js from Go installation
Import and use:
// WASI approach
import { loadWasiWasm } from '$lib/ModernWasm/wasi-loader.svelte';
// Legacy approach
import { loadWasm } from '$lib/ModernWasm/wasm-loader.svelte';
When contributing to this library:
MIT License - See LICENSE file for details.
Reusable Svelte 5 version of wasm.js