https://github.com/joyofrails/wasm-demo-app
Demo Rails app that can be booted in the browser
https://github.com/joyofrails/wasm-demo-app
Last synced: 4 months ago
JSON representation
Demo Rails app that can be booted in the browser
- Host: GitHub
- URL: https://github.com/joyofrails/wasm-demo-app
- Owner: joyofrails
- License: bsd-3-clause
- Created: 2024-03-26T12:08:58.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-03-31T14:11:32.000Z (about 1 year ago)
- Last Synced: 2025-01-11T21:33:45.392Z (6 months ago)
- Language: Ruby
- Homepage:
- Size: 56.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WASM Demo App
This Rails application is intended to serve as a demo app for joyofrails.com. It is designed to compile to WebAssembly so that it can be loaded directly in the browser.
## Building the WASM version
These steps are adapted from https://github.com/palkan/turbo-music-drive/compare/main...spike/wasmify
The minimal WASM version of the app can be built as follows:
- Install [wasi-vfs](https://github.com/kateinoigakukun/wasi-vfs):
```sh
brew tap kateinoigakukun/wasi-vfs https://github.com/kateinoigakukun/wasi-vfs.git
brew install kateinoigakukun/wasi-vfs/wasi-vfs
```- Install Rust toolchain:
```sh
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```- Build a WASM module with Ruby and all the app's dependencies (from the project's root):
```sh
bin/rbwasm build --ruby-version 3.3 -o .wasm/ruby.wasm
```Now you should be able to run a test script (performing an HTTP/Rack request) using [wasmtime](https://github.com/bytecodealliance/wasmtime) like this (from the project's root):
```sh
wasmtime run --dir ./::/ .wasm/ruby.wasm -e "$(cat .wasm/test.rb)"
```## Packaging
To pack the whole app into a single `.wasm` module (and avoid mounting files), you can use `wasi-vfs`:
```sh
bin/rbwasm pack .wasm/ruby.wasm \
--dir ./app::/app \
--dir ./config::/config \
--dir ./db::/db \
--dir ./vendor::/vendor \
--dir ./public::/public \
--dir ./lib::/lib \
-o .wasm/rails.wasm
```You can now verify it as follows:
```sh
wasmtime run .wasm/rails.wasm -e "$(cat .wasm/test.rb)"
```### Web version
To run the app in the browser, we must compile it with the `js` gem included. For that, run the following commands:
```sh
JS=true bin/rbwasm build --ruby-version 3.3 -o .wasm/ruby-web.wasm
```And now pack the app:
```sh
bin/rbwasm pack .wasm/ruby-web.wasm \
--dir ./app::/app \
--dir ./config::/config \
--dir ./db::/db \
--dir ./vendor::/vendor \
--dir ./public::/public \
--dir ./lib::/lib \
-o .wasm/rails-web.wasm
```