| 1 | # WebAssembly |
| 2 | |
| 3 | A Worker bundle can import a `.wasm` file. As in Wrangler, the import gives |
| 4 | the compiled module, not the bytes. |
| 5 | |
| 6 | ```js |
| 7 | import addModule from "./add.wasm"; |
| 8 | |
| 9 | const { exports } = new WebAssembly.Instance(addModule); |
| 10 | |
| 11 | export default { |
| 12 | fetch() { |
| 13 | return new Response(String(exports.add(2, 3))); |
| 14 | }, |
| 15 | }; |
| 16 | ``` |
| 17 | |
| 18 | `celld deploy` uploads each imported wasm file beside the bundle and marks the |
| 19 | deployment with the `wasm-v1` feature. A node that predates this feature |
| 20 | refuses the deployment, so a mixed fleet fails at deploy time, not at request |
| 21 | time. |
| 22 | |
| 23 | celld compiles each wasm module once per process, so later isolates and cell |
| 24 | activations reuse it. |
| 25 | |
| 26 | ## Example |
| 27 | |
| 28 | The [WebAssembly example](../examples/wasm) compiles a Rust Durable Object and |
| 29 | imports its WebAssembly module. |
| 30 | |
| 31 | <!-- celld-example: wasm --> |
| 32 | |
| 33 | ## Prebuilt Workers |
| 34 | |
| 35 | With `no_bundle: true`, celld preserves the entry JavaScript byte for byte. It |
| 36 | applies Wrangler's default `**/*.wasm` and `**/*.wasm?module` patterns below |
| 37 | the directory that contains `main`. For example, `main: "./dist/shim.mjs"` can |
| 38 | import `"./add.wasm"` or `"./lib/add.wasm"` from `dist`. The module names keep |
| 39 | these relative paths. This mode does not require esbuild. |
| 40 | |
| 41 | The scan uploads WASM files that the JavaScript does not import, so use a |
| 42 | dedicated build output directory. It skips `.git`, `.celld`, `.wrangler`, and |
| 43 | symbolic links to directories. It refuses a symbolic link whose name matches a |
| 44 | WASM pattern, so copy the file into the build output instead. |
| 45 | |
| 46 | celld does not implement the other |
| 47 | [Wrangler module discovery settings](https://developers.cloudflare.com/workers/wrangler/configuration/#find-additional-modules). |
| 48 | It does not discover additional JavaScript modules and does not accept |
| 49 | `rules`, `base_dir`, or `find_additional_modules`. The JavaScript must already |
| 50 | be bundled, and its WASM imports must be relative to the entry directory. |
| 51 | |
| 52 | ## Rust with workers-rs |
| 53 | |
| 54 | [workers-rs](https://github.com/cloudflare/workers-rs) builds a JavaScript shim |
| 55 | and a wasm file, and the shim is a normal entry point for `celld deploy`. |
| 56 | |
| 57 | 1. Install the build tool: `cargo install worker-build`. |
| 58 | 2. Build the crate: `worker-build --release`. |
| 59 | 3. Point the config at the shim: |
| 60 | |
| 61 | ```jsonc |
| 62 | { |
| 63 | "name": "my-app", |
| 64 | "main": "./build/worker/shim.mjs", |
| 65 | "compatibility_date": "2026-01-01", |
| 66 | } |
| 67 | ``` |
| 68 | |
| 69 | 4. Deploy: `celld deploy`. |
| 70 | |
| 71 | celld resolves entrypoint and Durable Object classes through the shim's Proxy |
| 72 | wrapper. A workers-rs API that needs a runtime feature missing from |
| 73 | [Cloudflare compatibility](cloudflare-compat.md) does not work. |
| 74 | |
| 75 | ## Dynamic Workers |
| 76 | |
| 77 | Pass wasm to a dynamically loaded worker as `{ wasm: bytes }` in the `modules` |
| 78 | map, and the worker imports a compiled module. celld refuses bare bytes, as |
| 79 | workerd does. |
| 80 | |
| 81 | ```js |
| 82 | const worker = env.loader.load({ |
| 83 | compatibilityDate: "2025-01-01", |
| 84 | mainModule: "main.js", |
| 85 | modules: { |
| 86 | "main.js": `import m from "./add.wasm"; ...`, |
| 87 | "add.wasm": { wasm: wasmBytes }, |
| 88 | }, |
| 89 | }); |
| 90 | ``` |
| 91 | |
| 92 | ## Limits |
| 93 | |
| 94 | Wasm bytes count against the deployment size limits like JavaScript modules. A |
| 95 | module that does not compile fails the importing module with a |
| 96 | `WebAssembly.CompileError` that names the file. |