This repository has been archived by the owner on Nov 8, 2021. It is now read-only.
forked from rustwasm/wasm-pack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rustwasm#576 from rustwasm/j-tut
webpack tutorial and some docs nits
- Loading branch information
Showing
23 changed files
with
203 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
position: relative; | ||
float: left; | ||
margin-bottom: 0; | ||
bottom: -5px; | ||
} | ||
|
||
.navbar-logo a img { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
docs/src/tutorials/hybrid-applications-with-webpack/getting-started.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Getting Started | ||
|
||
You can create a new Rust-WebAssembly webpack project by using the [rustwasm webpack-template]. | ||
|
||
Run: | ||
|
||
``` | ||
npm init rust-webpack my-app | ||
``` | ||
|
||
The last argument will be your project name. After you run the command, 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 webpack-template]: https://github.com/rustwasm/rust-webpack-template |
14 changes: 14 additions & 0 deletions
14
docs/src/tutorials/hybrid-applications-with-webpack/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Hybrid Applications with Webpack | ||
|
||
The goal of this tutorial is to introduce you to the [`rust-webpack-template`] | ||
and the `wasm-pack` workflow by building the example app in the template. | ||
|
||
This tutorial is aimed at folks who are both beginners to WebAssembly and Rust- you don't need | ||
much Rust knowledge to complete this tutorial. | ||
|
||
Be sure to have read and followed the [Prerequisites](../prerequisites/index.html). | ||
|
||
[Rust]: https://www.rust-lang.org | ||
[Node.js]: https://nodejs.org | ||
[npm]: https://npmjs.com | ||
[`rust-webpack-template`]: https://github.com/rustwasm/rust-webpack-template |
85 changes: 85 additions & 0 deletions
85
docs/src/tutorials/hybrid-applications-with-webpack/using-your-library.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Run The Code | ||
|
||
The Rust Webpack template is designed for creating monorepo-style Web applications with | ||
Rust-generated WebAssembly and Webpack without publishing your wasm to NPM. | ||
This portion of the tutorial will explain how to build a [Webpack] JavaScript project | ||
that will run your WebAssembly code in the browser. | ||
|
||
[Webpack]: https://webpack.js.org/ | ||
|
||
## Scaffold a JavaScript Project | ||
|
||
To generate a new Rust Webpack project, we've used the [`rust-webpack`] npm template. | ||
|
||
[`rust-webpack`]: https://github.com/rustwasm/rust-webpack-template | ||
|
||
``` | ||
npm init rust-webpack your-package-name | ||
``` | ||
|
||
A new project folder will be created with the name you supply. | ||
|
||
If we look in the project, 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 | ||
- `js/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 | ||
- `crate/src/lib.rs`: your Rust crate code! | ||
|
||
## Your Rust Crate | ||
|
||
The scaffolded project includes an example Rust WebAssembly webpack crate. | ||
|
||
Inside the `crate/src/lib.rs` file we see a `run` function that's callable from our JS file: | ||
```rust | ||
// Called by our JS entry point to run the example. | ||
#[wasm_bindgen] | ||
pub fn run() -> Result<(), JsValue> { | ||
set_panic_hook(); | ||
|
||
// ... | ||
let p: web_sys::Node = document.create_element("p")?.into(); | ||
p.set_text_content(Some("Hello from Rust, WebAssembly, and Webpack!")); | ||
// ... | ||
|
||
Ok(()) | ||
} | ||
``` | ||
|
||
Now, open up the `js/index.js` file. We see our Rust-generated wasm `run` function being | ||
called inside our JS file. | ||
|
||
```js | ||
import("../crate/pkg").then(module => { | ||
module.run(); | ||
}); | ||
``` | ||
|
||
## Run The Project | ||
|
||
To generate our Rust-compiled to wasm code, in the root directory we run: | ||
```bash | ||
npm run build | ||
``` | ||
This will create our bundled JavaScript module in a new directory `dist`. | ||
|
||
We should be ready to run our project now! | ||
In the root directory, we'll run: | ||
|
||
```bash | ||
npm start | ||
``` | ||
|
||
Then in a web browser navigate to `http://localhost:8080` and you should be greeted | ||
with text in the body of the page that says "Hello from Rust, WebAssembly, and Webpack!" | ||
|
||
If you did congrats! You've successfully used the rust-webpack template! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Tutorials | ||
|
||
We have two tutorials that help you get started with `wasm-pack`: | ||
|
||
- If you want to create and publish a package: [npm browser packages] | ||
- If you'd like to develop a Wasm library alongside a JavaScript application using Webpack: [Hybrid applications with Webpack] | ||
|
||
[npm browser packages]: npm-browser-packages/index.html | ||
[Hybrid applications with Webpack]: hybrid-applications-with-webpack/index.html |
24 changes: 24 additions & 0 deletions
24
docs/src/tutorials/npm-browser-packages/building-your-package.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Building your package | ||
|
||
We've written our code so now we need to package it all up. | ||
|
||
We are writing a package that should be used in the browser, so we run this in our terminal: | ||
|
||
```bash | ||
$ wasm-pack build --scope MYSCOPE | ||
``` | ||
|
||
If you were writing a package that should be used in Node.js (with CommonJS modules, e.g. `require`), | ||
you would run this in your terminal: | ||
|
||
```bash | ||
$ wasm-pack build --scope MYSCOPE --target nodejs | ||
``` | ||
|
||
where `MYSCOPE` is your npm username. Normally you could just type `wasm-pack init` but since | ||
other people are doing this tutorial as well we don't want conflicts with the `wasm-add` package | ||
name! This command when run does a few things: | ||
|
||
1. It'll compile your code to wasm if you haven't already | ||
2. It'll generate a pkg folder with the wasm file, a JS wrapper file around the wasm, your README, | ||
and a `package.json` file. |
File renamed without changes.
5 changes: 3 additions & 2 deletions
5
docs/src/tutorial/index.md → ...c/tutorials/npm-browser-packages/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions
1
.../src/tutorials/npm-browser-packages/template-deep-dive/building-your-project.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Building your project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Standalone WASM Binaries |