+#
+# Optimizes the wasm with binaryen. It prefers a native `wasm-opt` with the
+# same version as the `binaryen` npm package: same output, but multi-threaded
+# and ~10x faster than the npm package, which is an emscripten (JS) build.
+# It looks for it in PATH and in target/binaryen (`pnpm setup` installs it
+# there). Without one, it uses the npm package.
+#
+# The last output is kept in target/wasm-opt and reused while the input does
+# not change.
+#
+# WASM_OPT_NPM=1 always runs the npm package, without the cache.
+set -euo pipefail
+
+SELF="$(cd "$(dirname "$0")" && pwd)/$(basename "$0")"
+cd "$(dirname "$0")/.."
+
+FLAGS=(
+ -O
+ --enable-reference-types
+ --enable-bulk-memory
+ --enable-nontrapping-float-to-int
+ --enable-sign-ext
+ --enable-mutable-globals
+)
+
+VERSION=$(node -p 'require("./node_modules/binaryen/package.json").version.split(".")[0]')
+
+# `pnpm run` and `node --run` put node_modules/.bin first in PATH, so skip it
+native_wasm_opt() {
+ local bin
+ while read -r bin; do
+ case "$bin" in */node_modules/*) continue ;; esac
+ if [[ "$("$bin" --version 2> /dev/null)" == *"(version_$VERSION)"* ]]; then
+ echo "$bin"
+ return
+ fi
+ done < <(
+ type -ap wasm-opt || true
+ echo "$PWD/target/binaryen/binaryen-version_$VERSION/bin/wasm-opt"
+ )
+}
+
+sha256() {
+ if command -v sha256sum > /dev/null; then sha256sum; else shasum -a 256; fi | cut -d' ' -f1
+}
+
+# The output only depends on the input, the binaryen version and the flags in
+# this file: reuse the last output when none of them changed
+CACHE_DIR=target/wasm-opt
+CACHE="$CACHE_DIR/$({ sha256 < "$1"; echo "$VERSION"; sha256 < "$SELF"; } | sha256).wasm"
+
+if [ "${WASM_OPT_NPM:-}" != "1" ] && [ -f "$CACHE" ]; then
+ echo " input did not change, reusing $CACHE"
+ cp "$CACHE" "$2"
+ exit 0
+fi
+
+NATIVE=""
+if [ "${WASM_OPT_NPM:-}" != "1" ]; then
+ NATIVE=$(native_wasm_opt)
+fi
+
+if [ -n "$NATIVE" ]; then
+ echo " native wasm-opt version_$VERSION ($NATIVE)"
+ "$NATIVE" "${FLAGS[@]}" "$1" -o "$2"
+else
+ if [ "${WASM_OPT_NPM:-}" != "1" ]; then
+ echo " native wasm-opt version_$VERSION not found, using the npm package (slower, run \`pnpm setup\`)"
+ fi
+ pnpm exec wasm-opt "${FLAGS[@]}" "$1" -o "$2"
+fi
+
+if [ "${WASM_OPT_NPM:-}" != "1" ]; then
+ rm -rf "$CACHE_DIR"
+ mkdir -p "$CACHE_DIR"
+ cp "$2" "$CACHE"
+fi
diff --git a/test/browser/bench.html b/test/browser/bench.html
index 729931c..0e8e385 100644
--- a/test/browser/bench.html
+++ b/test/browser/bench.html
@@ -66,7 +66,7 @@
Same patterns and input (test/__data__/input-text.txt) as
- bin/bench-compare.mjs. Both engines compile the pattern once, before the
+ bin/bench-cases.mjs. Both engines compile the pattern once, before the
benchmark. \w is ASCII in RegExp and Unicode in
RRegex. Hover a bar for its numbers.
diff --git a/test/browser/compare.mjs b/test/browser/compare.mjs
index e349183..47a187e 100644
--- a/test/browser/compare.mjs
+++ b/test/browser/compare.mjs
@@ -1,4 +1,4 @@
-// Browser version of bin/bench-compare.mjs: runs every case for base, head
+// Browser version of bin/bench-node.mjs: runs every case for base, head
// and the native RegExp, and reports the samples to `?report=` (used by
// bin/bench-browser.mjs) or as `globalThis.compareResults`.
//
diff --git a/test/browser/compare.worker.mjs b/test/browser/compare.worker.mjs
index c2e6242..4b5dd66 100644
--- a/test/browser/compare.worker.mjs
+++ b/test/browser/compare.worker.mjs
@@ -1,6 +1,6 @@
// Runs one case for both builds and the native RegExp, in the given order.
// Each case runs in a new worker, so every build starts with a clean wasm
-// memory (see bin/bench-compare.mjs).
+// memory (see bin/bench-node.mjs).
import { cases, nativeCases } from "../../bin/bench-cases.mjs"
import { measure } from "./measure.mjs"
diff --git a/tsdown.config.ts b/tsdown.config.ts
index 9a062bc..9e325a0 100644
--- a/tsdown.config.ts
+++ b/tsdown.config.ts
@@ -2,6 +2,10 @@ import { defineConfig, type UserConfig } from "tsdown";
// Every entry is one self-contained file in lib/ next to lib/rregex.wasm.
// bin/build.sh generates js/wasm/ (wasm-bindgen glue and metadata) first.
+//
+// commonjs, core (cjs) and cf export exactly what core exports, so their
+// declarations are the same as core.d.mts: bin/build.sh copies it instead of
+// generating them (`dts: false`).
const shared = {
outDir: "lib",
clean: false,
@@ -29,6 +33,7 @@ export default defineConfig([
entry: { commonjs: "js/node.ts" },
platform: "node",
format: "cjs",
+ dts: false,
outExtensions: ext(".cjs", ".d.cts"),
},
// Any runtime, the user loads the module
@@ -44,6 +49,7 @@ export default defineConfig([
entry: { core: "js/core.ts" },
platform: "neutral",
format: "cjs",
+ dts: false,
outExtensions: ext(".cjs", ".d.cts"),
},
// Browsers and bundlers
@@ -60,6 +66,7 @@ export default defineConfig([
entry: { cf: "js/workerd.ts" },
platform: "neutral",
format: "esm",
+ dts: false,
deps: { onlyBundle: false, neverBundle: [/\.wasm$/] },
outExtensions: ext(".mjs", ".d.mts"),
},