|
| 1 | +# Deploying Rust and WebAssembly |
| 2 | + |
| 3 | +At this point in time deploying Rust and WebAssembly to the web or other |
| 4 | +locations unfortunately isn't a trivial task to do. This page hopes to serve |
| 5 | +as documentation for the various known options, and as always PRs are welcome |
| 6 | +to update this if it's out of date! |
| 7 | + |
| 8 | +## Bundlers |
| 9 | + |
| 10 | +The default output of `wasm-bindgen` assumes a model where the wasm module |
| 11 | +itself is natively an ES module. This model, however, not natively implemented |
| 12 | +in any JS implementation at this time. As a result, to consume the default |
| 13 | +output of `wasm-bindgen` you will need a bundler of some form. |
| 14 | + |
| 15 | +> **Note**: the choice of this default output was done to reflect the trends of |
| 16 | +> the JS ecosystem. While tools other than bundlers don't support wasm files as |
| 17 | +> native ES modules today they're all very much likely to in the future! |
| 18 | +
|
| 19 | +Currently the only known bundler known to be fully compatible with |
| 20 | +`wasm-bindgen` is [webpack]. Most [examples] use webpack, and you can check out |
| 21 | +the [hello world example online] to see the details of webpack configuration |
| 22 | +necessary. |
| 23 | + |
| 24 | +[webpack]: https://webpack.js.org/ |
| 25 | +[examples]: ../examples/index.html |
| 26 | +[hello world example online]: ../examples/hello-world.html |
| 27 | + |
| 28 | +## Without a Bundler |
| 29 | + |
| 30 | +If you're not using a bundler but you're still running code in a web browser, |
| 31 | +`wasm-bindgen` still supports this! For this use case you'll want to use the |
| 32 | +`--no-modules` flag. You can check out a [full example][nomex] in the |
| 33 | +documentation, but the highlights of this output are: |
| 34 | + |
| 35 | +* When using `wasm-pack` you'll pass `--target no-modules`, and when using |
| 36 | + `wasm-bindgen` directly you'll pass `--no-modules`. |
| 37 | +* The output can natively be included on a web page, and doesn't require any |
| 38 | + further postprocessing. |
| 39 | +* The `--no-modules` mode is not able to use NPM dependencies nor local JS |
| 40 | + snippets (both currently [proposed][rfc1] [features][rfc2]) |
| 41 | +* You'll want to review the [browser requirements] for `wasm-bindgen` because |
| 42 | + no polyfills will be available. |
| 43 | + |
| 44 | +[nomex]: ../examples/without-a-bundler.html |
| 45 | +[rfc1]: https://github.com/rustwasm/rfcs/pull/6 |
| 46 | +[rfc2]: https://github.com/rustwasm/rfcs/pull/8 |
| 47 | +[browser requirements]: browser-support.html |
| 48 | + |
| 49 | +Despite these limitations almost all code today is compatible with |
| 50 | +`--no-modules`, but this area is actively being worked on to improve the |
| 51 | +experience so the experience here may be tweaked over time! |
| 52 | + |
| 53 | +## Node.js |
| 54 | + |
| 55 | +If you're deploying WebAssembly into Node.js (perhaps as an alternative to a |
| 56 | +native module), then you'll want to pass the `--target nodejs` flag to |
| 57 | +`wasm-pack` or the `--nodejs` flag to `wasm-bindgen`. |
| 58 | + |
| 59 | +Like the "without a bundler" strategy, this method of deployment does not |
| 60 | +require any further postprocessing. The generated JS shims can be `require`'d |
| 61 | +just like any other Node module (even the `*_bg` wasm file can be `require`'d |
| 62 | +as it has a JS shim generated as well). |
| 63 | + |
| 64 | +Note that this method requires a version of Node.js with WebAssembly support, |
| 65 | +which is currently Node 8 and above. |
| 66 | + |
| 67 | +## NPM |
| 68 | + |
| 69 | +If you'd like to deploy compiled WebAssembly to NPM, then the tool for the job |
| 70 | +is [`wasm-pack`]. More information on this coming soon! |
| 71 | + |
| 72 | +[`wasm-pack`]: https://rustwasm.github.io/wasm-pack/book/ |
0 commit comments