Skip to content

Commit 827ce54

Browse files
authored
Merge pull request #99 from data-pup/add-example-usage-documentation
Add example usage documentation
2 parents 4e98b42 + b90c028 commit 827ce54

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

docs/init.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# wasm-pack init
2+
3+
The `wasm-pack init` command creates the files neccessary for JavaScript
4+
interoperability and for publishing a package to npm. This involves compiling
5+
your code to wasm and generating a pkg folder. This pkg folder will contain the
6+
wasm binary, a JS wrapper file, your `README`, and a `package.json` file.
7+
8+
## Path
9+
10+
The `wasm-pack init` command can be given an optional path argument, e.g.:
11+
12+
```
13+
wasm-pack init examples/js-hello-world
14+
```
15+
16+
This path should point to a directory that contains a `Cargo.toml` file. If no
17+
path is given, the `init` command will run in the current directory.
18+
19+
## Scope
20+
21+
The init command also accepts an optional `--scope` argument. This will scope
22+
your package name, which is useful if your package name might conflict with
23+
something in the public registry. For example:
24+
25+
```
26+
wasm-pack init examples/js-hello-world --scope test
27+
```
28+
29+
This command would create a `package.json` file for a package called
30+
`@test/js-hello-world`. For more information about scoping, you can refer to
31+
the npm documentation [here][npm-scope-documentation].
32+
33+
[npm-scope-documentation]: https://docs.npmjs.com/misc/scope

docs/pack.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# wasm-pack pack
2+
3+
The `wasm-pack pack` command is used to create a tarball of your wasm project
4+
using the `npm pack` command. This is useful if you would like to create a
5+
local tarball containing your package, without publishing it to the npm
6+
registry.
7+
8+
You can read more about the `npm pack` command [here][npm-pack-documentation].
9+
10+
[npm-pack-documentation]: https://docs.npmjs.com/cli/pack

docs/prerequisites.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Prerequisites
2+
3+
## Rust and npm
4+
5+
Before installing `wasm-pack`, you should make sure that you have already
6+
installed Rust and npm. You can confirm that these are installed using the
7+
following commands:
8+
9+
```sh
10+
# Check if Rust is installed.
11+
cargo --version
12+
13+
# Check if npm is installed.
14+
npm --version
15+
```
16+
17+
You can find more information about installing Rust
18+
[here][rust-wasm-install-info] and more information about installing npm
19+
[here][npm-install-info].
20+
21+
## Sign Up For npm
22+
23+
You will need to create an npm account if you plan on publishing your package to
24+
the npm public registry. You can find information about signing up for npm
25+
[here][npm-signup-info].
26+
27+
[rust-wasm-install-info]: https://rust-lang-nursery.github.io/rust-wasm/setup.html
28+
[npm-install-info]: https://www.npmjs.com/get-npm
29+
[npm-signup-info]: https://www.npmjs.com/signup

docs/publish.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# wasm-pack publish
2+
3+
The `wasm-pack publish` command will create a tarball of your wasm project,
4+
and publish it to the npm registry.

docs/setup.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Setup
2+
3+
## Installing wasm-pack
4+
5+
You can install `wasm-pack` using the following command:
6+
7+
```
8+
cargo install wasm-pack
9+
```
10+
11+
If you have already installed `wasm-pack` and want to install a newer version,
12+
you can use the `--force` option, like this:
13+
14+
```
15+
cargo install wasm-pack --force
16+
```
17+
18+
## Project Initialization
19+
20+
You can create a new Rust project named `my-lib` using this command.
21+
22+
```
23+
cargo new --lib my-lib
24+
```
25+
26+
The `--lib` flag specifies that the project is a library, which is important
27+
because we will be calling this code from JavaScript.
28+
29+
### Cargo.toml changes
30+
31+
You will need to add `wasm-bindgen` to your `Cargo.toml` in the dependencies
32+
section. `wasm-bindgen` is a tool that facilitates interoperability between
33+
wasm modules and JavaScript.
34+
35+
Next, add a `[lib]` section, with a new field named `crate-type` set to
36+
`"cdylib"`. This specifies that the library is a C compatible dynamic library,
37+
which helps `cargo` pass the correct flags to the Rust compiler when targeting
38+
`wasm32`.
39+
40+
After making these changes, your `Cargo.toml` file should look something like
41+
this:
42+
43+
```
44+
[package]
45+
name = "wasm-add"
46+
version = "0.1.0"
47+
authors = ["Michael Gattozzi <[email protected]>"]
48+
description = "Code used to demonstrate how to use wasm-pack"
49+
license = "MIT/Apache-2.0"
50+
repository = "https://github.com/mgattozzi/wasm-add"
51+
52+
[lib]
53+
crate-type = ["cdylib"]
54+
55+
[dependencies]
56+
wasm-bindgen="0.2"
57+
```

0 commit comments

Comments
 (0)