Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Support uploading a pre-built WASM module #1716

Closed
GregBrimble opened this issue Jan 11, 2021 · 8 comments
Closed

Support uploading a pre-built WASM module #1716

GregBrimble opened this issue Jan 11, 2021 · 8 comments
Labels
feature Feature requests and suggestions

Comments

@GregBrimble
Copy link
Member

💡 Feature request

Overview and problem statement

Right now, wrangler supports the building a WASM module as part of the build and upload process with type = "rust", but does not support uploading a pre-built WASM module. I propose adding a new key to the wrangler configuration to allow developers to specify the filepath location of any modules they want to bind (similar to how they already specify the webpack_config location). I imagine it would work similar to kv_namespaces.

Basic example

# wrangler.toml
name = "$SCRIPT_NAME"
type = "webpack"
account_id = "$ACCOUNT_ID"
workers_dev = true

# Just as webpack config files are located like this,
webpack_config = "./webpack.config.js"

# and KV namespaces are configured in an array like this,
kv_namespaces = [
  { binding = "FOO", id = "0f2ac74b498b48028cb68387c421e279", preview_id = "6a1ddb03f3ec250963f0a1e46820076f" },
  { binding = "BAR", id = "068c101e168d03c65bddf4ba75150fb0", preview_id = "fb69528dbc7336525313f2e8c3b17db0" }
]

# WASM modules could be configured as follows:
wasm_modules = [
  { binding = "MYWASM", location = "./my_wasm_file.wasm" }
]

Wrangler would simply have to locate the file, reference it in the metadata, and upload it alongside the script as a part of the PUT call to upload a new worker.

@GregBrimble GregBrimble changed the title Support uploading a pre-build WASM module Support uploading a pre-built WASM module Jan 11, 2021
@koeninger
Copy link
Contributor

Have you seen #1677

@GregBrimble
Copy link
Member Author

Ah, sweet! Didn't realize that PR will solve this too. Thanks for highlighting that, @koeninger.

@xtuc
Copy link
Member

xtuc commented Jan 18, 2021

@GregBrimble since you are using the webpack type, https://github.com/cloudflare/worker-emscripten-template/blob/master/webpack.config.js#L24-L31 should fix your issue. It needs to be located at a specific path to get uploaded by wrangler. I don't think #1677 will fix that.

@GregBrimble
Copy link
Member Author

Amazing—thanks for that example, @xtuc! If #1677 doesn't address it, and there's interest for what I'm proposing, I'd be happy to try make a PR. Anyone on the wrangler team able to say if that's something they'd like to support?

@xortive xortive added the feature Feature requests and suggestions label Feb 25, 2021
@xortive
Copy link
Contributor

xortive commented Feb 25, 2021

#1677 does in fact support uploading pre-built wasm modules.

@xortive xortive closed this as completed Feb 25, 2021
@sepbot
Copy link

sepbot commented Apr 10, 2021

#1677 does in fact support uploading pre-built wasm modules.

how do we upload pre-built wasm modules? It's not clear from the PR and not documented anywhere...

Using the API directly I can put this in the metadata.json and refer to WASM global variable (not using modules)

{
    "type": "wasm_module",
    "name": "WASM",
    "part": "wasm"
  }

How does one declare this in wrangler.toml when using modules?

I have tried arbitrarily throwing the pre-built WASM module in folder declared build.upload_dir (when build.upload_format = "modules") and I end up with this error:

 {
  "result": null,
  "success": false,
  "errors": [
    {
      "code": 10013,
      "message": "workers.api.error.unknown"
    }
  ],
  "messages": []
}

If I remove the WASM file from the above it succeeds, if I upload the same file to a non-module worker it still succeeds.

Some description or an example indicating how it's supposed to work would be appreciated.

@jmonster
Copy link

jmonster commented Jun 1, 2021

@xortive @koeninger would either of you happen to know the answer to @sepbot 's last question?

I've been scouring the web trying to figure out how (if at all?) I can use ffmpeg.wasm or ffmpeg.js in a worker


It seems like it should just work by importing it like anything else? but wrangler dev pegs the CPU at 100% for along while and then crashes with JS heap out of memory. This seems to perhaps be a webpack issue rather than a wrangler one -- but if someone could confirm? :)

@robertaboukhalil
Copy link
Contributor

robertaboukhalil commented Oct 1, 2021

In case it helps, setting wasm_modules = { MYWASM = "./something.wasm" } in wrangler.toml seems to work for me (see example here)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Feature requests and suggestions
Projects
None yet
Development

No branches or pull requests

8 participants