Step-by-step: add a new module
-
Create a new directory:
src/wasm/modules/<module-name>/.- Don't be daft - make sure to replace
<module-name>with the actual name your module should be given.
- Don't be daft - make sure to replace
-
Add
src/,include/and aMakefilethat obeys the root Makefile contract (targets:build,debug,clean). -
Make sure the module's
Makefilewrites output tobuild/withindex.jsandindex.wasm(the repo's alias generator expectsmodules/{name}/build). -
vite.config.jswill automatically find the module and create an alias@wasm-<module-name>on nextvitestart (or rebuild of the config). Example usage:import init from '@wasm-<module-name>/index.js';
await init(); -
Commit the
Makefileand source files; do not commitbuild/artifacts unless you want to vendor the WASM for static hosting without building.
Minimal module Makefile (copy/paste)
EMCC ?= emcc
SRC = $(wildcard src/*.cpp)
OUT_DIR = build
.PHONY: build debug clean
build:
mkdir -p $(OUT_DIR)
$(EMCC) $(SRC) -o $(OUT_DIR)/index.js -O3 -s WASM=1 --bind
debug:
mkdir -p $(OUT_DIR)
$(EMCC) $(SRC) -o $(OUT_DIR)/index.js -g -O0 -s ASSERTIONS=1 --bind
clean:
rm -rf $(OUT_DIR)