mirror of
https://git.pumpin.online/funkpower/typikon.git
synced 2026-10-22 18:57:13 +00:00
WebAssembly rather than a native Node addon: one artifact serves Node, Deno, Bun, a browser and anything built on them, where napi would have meant a prebuilt binary per platform and a source build for whoever the matrix missed. The engine is pure computation and the menologion is already compiled in, so there is nothing for it to give up. A day comes back as a plain object, not a handle. A wasm class would have meant a .free() that every caller eventually forgets, and this way JSON.stringify works and the thing spreads into state. Two serializer defaults had to be overridden to keep the promise that it is the same document --json emits: maps as objects, and an absent field as null rather than undefined, which stringify would otherwise drop entirely. The TypeScript is hand-written, because the generated types for a value crossing as JsValue are `any`, and a binding that types its whole return as `any` is a binding TypeScript cannot check. tsc --strict is a release gate now, the way mypy --strict already is, and the check includes three lines that must fail to compile. The one place this differs from every other binding: no remembered default jurisdiction, because a browser has nowhere to keep one. It is called out in the README, in the types and in docs/versioning.md. The test resolves typikon through a staged node_modules rather than a relative path, so what it exercises is the exports map, and it compares a year of days field by field against the command line for four jurisdictions.
56 lines
2.1 KiB
Bash
Executable File
56 lines
2.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Build the npm package into npm/dist.
|
|
#
|
|
# npm/build.sh # needs wasm-bindgen-cli at the version below
|
|
# WASM_BINDGEN=/path/to/wasm-bindgen npm/build.sh
|
|
#
|
|
# Two builds go in: `node/` for Node, which loads the module itself, and
|
|
# `web/` for browsers and bundlers, which cannot compile WebAssembly
|
|
# synchronously on the main thread and so has to be awaited. The exports map
|
|
# in package.json picks between them.
|
|
set -euo pipefail
|
|
|
|
root="$(cd "$(dirname "$0")/.." && pwd)"
|
|
cd "$root"
|
|
|
|
version="$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -1)"
|
|
bindgen="${WASM_BINDGEN:-wasm-bindgen}"
|
|
dist="npm/dist"
|
|
|
|
command -v "$bindgen" >/dev/null || {
|
|
echo "wasm-bindgen is not on PATH. cargo install wasm-bindgen-cli --version \
|
|
$(sed -n 's/^wasm-bindgen = { version = "\(.*\)".*/\1/p' Cargo.toml)" >&2
|
|
exit 1
|
|
}
|
|
|
|
cargo build --lib --release --target wasm32-unknown-unknown --features wasm
|
|
|
|
rm -rf "$dist"
|
|
for target in nodejs web; do
|
|
out="$dist/$([ "$target" = nodejs ] && echo node || echo web)"
|
|
"$bindgen" --target "$target" --out-dir "$out" --out-name typikon \
|
|
target/wasm32-unknown-unknown/release/typikon.wasm
|
|
# wasm-bindgen writes an overridden parameter type without the optional
|
|
# marker, so an options argument nobody wants to pass has to be passed as
|
|
# `undefined`. The override is what gives the argument a real type at all;
|
|
# this restores the `?` it costs.
|
|
sed -i 's/options: Options/options?: Options/g' "$out/typikon.d.ts"
|
|
done
|
|
|
|
# wasm-opt, where it is to hand. Worth about a fifth of the file.
|
|
if command -v wasm-opt >/dev/null; then
|
|
for w in "$dist"/*/typikon_bg.wasm; do
|
|
wasm-opt -Oz -o "$w.opt" "$w" && mv "$w.opt" "$w"
|
|
done
|
|
fi
|
|
|
|
sed "s/@VERSION@/$version/" npm/package.json.in > "$dist/package.json"
|
|
# The package is ESM; the Node build is what wasm-bindgen emits for Node, which
|
|
# is CommonJS. One file says so for that directory alone.
|
|
echo '{ "type": "commonjs" }' > "$dist/node/package.json"
|
|
cp npm/README.md "$dist/README.md"
|
|
cp LICENSE "$dist/LICENSE"
|
|
|
|
echo "built $dist, version $version"
|
|
du -h "$dist"/*/typikon_bg.wasm
|