Skip to content

Commit cf39661

Browse files
committed
Make updates
1 parent 10f54d8 commit cf39661

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

src/wasm-pack/initialize.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Now that we've installed all of our tools and setup our npm account we can actually start coding!
44
We'll be writing up a small crate that adds two numbers and outputs the numbers. While this will
55
be a simple example, we're really trying to focus on how to use wasm-pack. You'll be provided links
6-
to other resources so you can make more complicated code to package and ship to npm!
6+
to other resources so you can make more complicated code to package and ship them to npm!
77

88
Let's get started then! First off run this command to create our project:
99

src/wasm-pack/introduction.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Introduction
22

3-
`wasm-pack` is a brand new tool designed to make packaging up wasm binaries and distributing them
4-
on npm easy to do. We can't necessarily distribute Rust code to developers directly and expect them
5-
to build it from scratch. npm is about installing the packages and importing them, not also
6-
compiling them. With wasm though it's not a problem. Once it's compiled it's all good to go.
3+
`wasm-pack` is a brand new tool designed to make packaging up binaries that include wasm (that may
4+
or may not have JS in them) and make publishing them on npm easy to do. We can't necessarily
5+
distribute Rust code to developers directly and expect them
6+
to build it from scratch. npm is used to install packages for frontend work but it doesn't know how
7+
to compile Rust! With wasm though it's not a problem. Once it's compiled it's all good to go.
78
However, getting it ready to be distributed, packaging it up properly for npm, and then sending it
89
to npm can be a bit of a hassle. `wasm-pack` is here to make that easier.
910

src/wasm-pack/run-the-code.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ Now we need to create a `package.json` file that looks like this:
2121
"webpack": "^4.0.1",
2222
"webpack-cli": "^2.0.10",
2323
"webpack-dev-server": "^3.1.0"
24+
}
2425
}
2526
```
2627

2728
where `MYSCOPE` is whatever you used before. You can expand this to be a more complete file but
2829
we're really just trying to verify that this works!
2930

3031
Next up we'll need to create a small webpack configuration so that we can use the
31-
`webpack-dev-server` to serve the wasm file properly. Here's what it should look like:
32+
`webpack-dev-server` to serve the wasm file properly. It should be noted that webpack isn't
33+
a requirement. It's just what was chosen for this tutorial. You just need something to server the
34+
code! Here's what it should look like:
3235

3336
```javascript
3437
const path = require('path');
@@ -66,7 +69,8 @@ js.then(js => {
6669
});
6770
```
6871

69-
Since web pack can't load stuff synchronously yet we are using the import statement above followed
72+
Since web pack [can't load wasm synchronously yet](https://github.com/webpack/webpack/issues/6615)
73+
we are using the import statement above followed
7074
by the promise in order to load it properly. This is what lets us then call `alert_add`. We're
7175
importing from the `node_module` folder we haven't gotten yet so let's import all of our
7276
dependencies finally and run the example!

src/wasm-pack/rust-code.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ use wasm_bindgen::prelude::*;
3434
Let's step through this line by line. First up is the `proc_macro` feature. We're enabling this for
3535
the whole crate. What this means is that we will later tag code with an attribute and this will
3636
allow rust to generate code that we don't have to write by hand. In our case it'll use
37-
`wasm-bindgen.` This crate knows how to make code that works well with wasm so we don't have to
37+
`wasm-bindgen.` It should be noted that `#![feature(proc_macro)]` implies using the nightly
38+
compiler. This gated feature will hopefully be stabilized and landed soon so that you won't need it!
39+
40+
`wasm-bindgen` knows how to make code that works well with wasm so we don't have to
3841
worry about it too much and just write rust code for the most part. If you want to know the full
3942
extent of it's capabilities check out the README on it's repo which can be found
4043
[here](https://github.com/alexcrichton/wasm-bindgen). For our purposes we need to know that if we
@@ -64,14 +67,12 @@ call `alert` to print out the results before returning the value.
6467

6568
```rust
6669
#[wasm_bindgen]
67-
#[no_mangle]
68-
pub extern fn add(a: i32, b: i32) -> i32 {
70+
pub fn add(a: i32, b: i32) -> i32 {
6971
a + b
7072
}
7173

7274
#[wasm_bindgen]
73-
#[no_mangle]
74-
pub extern fn alert_add(a: i32, b: i32) -> i32 {
75+
pub fn alert_add(a: i32, b: i32) -> i32 {
7576
let c = add(a, b);
7677
alert(&format!("Hello from Rust! {} + {} = {}", a, b, c));
7778
c
@@ -106,14 +107,12 @@ extern {
106107
}
107108

108109
#[wasm_bindgen]
109-
#[no_mangle]
110-
pub extern fn add(a: i32, b: i32) -> i32 {
110+
pub fn add(a: i32, b: i32) -> i32 {
111111
a + b
112112
}
113113

114114
#[wasm_bindgen]
115-
#[no_mangle]
116-
pub extern fn alert_add(a: i32, b: i32) -> i32 {
115+
pub fn alert_add(a: i32, b: i32) -> i32 {
117116
let c = add(a, b);
118117
alert(&format!("Hello from Rust! {} + {} = {}", a, b, c));
119118
c

src/wasm-pack/setup.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ and make sure the binary is in your `$PATH` so you can run it.
1818
If you also have not installed npm already you'll need to do so! Follow the docs available on
1919
[npm](https://www.npmjs.com/get-npm) or install it through your package manager!
2020

21+
To confirm you've succeeded run:
22+
23+
```bash
24+
$ npm --version
25+
```
26+
27+
You should see the version number pop out in your terminal if you installed it successfully!
28+
2129
## npm account
2230

2331
After you have npm installed you'll need to sign up for an account on npm if you have not already

0 commit comments

Comments
 (0)