Deploying Rust and WebAssembly
At this point in time deploying Rust and WebAssembly to the web or otherlocations unfortunately isn't a trivial task to do. This page hopes to serveas documentation for the various known options, and as always PRs are welcometo update this if it's out of date!
The methods of deployment and integration here are primarily tied to the--target
flag.
Value | Summary |
---|---|
bundler | Suitable for loading in bundlers like Webpack |
web | Directly loadable in a web browser |
nodejs | Loadable viarequire as a Node.js CommonJS module |
deno | Loadable using imports from Deno modules |
no-modules | Likeweb , but older and doesn't use ES modules |
experimental-nodejs-module | Loadable viaimport as a Node.js ESM module. |
Bundlers
--target bundler
The default output ofwasm-bindgen
, or thebundler
target, assumes a modelwhere the Wasm module itself is natively an ES module. This model, however, is notnatively implemented in any JS implementation at this time. As a result, toconsume the default output ofwasm-bindgen
you will need a bundler of someform.
Note: the choice of this default output was done to reflect the trends ofthe JS ecosystem. While tools other than bundlers don't support Wasm files asnative ES modules today they're all very much likely to in the future!
Currently the only known bundler known to be fully compatible withwasm-bindgen
iswebpack. Mostexamples use webpack, and you can check outthehello world example online to see the details of webpack configurationnecessary.
Without a Bundler
--target web
or--target no-modules
If you're not using a bundler but you're still running code in a web browser,wasm-bindgen
still supports this! For this use case you'll want to use the--target web
flag. You can check out afull example in thedocumentation, but the highlights of this output are:
- When compiling you'll pass
--target web
towasm-bindgen
- The output can natively be included on a web page, and doesn't require anyfurther postprocessing. The output is included as an ES module.
- The
--target web
mode is not able to use NPM dependencies. - You'll want to review thebrowser requirements for
wasm-bindgen
becauseno polyfills will be available.
The CLI also supports an output mode called--target no-modules
which issimilar to theweb
target in that it requires manual initialization of thewasm and is intended to be included in web pages without any furtherpostprocessing. See thewithout a bundler example for some moreinformation about--target no-modules
.
Node.js
--target nodejs
If you're deploying WebAssembly into Node.js (perhaps as an alternative to anative module), then you'll want to pass the--target nodejs
flag towasm-bindgen
.
Like the "without a bundler" strategy, this method of deployment does notrequire any further postprocessing. The generated JS shims can berequire
'djust like any other Node module (even the*_bg
Wasm file can berequire
'das it has a JS shim generated as well).
Note that this method requires a version of Node.js with WebAssembly support,which is currently Node 8 and above.
Node.js Module
--target experemintal-nodejs-module
If you're deploying WebAssembly into Node.js as a JavaScript module,then you'll want to pass the--target experimental-nodejs-module
flag towasm-bindgen
.
Like the "node" strategy, this method of deployment does notrequire any further postprocessing. The generated JS shims can beimport
edjust like any other Node module.
Note that this method requires a version of Node.js with WebAssembly and module support,which is currently Node 12 and above.
Currently experimental. Target is expected to be changed before stabilization.
Deno
--target deno
To deploy WebAssembly to Deno, use the--target deno
flag.To then import your module inside deno, use
// @deno-types="./out/crate_name.d.ts"import { yourFunction } from "./out/crate_name.js";
NPM
If you'd like to deploy compiled WebAssembly to NPM, then the tool for the jobiswasm-pack
. More information on this coming soon!