How to include wasm file/binding with wranger publish?

I have a JS worker (type = javascript) and running wrangler publish will upload this script to the worker, great.

The worker calls a WebAssembly module, (a wasm file). I can upload this manually in the Worker editor on cloudflare, but I want the wrangler tool to upload this also. But I can’t see guidance on how to do this. If I publish, any wasm file I’ve uploaded is deleted. I just have the wasm file that I need including as part of my upload.

I don’t want wrangler to do any compilation, I’ve already done that elsewhere. It’s not a Rust project.

How can I tell the wrangler tool to include my wasm file and upload it with the specified name?

3 Likes

The blog article Let's build a Cloudflare Worker with WebAssembly and Haskell explains how to publish the worker through curl manually and add the wasm file there.
But indeed it would be nice if this was possible through wrangler.

curl -X PUT "https://api.cloudflare.com/client/v4/accounts/$CF_ACCOUNT_ID/workers/scripts/$SCRIPT_NAME" \
     -H  "Authorization: Bearer $CF_API_TOKEN" \
     -F "[email protected];type=application/json" \
     -F "[email protected];type=application/javascript" \
     -F "[email protected];type=application/wasm"

Also being able to use wrangler preview --watch would be cool.

1 Like

It seems like with newer versions of Wrangler you can define the following in the wrangler.toml file.

wasm_modules = { MYWASMBINDINGNAME = "./something.wasm" }

# Or when multiple modules
[wasm_modules]
SOMETHINGWASM = "./something.wasm"
OTHERWASM = "./other.wasm"

(See this GitHub Issue)

Then in your worker code you would reference the wasm module via a global variable.

const instance = new WebAssembly.Instance(MYWASMBINDINGNAME);
instance.exports.exported_func();

Note: I haven’t tested this, but this is the information I’ve discovered so far.