cloudflare-mbt-worker-bundle
Installation
SKILL.md
Cloudflare Workers + MoonBit bundle pipeline
The canonical way to ship a Cloudflare Worker where the bulk of the request handler is MoonBit code (compiled to JS via moon build --target js --release) but the entry shim is hand-written TypeScript.
When to invoke
Use when you're:
- Setting up a new MoonBit → Cloudflare Workers project.
- Migrating an existing project from a hand-written
dist/worker.mjsshim to wrangler-native TS bundling. - Diagnosing a Worker that hangs on the first
awaitafter startup (FFI rewrite missed; see references).
Key facts about wrangler + MoonBit
- wrangler bundles TS natively. As of wrangler 4.x,
main: "src/worker.ts"inwrangler.jsoncis enough — wrangler's built-in esbuild integration transpiles + bundles + emits a single JS file on everywrangler dev/wrangler deploy. No separatetscemit step is needed for the worker. - MoonBit output is plain ESM JS.
moon build --target js --releasewrites_build/js/release/build/<package>.js. wrangler canimportit from a TS entry — no bridging needed except for the side-effect import (the moon module registers globals at module init). - Two MoonBit-specific source rewrites are mandatory. These are not optional and cannot move into TS:
moonbitlang$async$internal$event_loop$$reschedule()→ the mangled_M0FP...event__loop10reschedule()name. The legacy hook only drains the deque once; the new name re-pumps viasetTimeout(0). Without the rewrite, everyawaitpast startup hangs.- Module-scope random seed → a constant. Workers reject random in module init.
Both must happen between
moon buildandwrangler deploy, applied to the moon JS output. Seeassets/scripts/prepare-worker.ts.template.