Skip to content

Commit 095779e

Browse files
authored
Merge pull request #10 from germania/master
Add support for wasm32-unknown-unknown
2 parents ee15aec + 160ac4f commit 095779e

13 files changed

+4895
-1018
lines changed

README.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
[![npm](https://img.shields.io/npm/v/rust-wasm-loader.svg)](https://www.npmjs.com/package/rust-wasm-loader)
44

5-
### Usage
5+
## Usage
66

7-
This is a simple Webpack loader that shells out to cargo to build a Rust project targeting WebAssembly. See [this post](https://users.rust-lang.org/t/compiling-to-the-web-with-rust-and-emscripten/7627) for
7+
This is a simple Webpack loader that shells out to cargo to build a Rust project targeting WebAssembly. See [this post](https://www.hellorust.com/setup/wasm-target/) for
88
more details on using Rust to target the web.
99

1010
To use it, first install the package:
@@ -38,28 +38,27 @@ module.exports = {
3838

3939
Note: if you are using `file-loader`, make sure to add `.wasm` to the test field, otherwise the module will not be copied! (e.g. `test: /\.(wasm|jpg|jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i,`).
4040

41-
Make sure you have the `cargo`, `rustc`, and `emsdk` binaries somewhere in your `PATH`.
41+
Make sure you have the `cargo`, `rustc`, and (optionally) `emsdk` binaries somewhere in your `PATH`. `stdweb` and other Rust libraries require a nightly build, which can be installed from https://rustup.rs/ .
42+
4243
Require and initialize the wasm module:
4344

4445
```js
45-
const wasm = require('./main.rs')
46-
wasm.initialize().then(module => {
46+
const wasm = require('./lib.rs')
47+
wasm.then(module => {
4748
// Use your module here
48-
const doub = module.cwrap('doub', 'number', ['number'])
49-
console.log(doub(21))
49+
console.log(module.doub(21))
5050
})
5151
```
5252

5353
or with async/await:
5454

5555
```js
56-
async function loadAndUseWasm() {
57-
const wasm = require('./main.rs')
58-
const module = await wasm.initialize()
59-
const doub = module.cwrap('doub', 'number', ['number'])
60-
console.log(doub(21))
56+
async function loadwasm() {
57+
const lib = await require('./lib.rs');
58+
// Use your module here
59+
console.log(lib.doub(21));
6160
}
62-
loadAndUseWasm()
61+
loadwasm();
6362
```
6463

6564
### Configuration
@@ -70,6 +69,7 @@ The following options can be added to the Webpack loader query:
7069
| ---- | ----------- | -------- | ------- |
7170
| `release` | Whether or not to pass the `--release` flag to cargo | false | false |
7271
| `path` | Path to your webpack output folder relative to project root | true | '' |
72+
| `target` | Allows one to specify `wasm32-unknown-emscripten` as build target | false | 'wasm32-unknown-unknown' |
7373

7474
### Example
7575

example/Cargo.lock

+150-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/Cargo.toml

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
authors = ["Ian J Sikes <[email protected]>"]
3-
name = "rust-wasm-loader-example"
4-
version = "0.1.0"
3+
name = "rustexample"
4+
version = "0.2.0"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
58

69
[dependencies]
7-
webplatform = "0.4.2"
10+
"stdweb" = "0.4.6"

example/index.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4+
<base href="build/">
45
<title>Hello World</title>
56
</head>
67
<body>
78
<div id="container"></div>
8-
<script src="build/bundle.js"></script>
9+
<script src="bundle.js"></script>
910
</body>
1011
</html>

0 commit comments

Comments
 (0)