#!/usr/bin/env bash # Build every component: MoonBit -> core wasm -> component -> jco transpile. # scripts/build.sh [component-name ...] (default: all in components.txt) # Outputs: build/components/.wasm (+ .meta.json), build/jco// (JS host glue). # Components build independently: rebuilding one never touches another's output. set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)"; cd "$ROOT" export PATH="$ROOT/.tools/bin:$HOME/.moon/bin:$PATH" for t in moon wasm-tools; do command -v "$t" >/dev/null || { echo "error: $t not found (run scripts/setup-toolchain.sh; install MoonBit)" >&2; exit 1; } done JCO="$ROOT/host/node_modules/.bin/jco" [ -x "$JCO" ] || { echo "error: jco missing; run: (cd host && npm ci)" >&2; exit 1; } want=("$@") mkdir -p build/components build/jco wasm-tools component wit wit >/dev/null # the WIT package itself must parse echo ">> moon build (wasm, release)" moon build --target wasm --release 2>&1 | grep -v -E 'Warning|^\s*$|[│╭╰─]' || true [ "${PIPESTATUS[0]}" -eq 0 ] || { echo "error: moon build failed" >&2; exit 1; } while read -r name world; do case "$name" in ''|'#'*) continue;; esac if [ ${#want[@]} -gt 0 ] && [[ ! " ${want[*]} " =~ " $name " ]]; then continue; fi core="_build/wasm/release/build/components/$name/gen/gen.wasm" [ -f "$core" ] || { echo "error: $core missing" >&2; exit 1; } out="build/components/$name.wasm" wasm-tools component embed wit "$core" --world "$world" --encoding utf16 -o "build/components/$name.embed.wasm" wasm-tools component new "build/components/$name.embed.wasm" -o "$out" rm -f "build/components/$name.embed.wasm" wasm-tools validate --features component-model "$out" # Conformance: the component must be a valid implementation of its world. wasm-tools component targets --world "$world" wit "$out" >/dev/null # Capability manifest: which host interfaces the binary imports. imports="$(wasm-tools component wit "$out" | sed -n 's/^ import moonplasma:desktop\/\([a-z-]*\)@.*;/\1/p' | grep -v '^types$' | sort | paste -sd, - || true)" exports="$(wasm-tools component wit "$out" | sed -n 's/^ export moonplasma:desktop\/\([a-z-]*\)@.*;/\1/p' | sort | paste -sd, -)" printf '{"name":"%s","world":"%s","imports":[%s],"exports":[%s]}\n' "$name" "$world" \ "$(echo "$imports" | sed 's/\([a-z-]\+\)/"\1"/g')" "$(echo "$exports" | sed 's/\([a-z-]\+\)/"\1"/g')" \ > "build/components/$name.meta.json" rm -rf "build/jco/$name" "$JCO" transpile "$out" -o "build/jco/$name" --instantiation async --no-typescript --name "$name" >/dev/null echo "{\"type\":\"module\"}" > "build/jco/$name/package.json" printf ' %-14s %-16s %7s bytes imports=[%s]\n' "$name" "$world" "$(stat -c %s "$out")" "$imports" done < scripts/components.txt echo "ok"