Skip to content

Commit

Permalink
docs: wrote js interop docs
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Jan 11, 2023
1 parent f22f05d commit be9dbc9
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/next/en-US/fundamentals/js-interop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Working with JS

When you're working with Perseus, you usually won't need to use any JavaScript whatsoever, since Perseus handles the only JS you need (the stuff that calls your Wasm bundle) automatically. However, sometimes you'll need to work with an external module, or you'll need to work with some other JS code that some other team has built, etc. In these cases, you'll need to work more directly with JS, which can be done through `wasm-bindgen`, which underlies Perseus. For more details on this process, take a look at [their documentation] on this.

A simple example of working with JS might be this, on the landing page of a hypothetical app:

```
{{#include ../../../examples/core/js_interop/src/templates/index.rs}}
````
The comments mostly explain this, but the main thing to understand from the perspective of Perseus as a framework is that `#[wasm_bindgen(module = "..")]` attribute, which can be basically considered magic, because you specify a file, and then that just ends up being hosted by Perseus, and everything *just works*.
Somewhat humorously, Perseus actually does next to nothing to make this happen, because it calls `wasm-bindgen` under the hood (which is also a CLI as well as a library), which automatically copies any JS files you references into a file Perseus puts in `dist/`. That's then served automatically by Perseus' server systems and appropriately relocated by the export systems. These files are called JS 'snippets', and are available at `/.perseus/snippets` (which you don't have to import or anything, it literally just works because of how Perseus hosts other files, namely `bundle.js`). Again, 95% of this is done by `wasm-bindgen`, not Perseus, and it's best to take a look at [their docs] on the subject.

0 comments on commit be9dbc9

Please sign in to comment.