Skip to content

Commit

Permalink
feat: wasm and text file support for local mode in wrangler dev (#334)
Browse files Browse the repository at this point in the history
This adds support for `*.wasm` modules into local mode for `wrangler dev`.

In 'edge' mode, we create a javascript bundle, but wasm modules are uploaded to the preview server directly when making the worker definition form upload. However, in 'local' mode, we need to have the actual modules available to the bundle. So we copy the files over to the bundle path. We also pass appropriate `--modules-rule` directive to `miniflare`.

I also added a sample wasm app to use for testing, created from a default `workers-rs` project.

Fixes #299
  • Loading branch information
threepointone authored Feb 4, 2022
1 parent c6f5f02 commit 536c7e5
Show file tree
Hide file tree
Showing 10 changed files with 795 additions and 51 deletions.
13 changes: 13 additions & 0 deletions .changeset/swift-rings-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"wrangler": patch
---

feat: wasm support for local mode in `wrangler dev`

This adds support for `*.wasm` modules into local mode for `wrangler dev`.

In 'edge' mode, we create a javascript bundle, but wasm modules are uploaded to the preview server directly when making the worker definition form upload. However, in 'local' mode, we need to have the actual modules available to the bundle. So we copy the files over to the bundle path. We also pass appropriate `--modules-rule` directive to `miniflare`.

I also added a sample wasm app to use for testing, created from a default `workers-rs` project.

Fixes https://github.com/cloudflare/wrangler2/issues/299
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/example-wasm-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## example-wasm-app

This is a sample wasm worker. It was created by creating a [`workers-rs`](https://github.com/cloudflare/workers-rs) project, running a build, removing unneeded artifacts, and copying the output folder here. You can run this worker with `npx wrangler dev worker/shim.js` (or from the `wrangler` package directory with `npm start -- dev ../example-wasm-app/worker/shim.js`).
5 changes: 5 additions & 0 deletions packages/example-wasm-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "example-wasm-app",
"version": "1.0.0",
"module": "worker/shim.js"
}
10 changes: 10 additions & 0 deletions packages/example-wasm-app/worker/export_wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as index_bg from "./index_bg.js";
import _wasm from "./index_bg.wasm";

const _wasm_memory = new WebAssembly.Memory({ initial: 512 });
let importsObject = {
env: { memory: _wasm_memory },
"./index_bg.js": index_bg,
};

export default new WebAssembly.Instance(_wasm, importsObject).exports;
Loading

0 comments on commit 536c7e5

Please sign in to comment.