| 1 | # WebAssembly |
| 2 | |
| 3 | A Worker bundle can import a `.wasm` file. The import gives the |
| 4 | compiled module, not the bytes. This is the same rule that Wrangler |
| 5 | applies, so a bundle that runs on Cloudflare runs on celld without a |
| 6 | change. |
| 7 | |
| 8 | ```js |
| 9 | import addModule from "./add.wasm"; |
| 10 | |
| 11 | const { exports } = new WebAssembly.Instance(addModule); |
| 12 | |
| 13 | export default { |
| 14 | fetch() { |
| 15 | return new Response(String(exports.add(2, 3))); |
| 16 | }, |
| 17 | }; |
| 18 | ``` |
| 19 | |
| 20 | `celld deploy` finds each wasm import, uploads the file beside the |
| 21 | bundle, and marks the deployment with the `wasm-v1` feature. A node |
| 22 | that predates this feature refuses the deployment with a clear |
| 23 | message, so a mixed fleet fails at deploy time and not at request |
| 24 | time. |
| 25 | |
| 26 | celld compiles each wasm module once for the whole process. Every |
| 27 | isolate after the first one reuses the compiled module, so a cell |
| 28 | activation does not pay the compilation again. |
| 29 | |
| 30 | ## Prebuilt Workers |
| 31 | |
| 32 | With `no_bundle: true`, celld preserves the entry JavaScript byte for byte. |
| 33 | It applies Wrangler's default `**/*.wasm` and `**/*.wasm?module` patterns below |
| 34 | the directory that contains `main`. For example, `main: "./dist/shim.mjs"` can |
| 35 | import `"./add.wasm"` or `"./lib/add.wasm"` from `dist`. The module names keep |
| 36 | these relative paths. The files use the same WASM metadata and feature gate as |
| 37 | a bundled deploy. |
| 38 | |
| 39 | The default scan includes WASM files that the JavaScript does not import. Use |
| 40 | a dedicated build output directory, so celld does not upload unrelated WASM. |
| 41 | The scan excludes `.git`, `.celld`, `.wrangler`, and symbolic links to |
| 42 | directories. It refuses a symbolic link whose name matches a WASM pattern. |
| 43 | Copy the WASM file into the build output instead of linking to it or importing |
| 44 | it from a parent directory. |
| 45 | |
| 46 | This mode matches Wrangler's default WASM discovery, but it does not implement |
| 47 | all [Wrangler module discovery settings](https://developers.cloudflare.com/workers/wrangler/configuration/#find-additional-modules). |
| 48 | celld 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. This |
| 51 | mode does not require an esbuild executable. |
| 52 | |
| 53 | ## Rust with workers-rs |
| 54 | |
| 55 | [workers-rs](https://github.com/cloudflare/workers-rs) compiles a Rust |
| 56 | crate to a Worker. The tool `worker-build` produces a JavaScript shim |
| 57 | and a wasm file, and the shim is a normal entry point for |
| 58 | `celld deploy`. |
| 59 | |
| 60 | 1. Install the build tool: `cargo install worker-build`. |
| 61 | 2. Build the crate: `worker-build --release`. |
| 62 | 3. Point the config at the shim: |
| 63 | |
| 64 | ```jsonc |
| 65 | { |
| 66 | "name": "my-app", |
| 67 | "main": "./build/worker/shim.mjs", |
| 68 | "compatibility_date": "2026-01-01", |
| 69 | } |
| 70 | ``` |
| 71 | |
| 72 | 4. Deploy: `celld deploy`. |
| 73 | |
| 74 | The shim wraps its exports in a JavaScript Proxy, and celld resolves |
| 75 | entrypoint classes and Durable Object classes through that wrapper. |
| 76 | The runtime surface that the application can use is the surface in |
| 77 | [Cloudflare compatibility](cloudflare-compat.md); a workers-rs API |
| 78 | that maps to a missing runtime feature does not work. |
| 79 | |
| 80 | ## Dynamic Workers |
| 81 | |
| 82 | A dynamically loaded worker can also carry wasm. Pass the bytes in the |
| 83 | `modules` map; a `BufferSource` value becomes a compiled-module import |
| 84 | in the loaded worker. |
| 85 | |
| 86 | ```js |
| 87 | const worker = env.loader.load({ |
| 88 | mainModule: "main.js", |
| 89 | modules: { |
| 90 | "main.js": `import m from "./add.wasm"; ...`, |
| 91 | "add.wasm": wasmBytes, |
| 92 | }, |
| 93 | }); |
| 94 | ``` |
| 95 | |
| 96 | ## Limits |
| 97 | |
| 98 | The wasm bytes count against the deployment size limits, exactly as |
| 99 | JavaScript modules do. A wasm module that does not compile fails the |
| 100 | importing module with a `WebAssembly.CompileError` that names the |
| 101 | file. |