| 1 |
# Without a Bundler |
| 2 |
|
| 3 |
[View full source code][code] |
| 4 |
|
| 5 |
[code]: https://github.com/rustwasm/wasm-bindgen/tree/master/examples/without-a-bundler |
| 6 |
|
| 7 |
This example shows how the `--target web` flag can be used load code in a |
| 8 |
browser directly. For this deployment strategy bundlers like Webpack are not |
| 9 |
required. For more information on deployment see the [dedicated |
| 10 |
documentation][deployment]. |
| 11 |
|
| 12 |
First, you'll need to add `web-sys` to your Cargo.toml. |
| 13 |
```toml |
| 14 |
[dependencies.web-sys] |
| 15 |
version = "0.3.4" |
| 16 |
features = [ |
| 17 |
'Document', |
| 18 |
'Element', |
| 19 |
'HtmlElement', |
| 20 |
'Node', |
| 21 |
'Window', |
| 22 |
] |
| 23 |
``` |
| 24 |
|
| 25 |
Then, let's take a look at the code and see how when we're using `--target web` |
| 26 |
we're not actually losing any functionality! |
| 27 |
|
| 28 |
```rust |
| 29 |
{{#include ../../../examples/without-a-bundler/src/lib.rs}} |
| 30 |
``` |
| 31 |
|
| 32 |
Otherwise the rest of the deployment magic happens in `index.html`: |
| 33 |
|
| 34 |
```html |
| 35 |
{{#include ../../../examples/without-a-bundler/index.html}} |
| 36 |
``` |
| 37 |
|
| 38 |
> **Note**: You cannot directly open `index.html` in your web browser due to [CORS][cors] |
| 39 |
> limitations. Instead, you can set up a quick development environment using |
| 40 |
> Python's built-in HTTP server: |
| 41 |
> ```sh |
| 42 |
> wasm-pack build --target web |
| 43 |
> python3 -m http.server 8080 |
| 44 |
> ``` |
| 45 |
> If you don't have Python installed, you can also use [miniserve][miniserve] which |
| 46 |
> is installable via Cargo: |
| 47 |
> ```sh |
| 48 |
> cargo install miniserve |
| 49 |
> miniserve . --index "index.html" -p 8080 |
| 50 |
> ``` |
| 51 |
|
| 52 |
And that's it! Be sure to read up on the [deployment options][deployment] to see |
| 53 |
what it means to deploy without a bundler. |
| 54 |
|
| 55 |
[cors]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS |
| 56 |
[miniserve]: https://crates.io/crates/miniserve |
| 57 |
[deployment]: ../reference/deployment.html |
| 58 |
|
| 59 |
## Using the older `--target no-modules` |
| 60 |
|
| 61 |
[View full source code][code-no-modules] |
| 62 |
|
| 63 |
[code-no-modules]: https://github.com/rustwasm/wasm-bindgen/tree/master/examples/without-a-bundler-no-modules |
| 64 |
|
| 65 |
The older version of using `wasm-bindgen` without a bundler is to use the |
| 66 |
`--target no-modules` flag to the `wasm-bindgen` CLI. |
| 67 |
|
| 68 |
While similar to the newer `--target web`, the `--target no-modules` flag has a |
| 69 |
few caveats: |
| 70 |
|
| 71 |
* It does not support [local JS snippets][snippets] |
| 72 |
* It does not generate an ES module |
| 73 |
* It does not support `--split-linked-modules` outside of a document, e.g. |
| 74 |
inside a worker |
| 75 |
|
| 76 |
With that in mind the main difference is how the wasm/JS code is loaded, and |
| 77 |
here's an example of loading the output of `wasm-pack` for the same module as |
| 78 |
above. |
| 79 |
|
| 80 |
```html |
| 81 |
{{#include ../../../examples/without-a-bundler-no-modules/index.html}} |
| 82 |
``` |
| 83 |
|
| 84 |
[snippets]: ../reference/js-snippets.html |