Merge pull request #246 from rustwasm/docs-fix

the infamous docs pr
master
ashley williams 7 years ago committed by GitHub
commit eb850773c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .travis.yml
  2. 50
      docs/index.html
  3. 188
      docs/installer/index.html
  4. 98
      docs/public/custom.css
  5. BIN
      docs/public/img/rustwasm.png
  6. BIN
      docs/public/img/wasm-ferris.png
  7. 14
      docs/src/SUMMARY.md
  8. 1
      docs/src/command/pack-and-publish.md
  9. 5
      docs/src/commands/build.md
  10. 7
      docs/src/getting-started/index.md
  11. 14
      docs/src/getting-started/installation.md
  12. 40
      docs/src/getting-started/project-setup/manual-setup.md
  13. 1
      docs/src/prerequisites.md
  14. 8
      docs/src/prerequisites/index.md
  15. 18
      docs/src/prerequisites/npm.md
  16. 28
      docs/src/prerequisites/rust.md
  17. 2
      docs/src/project-setup/index.md
  18. 86
      docs/src/project-setup/manual-setup.md
  19. 3
      docs/src/project-setup/using-a-template.md
  20. 78
      docs/src/setup.md
  21. BIN
      docs/src/tutorial/example-output.png
  22. 28
      docs/src/tutorial/getting-started.md
  23. 14
      docs/src/tutorial/index.md
  24. 4
      docs/src/tutorial/packaging-and-publishing.md
  25. 1
      docs/src/tutorial/template-deep-dive.md
  26. 90
      docs/src/tutorial/template-deep-dive/cargo-toml.md
  27. 16
      docs/src/tutorial/template-deep-dive/index.md
  28. 3
      docs/src/tutorial/template-deep-dive/src-lib-rs.md
  29. 3
      docs/src/tutorial/template-deep-dive/src-utils-rs.md
  30. 122
      docs/src/tutorial/using-your-library.md
  31. 1
      docs/src/tutorial/writing-a-rust-webassembly-library.md

@ -66,7 +66,7 @@ matrix:
provider: pages
skip-cleanup: true
github-token: *GH_TOKEN
local-dir: docs/book
local-dir: docs
keep-history: false
on:
branch: master

@ -0,0 +1,50 @@
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>wasm-pack</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" rel="stylesheet">
<link rel="stylesheet" href="./public/custom.css" />
</head>
<body>
<nav class="navbar">
<div class="container">
<ul class="navbar-list">
<li class="navbar-logo">
<a href="/">
<img src="./public/img/rustwasm.png">
</a>
</a>
<li class="navbar-item">
<a href="https://github.com/rustwasm/wasm-pack/issues/new/choose">File an Issue</a>
</li>
<li class="navbar-item">
<a href="/book">Documentation</a>
</li>
<li class="navbar-item">
<a href="/installer">Install</a>
</li>
</ul>
</div>
</nav>
<header>
<div class="container">
<div class="row">
<div class="seven columns" id="logo">
<img src="./public/img/wasm-ferris.png"alt="wasm ferris logo" />
<h1><code>wasm-pack</code></h1>
<h2>📦✨ your favorite rust -> wasm workflow tool!</h2>
</div>
<div class="five columns" id="installer">
<a class="button button-primary" href="/installer">✨ Install wasm-pack 0.4.2 ✨</a>
<p>24 July 2018 |
<a href="https://github.com/rustwasm/wasm-pack/releases/tag/v0.4.2">
Release Notes
</a>
</p>
</div>
</div>
</header>
</div>
</div>
<script type="text/javascript" src="installer/wasm-pack.js"></script>
</body>

@ -1,20 +1,13 @@
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>wasm-pack</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" rel="stylesheet">
<link rel="stylesheet" href="../public/custom.css"/>
<style>
body {
text-align: center;
margin: 100px;
font-size: 150%;
}
#main {
padding: 100px;
}
.instructions {
padding: 100px;
border: 1px solid black;
border: 1px solid #eee;
}
.winlink {
display: block;
@ -22,85 +15,102 @@
</style>
</head>
<body>
<nav class="navbar">
<div class="container">
<ul class="navbar-list">
<li class="navbar-logo">
<a href="/">
<img src="../public/img/rustwasm.png">
</a>
</a>
<li class="navbar-item">
<a href="https://github.com/rustwasm/wasm-pack/issues/new/choose">File an Issue</a>
</li>
<li class="navbar-item">
<a href="/book">Documentation</a>
</li>
<li class="navbar-item">
<a href="/installer">Install</a>
</li>
</ul>
</div>
</nav>
<section id="installer">
<img src="../public/img/wasm-ferris.png">
<h1>Install <code>wasm-pack</code></h1>
<div class="container">
<div id="platform-instructions-unix" style="display: none;">
<div class="curl">
<code>curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh</code>
<p>
You appear to be running a *nix system (Unix, Linux, MacOS). If not,
<a class="default-platform-button" href="#">display all supported installers</a>.
</p>
</div>
</div>
<div id='main'>
Install wasm-pack! A tool with a blurb here.
</div>
<div id="platform-instructions-unix" style="display: none;">
<pre>curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh</pre>
<p>
You appear to be running Unix. If not,
<a class="default-platform-button" href="#">display all supported installers</a>.
</p>
</div>
<div id="platform-instructions-win64" class="instructions" style="display: none;">
<p>
You appear to be running windows 64-bit, download and run
<a class='winlink' href="https://github.com/rustwasm/wasm-pack/releases/download/$VERSION/wasm-pack-init.exe">wasm-pack-init.exe</a>
then follow the onscreen
instructions.
</p>
<hr/>
<p>
If you're a Windows Subsystem for Linux user run the following in your
terminal, then follow the onscreen instructions to install wasm-pack.
</p>
<pre>curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh</pre>
<hr/>
<p>
You appear to be running Windows 64-bit. If not,
<a class="default-platform-button" href="#">
display all supported installers
</a>.
</p>
</div>
<div id="platform-instructions-unknown" class="instructions" style="display: none;">
<p>I don't recognize your platform.</p>
<p>
We would appreciate it if you
<a href="https://github.com/rustwasm/wasm-pack/issues/new">reported an issue</a>,
along with the following values:
</p>
<div>
<div>navigator.platform:</div>
<div id="nav-plat"></div>
<div>navigator.appVersion:</div>
<div id="nav-app"></div>
</div>
</div>
<div id="platform-instructions-default" class="instructions">
<div>
<p>
To install wasm-pack, if you are running Unix,<br/>
run the following in your terminal, then follow the onscreen
instructions.
</p>
<pre>curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh</pre>
</div>
<hr/>
<div>
<p>
If you are running Windows 64-bit,<br/>download and run
<a class='winlink' href="https://github.com/rustwasm/wasm-pack/releases/download/$VERSION/wasm-pack-init.exe">wasm-pack-init.exe</a>
then follow the onscreen instructions.
</p>
</div>
<hr/>
<div id="platform-instructions-win64" class="instructions" style="display: none;">
<p>
You appear to be running Windows 64-bit, download and run
<a class='winlink' href="https://github.com/rustwasm/wasm-pack/releases/download/$VERSION/wasm-pack-init.exe">wasm-pack-init.exe</a>
then follow the onscreen instructions.
</p>
<hr/>
<p>
If you're a Windows Subsystem for Linux user run the following in your
terminal, then follow the onscreen instructions to install wasm-pack.
</p>
<code>curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh</code>
<hr/>
<p>
You appear to be running Windows 64-bit. If not,
<a class="default-platform-button" href="#">
display all supported installers
</a>.
</p>
</div>
<div>
<p>
For all other platforms, run the following in your terminal:
</p>
<pre>cargo install wasm-pack</pre>
</div>
</div>
<div id="platform-instructions-unknown" class="instructions" style="display: none;">
<p>I don't recognize your platform.</p>
<p>
We would appreciate it if you
<a href="https://github.com/rustwasm/wasm-pack/issues/new">reported an issue</a>,
along with the following values:
</p>
<div>
<div>navigator.platform:</div>
<div id="nav-plat"></div>
<div>navigator.appVersion:</div>
<div id="nav-app"></div>
</div>
</div>
<script type="text/javascript" src="wasm-pack.js"></script>
<div id="platform-instructions-default" class="instructions">
<div>
<p>
To install wasm-pack, if you are running a *nix system (Unix, Linux, MacOS),<br/>
run the following in your terminal, then follow the onscreen
instructions.
</p>
<code>curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh</code>
</div>
<hr/>
<div>
<p>
If you are running Windows 64-bit,<br/>download and run
<a class='winlink' href="https://github.com/rustwasm/wasm-pack/releases/download/$VERSION/wasm-pack-init.exe">wasm-pack-init.exe</a>
then follow the onscreen instructions.
</p>
</div>
<hr/>
<div>
<p>
For all other platforms, run the following in your terminal:
</p>
<code>cargo install wasm-pack</code>
</div>
</div>
</div>
</section>
<script type="text/javascript" src="wasm-pack.js"></script>
</body>

@ -0,0 +1,98 @@
@import url('https://fonts.googleapis.com/css?family=Raleway');
.navbar {
display: block;
width: 100%;
height: 6.5rem;
background: #fff;
z-index: 99;
border-bottom: 1px solid #eee;
}
.navbar-list {
list-style: none;
margin-bottom: 0;
}
.navbar-item {
position: relative;
float: right;
margin-bottom: 0;
}
.navbar-logo {
position: relative;
float: left;
margin-bottom: 0;
}
.navbar-logo a img {
width: 50px;
}
.navbar-item a {
text-transform: uppercase;
font-size: 11px;
font-weight: 600;
letter-spacing: .2rem;
margin-right: 35px;
text-decoration: none;
line-height: 6.5rem;
color: #222;
}
#logo, #installer {
text-align: center;
}
#logo {
padding: 50px;
}
#logo img {
width: 80%;
}
#logo h1, h2 {
text-align: center;
}
#logo h2 {
font-size: 1.8rem;
letter-spacing: 0.01rem;
color: #222;
}
#installer a.button {
width: 100%;
height: 20%;
font-size: 1.8rem;
padding: 1.8rem;
margin: 10rem 0 1.8rem 0;
background-color: #6556EC;
border-color: #6556EC;
}
#installer img {
padding: 50px;
width: 20%;
}
.curl {
padding: 50px;
}
.curl p {
margin-top: 50px;
}
.curl code {
padding: 20px;
font-size: 2rem;
background-color: #000;
color: #fff;
}
a {
color: #6556EC;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 117 KiB

@ -3,17 +3,19 @@
- [Prerequisites](./prerequisites/index.md)
- [Rust](./prerequisites/rust.md)
- [npm](./prerequisites/npm.md)
- [Getting Started](./getting-started/index.md)
- [Installation](./getting-started/installation.md)
- [Project Setup](./getting-started/project-setup/index.md)
- [Using a Template](./getting-started/project-setup/using-a-template.md)
- [Manual Setup](./getting-started/project-setup/manual-setup.md)
- [Project Setup](./project-setup/index.md)
- [Using a Template](./project-setup/using-a-template.md)
- [Manual Setup](./project-setup/manual-setup.md)
- [Commands](./commands/index.md)
- [`init` (DEPRECATED)](./commands/init.md)
- [`build`](./commands/build.md)
- [`pack` and `publish`](./commands/pack-and-publish.md)
- [Tutorial](./tutorial/index.md)
- [Writing a Rust-WebAssembly Library](./tutorial/writing-a-rust-webassembly-library.md)
- [Getting Started](./tutorial/getting-started.md)
- [Template Deep Dive](./tutorial/template-deep-dive/index.md)
- [`Cargo.toml`](./tutorial/template-deep-dive/cargo-toml.md)
- [`src/lib.rs`](./tutorial/template-deep-dive/src-lib-rs.md)
- [`src/utils.rs`](./tutorial/template-deep-dive/src-utils-rs.md)
- [Packaging and Publishing](./tutorial/packaging-and-publishing.md)
- [Using your Library](./tutorial/using-your-library.md)
- [Contributing](./contributing.md)

@ -41,8 +41,9 @@ wasm-pack build --target nodejs
| Option | Description |
|-----------|-----------------------------------------------------------------------------------------------------------------|
| `nodejs` | Outputs JS that uses CommonJS modules, for use with a `require` statement. |
| `browser` | Outputs JS that uses ES6 modules, primarily for use with `import` statements and/or bundlers such as `webpack`. |
| `nodejs` | Outputs JS that uses CommonJS modules, for use with a `require` statement. `main` key in `package.json`. |
| `nomodules` | Outputs JS that use no modules. `browser` key in `package.json`. |
| `browser` | Outputs JS that uses ES6 modules, primarily for use with `import` statements and/or bundlers such as `webpack`. `module` key in `package.json`. `sideEffects: false` by default. |
## Scope

@ -1,7 +0,0 @@
# Getting Started
In this section, we'll teach you how to get `wasm-pack` installed and how to setup a project
to use `wasm-pack` with.
- [Installation](./installation.html)
- [Project Setup](./project-setup/index.html)

@ -1,14 +0,0 @@
## Installation
You can install `wasm-pack` using the following command:
```
cargo install wasm-pack
```
If you have already installed `wasm-pack` and want to install a newer version,
you can use the `--force` option, like this:
```
cargo install wasm-pack --force
```

@ -1,40 +0,0 @@
# Manual Setup
You can create a new Rust project named `my-lib` using this command.
```
cargo new --lib my-lib
```
The `--lib` flag specifies that the project is a library, which is important
because we will be calling this code from JavaScript.
#### Cargo.toml changes
You will need to add `wasm-bindgen` to your `Cargo.toml` in the dependencies
section. `wasm-bindgen` is a tool that facilitates interoperability between
wasm modules and JavaScript.
Next, add a `[lib]` section, with a new field named `crate-type` set to
`"cdylib"`. This specifies that the library is a C compatible dynamic library,
which helps `cargo` pass the correct flags to the Rust compiler when targeting
`wasm32`.
After making these changes, your `Cargo.toml` file should look something like
this:
```
[package]
name = "wasm-add"
version = "0.1.0"
authors = ["Michael Gattozzi <mgattozzi@gmail.com>"]
description = "Code used to demonstrate how to use wasm-pack"
license = "MIT/Apache-2.0"
repository = "https://github.com/mgattozzi/wasm-add"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen="0.2"
```

@ -1 +0,0 @@
# Prerequisites

@ -2,5 +2,9 @@
To run `wasm-pack` you'll need to have both Rust and npm installed and configured.
- [Rust](./rust.html)
- [npm](./npm.html)
- [Rust](/prerequisites/rust.html)
- [npm](/prerequisites/npm.html)
In the future, we intend to rewrite the npm registry client bits so that the need
for a Node runtime is eliminated. If you're excited about that work- you should
reach out to the maintainers and get involved!

@ -4,17 +4,27 @@ Currently, `wasm-pack` requires that you have npm installed to pack and publish
package. Longterm, this will be replaced by a Rust only version.
If you would rather use another package manager that interfaces with the npm registry
you may, however, the `pack` and `publish` commands depend on having npm installed.
you may, however, the `pack`, `publish`, and `login` commands wrap the npm CLI interface
and as a result require that npm be installed.
You can install [npm] by following [these instructions][npm-install-info].
[npm]: https://www.npmjs.com
### npm Account
Regardless of which package manager CLI tool you'l like to you, if you wish to publish
your package to the npm registry, or another npm-like registry, you'll need an npm
account.
Part of the `wasm-pack` workflow is to publish your package to the npm Registry.
Regardless of which package manager CLI tool you prefer, if you wish to publish
your package to the npm registry you'll need an npm account.
You can find information about signing up for npm [here][npm-signup-info].
If you'd rather not sign up for an account or publish a package to the regsitry, you can
use the [`npm link`] command to use your generated package locally, which we'll discuss
later in the [Tutorial].
[`npm link`]: https://docs.npmjs.com/cli/link
[npm-install-info]: https://www.npmjs.com/get-npm
[npm-signup-info]: https://www.npmjs.com/signup
[Tutorial]: ../tutorial/index.html

@ -1,31 +1,35 @@
# Rust
`wasm-pack` is a tool written in Rust, and distributed with `cargo`. As a result,
you'll need Rust and `cargo` to use `wasm-pack`.
`wasm-pack` is a Command Line Interface tool written in Rust, and distributed with `cargo`.
As a result, you'll need Rust and `cargo` to use `wasm-pack`.
To install Rust, visit this [page](https://www.rust-lang.org/en-US/install.html).
### Installing Rust and Cargo
You can be sure you have Rust and Cargo installed by running:
To install Rust, visit this [page](https://www.rust-lang.org/en-US/install.html), which will
walk you through installing Rust and `cargo` on your machine using a tool called `rustup`.
To confirm you have Rust and `cargo` installed, run:
```
rustc --version
cargo --version
```
### `nightly` Rust
### Rust Versions
`wasm-pack` depends on `wasm-bindgen` which currently requires Rust features that
have not yet been stabilized. As a result, you'll need to use a nightly version of
Rust to run `wasm-pack`.
`wasm-pack` depends on a library called `wasm-bindgen`. `wasm-bindgen` requires that you use
Rust 1.30.0 or higher. This version is currently only available on the `nightly` or `beta`
channels.
You can install the nightly channel by running:
To get the correct version of Rust, you'll use `rustup` a Rust version manager that comes
bundled with Rust. Run this command to install the latest Rust on the `beta` channel:
```
rustup install nightly
rustup install beta
```
You can configure rustup to always use `nightly` in a directory by running:
You can set your project directory to always use this version of Rust by running:
```
rustup override set nightly
rustup override set beta
```

@ -1,5 +1,7 @@
# Project Setup
In this section, how to setup a `wasm-pack` project.
There are a few things you need to do to setup a project for `wasm-pack`.
We strongly recommending [using a template], but you can also set the project
up [manually].

@ -0,0 +1,86 @@
# Manual Setup
This is not the recommended way to start a `wasm-pack` project! If you ended up
here by mistake, go check out our recommended project start, [Using A Template].
[Using A Template]: using-a-template.md
### Step 1: Create a New Rust Library Project
You can create a new Rust project named `my-lib` using this command.
```
cargo new --lib my-lib
```
The `--lib` flag specifies that the project is a library, which is important
because we will be calling this code from JavaScript.
### Step 2: Edit your `Cargo.toml` File
#### Add the `wasm-bindgen` dependency
You will need to add `wasm-bindgen` to your `Cargo.toml` in the dependencies
section. `wasm-bindgen` is a tool that facilitates interoperability between
wasm modules and JavaScript.
If you are coming from JavaScript, you might note that when we add the dependency
there is no `^` or `~` symbol- it looks like we're locking to the `0.2` version.
However, that's not the case! In Rust, the `^` is implied.
#### Add `crate-type`
Next, add a `[lib]` section, with a new field named `crate-type` set to
`"cdylib"`. This specifies that the library is a C compatible dynamic library,
which helps `cargo` pass the correct flags to the Rust compiler when targeting
`wasm32`.
After making these changes, your `Cargo.toml` file should look something like
this:
```toml
[package]
name = "hello-wasm
version = "0.1.0"
authors = ["Ashley Williams <ashley666ashley@gmail.com>"]
description = "babby's first wasm package"
license = "MIT/Apache-2.0"
repository = "https://github.com/ashleygwilliams/hello-wasm"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen="0.2"
```
### Step 3: Write some Rust!
Now that your crate is correctly configured in your `Cargo.toml` file, the only step
left to setup your project is to have some Rust code in your `src/lib.rs` file.
#### Browser Example
The template we have gives you a quick "Hello, World" project to use for compiling into
a WebAssembly library that you can use in the browser:
```rust
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, World!");
}
```
And that's it! We'll talk about what this code does in the [Tutorial], which you are all
setup for now. Happy `wasm-pack`ing!
[Tutorial]: ../tutorial/index.html

@ -15,6 +15,7 @@ cargo generate --git https://github.com/rustwasm/wasm-pack-template
```
You will be prompted to give your project a name. Once you do, you will have a directory
with a new project, ready to go.
with a new project, ready to go. We'll talk about what's been included in this template
further in this guide.
[rustwasm wasm-pack-template]: https://github.com/rustwasm/wasm-pack-template

@ -1,78 +0,0 @@
# Setup
## Installing wasm-pack
You can install `wasm-pack` using the following command:
```
cargo install wasm-pack
```
If you have already installed `wasm-pack` and want to install a newer version,
you can use the `--force` option, like this:
```
cargo install wasm-pack --force
```
## Project Initialization
### Using a Template
You can create a new Rust-WebAssembly project by using the [rustwasm wasm-pack-template].
To so do, you'll need the `cargo-generate` tool. To install `cargo-generate`:
```
cargo install cargo-generate
```
Then run:
```
cargo generate --git https://github.com/rustwasm/wasm-pack-template
```
You will be prompted to give your project a name. Once you do, you will have a directory
with a new project, ready to go.
### Manually
You can create a new Rust project named `my-lib` using this command.
```
cargo new --lib my-lib
```
The `--lib` flag specifies that the project is a library, which is important
because we will be calling this code from JavaScript.
#### Cargo.toml changes
You will need to add `wasm-bindgen` to your `Cargo.toml` in the dependencies
section. `wasm-bindgen` is a tool that facilitates interoperability between
wasm modules and JavaScript.
Next, add a `[lib]` section, with a new field named `crate-type` set to
`"cdylib"`. This specifies that the library is a C compatible dynamic library,
which helps `cargo` pass the correct flags to the Rust compiler when targeting
`wasm32`.
After making these changes, your `Cargo.toml` file should look something like
this:
```
[package]
name = "wasm-add"
version = "0.1.0"
authors = ["Michael Gattozzi <mgattozzi@gmail.com>"]
description = "Code used to demonstrate how to use wasm-pack"
license = "MIT/Apache-2.0"
repository = "https://github.com/mgattozzi/wasm-add"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen="0.2"
```

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

@ -0,0 +1,28 @@
# Getting Started
You can create a new Rust-WebAssembly project by using the [rustwasm wasm-pack-template].
To so do, you'll need the `cargo-generate` tool. To install `cargo-generate`:
```
cargo install cargo-generate
```
Then run:
```
cargo generate --git https://github.com/rustwasm/wasm-pack-template
```
You will be prompted to give your project a name. Once you do, you will have a directory
with a new project, ready to go. We'll talk about what's been included in this template
further in this guide.
[rustwasm wasm-pack-template]: https://github.com/rustwasm/wasm-pack-template
## Manual Setup
If you'd rather not use a template, or are having trouble with the template, you can
do a manual setup by following [these instructions].
[these instructions]: ../project-setup/manual-setup/index.html

@ -7,6 +7,14 @@ much Rust knowledge to complete this tutorial.
Be sure to have done the following before starting:
1. Read and install all the [Prerequisites](../prerequisites/index.html).
2. [Install `wasm-pack`](../../getting-started/installation.html).
3. [Setup a new project](../../getting-started/project-setup/using-a-template.html).
1. [Install `wasm-pack`](../../installer)
1. Read and install the [Prerequisites](../prerequisites/index.html).
- You'll need [Rust], version 1.30 or higher. (Currently either `beta` or `nightly` channels). [Learn more](../project-setup/rust.html).
- You'll need [Node.js] and [npm] installed. You'll also need an npm Account. [Learn more](../project-setup/npm.html).
We strongly recommend that you install [Node.js] using a version manager. You can learn more [here](https://npmjs.com/get-npm).
[Rust]: https://www.rust-lang.org
[Node.js]: https://nodejs.org
[npm]: https://npmjs.com

@ -4,7 +4,7 @@ We've made our code so now we need to package it all up. In your project directo
command:
```bash
$ wasm-pack init --scope MYSCOPE
$ wasm-pack build --scope MYSCOPE
```
where `MYSCOPE` is your npm username. Normally you could just type `wasm-pack init` but since
@ -21,7 +21,7 @@ First off you'll need to login to npm with the account you made earlier if you d
one:
```bash
$ npm login
$ wasm-pack login
```
Next you'll need to go into the `pkg` directory and actually upload the package:

@ -0,0 +1,90 @@
# Cargo.toml
`Cargo.toml` is the manifest file for Rust's package manager, `cargo`. This file contains
metadata such as name, version, and dependencies for packages, which are call "crates" in Rust.
There's a bunch of metadata that the template gives us, but there are 3 key parts to discuss:
- [`crate-type`](#a1-crate-type)
- [`wasm-bindgen` dependency](#a2-wasm-bindgen-dependency)
- [`[features]` and `wee-alloc`, `console-error-panic-hook` dependencies](#a3-features-and-wee-alloc-console-error-panic-hook-dependencies)
<hr/>
## 1. `crate-type`
```toml
[lib]
crate-type = ["cdylib"]
```
A Rust-`wasm` crate is a bit different from a normal crate, and as a result, we need to note
this in our `Cargo.toml`.
When `cargo` is told to build a project, or compilation is otherwise done on a Rust project,
the Rust compiler will need to link crates together, using a particular method, either
staticly or dynamically. The two types of crate that you are likely most familiar with are
`#[crate_type = "bin"]` and `#[crate_type = "lib"]`, which are the crate types that largely
represent the difference between Rust application projects and Rust libraries.
`#[crate_type = "cdylib"]` signifies that you'd like the compiler to create a dynamic system
library. This type of library is suited for situations where you'd like to compile Rust code
as a dynamic library to be loaded from another language. In our case, we'll be compiling to a
`.wasm` file, but this output type will create `*.so` files on Linux, `*.dylib` files on
macOS, and `*.dll` files on Windows in non-`wasm` circumstances.
You can read more about linking and crate types, [here](https://doc.rust-lang.org/reference/linkage.html).
## 2. `wasm-bindgen` dependency
`wasm-bindgen` is our most important dependency. This package allows us to use the
`#[wasm-bindgen]` attribute to tag code that represents the interface we want between
our JavaScript and Rust-generated `wasm`. We can import JS and export Rust by using this
attribute.
```toml
wasm-bindgen = "0.2"
```
We'll see more about how to use this library when we discuss what has been generated in `lib.rs`.
If you are coming from JavaScript, you might note that when we add the dependency
there is no `^` or `~` symbol- it looks like we're locking to the `0.2` version.
However, that's not the case! In Rust, the `^` is implied.
## 3. `[features]` and `wee-alloc`, `console-error-panic-hook` dependencies
As part of our effort to design a template that helps people discover useful crates
for their particular use case, this template includes 2 dependencies that can be
very useful for folks developing Rust-`wasm` crates: `console-error-panic-hook` and
`wee-alloc`.
Because these dependencies are useful primarily in a specifc portion of the Rust-`wasm`
crate development workflow, we've also set up a bit of glue code that allows us to include
them both as dependences, but allowing for them to be optionally included.
```toml
[features]
default-features = ["console_error_panic_hook", "wee_alloc"]
[dependencies]
cfg-if = "0.1.2"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.1", optional = true }
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
wee_alloc = { version = "0.4.1", optional = true }
```
[`cfg-if`] allows us to check if certain features are enabled on a rust crate. We'll
use this crate in `utils.rs` to optionally enable `console_error_panic_hook` or
`wee_alloc`. By default, we have them enabled. To disable them, we can remove their
entry from the `default-features` vector.
To learn more about these features, we discuss them in depth in the `utils.rs` section.

@ -0,0 +1,16 @@
# Template Deep Dive
This section is a deep dive into the contents of the files of a "Hello, World" project,
specifically written for people who are not that familiar with Rust. If you'd rather just
checkout the workflow, feel free to skip this section!
If you haven't used a template to set up your project, the contents of your files
may look slightly different than what is described here.
### What the Template Gave Us
Let's start by taking a look at what the template generated for us.
- [`Cargo.toml`](./cargo-toml.html)
- [`src/lib.rs`](./src-lib-rs.html)
- [`src/utils.rs`](./src-utils-rs.html)

@ -0,0 +1,3 @@
# src/lib.rs
🚧 COMING SOON 🚧

@ -0,0 +1,3 @@
# src/utils.rs
🚧 COMING SOON 🚧

@ -1,90 +1,70 @@
# Run The Code From npm
Alright let's make a new small directory to test that we can now run this code and pull it from npm.
This portion of the tutorial will help you create a [Webpack] JavaScript project that will
run your WebAssembly code in the browser.
```bash
$ mkdir test
$ cd test
```
[Webpack]: https://webpack.js.org/
Now we need to create a `package.json` file that looks like this:
```json
{
"scripts": {
"serve": "webpack-dev-server"
},
"dependencies": {
"@MYSCOPE/wasm-add": "^0.1.0"
},
"devDependencies": {
"webpack": "^4.0.1",
"webpack-cli": "^2.0.10",
"webpack-dev-server": "^3.1.0"
}
}
```
## Scaffold a JavaScript Project
where `MYSCOPE` is your npm username. You can expand this to be a more complete file but
we're really just trying to verify that this works!
Next up we'll need to create a small webpack configuration so that we can use the
`webpack-dev-server` to serve the wasm file properly. It should be noted that webpack isn't
a requirement. It's just what was chosen for this tutorial. You just need something to server the
code! Here's what your `webpack.config.js` should look like:
```javascript
const path = require('path');
module.exports = {
entry: "./index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "index.js",
},
mode: "development"
};
```
To scaffold a project that we can use our new package in, we'll use an npm template called
[`create-wasm-app`]. To use this run this command in a directory *different* than your Rust
project:
This tells webpack that if it's going to start things up use `index.js`. Before we do that though
we'll need to setup a small html file. Create a new file called `index.html` and put this inside it:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>wasm-pack example</title>
</head>
<body>
<script src="./index.js"></script>
</body>
</html>
[`create-wasm-app`]: https://github.com/rustwasm/create-wasm-app
```
npm init wasm-app
```
We're almost set. Now we need to setup our JS file so that we can run some wasm code!
Make a file called `index.js` and put this inside of it:
This tool will ask you for the name of a project, and then create a directory with that name.
If we look in that directory, we'll see the following:
- `.gitignore`: ignores `node_modules`
- `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you
- `README.md`: the file you are reading now!
- `index.html`: a bare bones html document that includes the webpack bundle
- `index.js`: example js file with a comment showing how to import and use a wasm pkg
- `package.json` and `package-lock.json`:
- pulls in devDependencies for using webpack:
- [`webpack`](https://www.npmjs.com/package/webpack)
- [`webpack-cli`](https://www.npmjs.com/package/webpack-cli)
- [`webpack-dev-server`](https://www.npmjs.com/package/webpack-dev-server)
- defines a `start` script to run `webpack-dev-server`
- `webpack.config.js`: configuration file for bundling your js with webpack
```javascript
const js = import("@MYSCOPE/wasm-add/wasm_add.js");
js.then(js => {
js.alert_add(3,2);
});
## Add Your npm Package
The scaffolded project includes an example WebAssembly package, `hello-wasm-pack`, in your
`package.json`. Go into the `package.json` file, add your package, and remove the
`hello-wasm-pack` dependency from the `"dependencies"` section.
Now, open up the `index.js` file. Replace the `hello-wasm-pack` in the first line with the
name of your package:
```js
import * as wasm from "<your package name>";
wasm.greet();
```
Since web pack [can't load wasm synchronously yet](https://github.com/webpack/webpack/issues/6615)
we are using the import statement above followed
by the promise in order to load it properly. This is what lets us then call `alert_add`. We're
importing from the `node_module` folder we haven't gotten yet so let's import all of our
dependencies finally and run the example!
## Run The Project
Before we run our project, we need to make sure we install our dependencies:
```bash
$ npm install
$ npm run serve
npm install
```
Then in a web browser navigate to `http://localhost:8080` you should see something like this:
We should be ready to run our project now! To run our project we'll run:
```bash
npm start
```
![An alert box saying "Hello from Rust! 3 + 2 = 5"](./wasm-pack/wasm-pack.png)
Then in a web browser navigate to `http://localhost:8080` and you should be greeted with an
alert box that says "Hello World!".
If you did congrats you've successfully uploaded your first bit of wasm code to npm and used it
properly!

@ -1 +0,0 @@
# Writing a Rust-WebAssembly Library
Loading…
Cancel
Save