Skip to content

Instantly share code, notes, and snippets.

@JaneOri
Created March 11, 2019 12:11
Show Gist options
  • Save JaneOri/8e1fc13016faa0afa7f6647c896c5e1d to your computer and use it in GitHub Desktop.
Save JaneOri/8e1fc13016faa0afa7f6647c896c5e1d to your computer and use it in GitHub Desktop.
Load/Import webassembly modules with StealJS

Couldn't find a plugin to load web assembly modules so I wrote one. Posting it here in case anyone else needs it whenever:

./wasm.js:

exports.fetch = () => ""
exports.instantiate = function (load) {
  const loader = this
  return fetch(load.address).then(r => {
    const contentType = r.headers.get("content-type")
    if (contentType === "application/wasm") {
      return WebAssembly.instantiateStreaming(r)
    }
    // does the same thing but slightly slower and won't fail because of the MIME type check:
    return r.arrayBuffer().then(b => WebAssembly.instantiate(b))
  }).then(m => {
    return {
      deps: [],
      execute: () => loader.newModule(m)
    }
  })
}

then in your project:

import { instance, module } from "../src/myWebAssemblyModule.wasm!wasm"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment