Skip to content
This repository was archived by the owner on Sep 21, 2020. It is now read-only.

Commit 52db85b

Browse files
committed
Initial
0 parents  commit 52db85b

11 files changed

+281
-0
lines changed

.appveyor.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
install:
2+
- appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe
3+
- if not defined RUSTFLAGS rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly
4+
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
5+
- rustc -V
6+
- cargo -V
7+
8+
build: false
9+
10+
test_script:
11+
- cargo test --locked

.cargo-ok

Whitespace-only changes.

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/target
2+
**/*.rs.bk
3+
Cargo.lock
4+
bin/
5+
pkg/
6+
wasm-pack.log

.travis.yml

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
language: rust
2+
sudo: false
3+
4+
cache: cargo
5+
6+
matrix:
7+
include:
8+
9+
# Builds with wasm-pack.
10+
- rust: beta
11+
env: RUST_BACKTRACE=1
12+
addons:
13+
firefox: latest
14+
chrome: stable
15+
before_script:
16+
- (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update)
17+
- (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate)
18+
- cargo install-update -a
19+
- curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f
20+
script:
21+
- cargo generate --git . --name testing
22+
# Having a broken Cargo.toml (in that it has curlies in fields) anywhere
23+
# in any of our parent dirs is problematic.
24+
- mv Cargo.toml Cargo.toml.tmpl
25+
- cd testing
26+
- wasm-pack build
27+
- wasm-pack test --chrome --firefox --headless
28+
29+
# Builds on nightly.
30+
- rust: nightly
31+
env: RUST_BACKTRACE=1
32+
before_script:
33+
- (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update)
34+
- (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate)
35+
- cargo install-update -a
36+
- rustup target add wasm32-unknown-unknown
37+
script:
38+
- cargo generate --git . --name testing
39+
- mv Cargo.toml Cargo.toml.tmpl
40+
- cd testing
41+
- cargo check
42+
- cargo check --target wasm32-unknown-unknown
43+
- cargo check --no-default-features
44+
- cargo check --target wasm32-unknown-unknown --no-default-features
45+
- cargo check --no-default-features --features console_error_panic_hook
46+
- cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook
47+
- cargo check --no-default-features --features "console_error_panic_hook wee_alloc"
48+
- cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc"
49+
50+
# Builds on beta.
51+
- rust: beta
52+
env: RUST_BACKTRACE=1
53+
before_script:
54+
- (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update)
55+
- (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate)
56+
- cargo install-update -a
57+
- rustup target add wasm32-unknown-unknown
58+
script:
59+
- cargo generate --git . --name testing
60+
- mv Cargo.toml Cargo.toml.tmpl
61+
- cd testing
62+
- cargo check
63+
- cargo check --target wasm32-unknown-unknown
64+
- cargo check --no-default-features
65+
- cargo check --target wasm32-unknown-unknown --no-default-features
66+
- cargo check --no-default-features --features console_error_panic_hook
67+
- cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook
68+
# Note: no enabling the `wee_alloc` feature here because it requires
69+
# nightly for now.

Cargo.toml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[package]
2+
name = "hello"
3+
version = "0.1.0"
4+
authors = ["poma <[email protected]>"]
5+
6+
[lib]
7+
crate-type = ["cdylib", "rlib"]
8+
9+
[features]
10+
default = ["console_error_panic_hook"]
11+
12+
[dependencies]
13+
cfg-if = "0.1.2"
14+
wasm-bindgen = "0.2"
15+
16+
# The `console_error_panic_hook` crate provides better debugging of panics by
17+
# logging them with `console.error`. This is great for development, but requires
18+
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
19+
# code size when deploying.
20+
console_error_panic_hook = { version = "0.1.1", optional = true }
21+
22+
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
23+
# compared to the default allocator's ~10K. It is slower than the default
24+
# allocator, however.
25+
#
26+
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
27+
wee_alloc = { version = "0.4.2", optional = true }
28+
29+
[dev-dependencies]
30+
wasm-bindgen-test = "0.2"
31+
32+
[profile.release]
33+
# Tell `rustc` to optimize for small code size.
34+
opt-level = "s"

LICENSE

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright (c) 2018 poma <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any
4+
person obtaining a copy of this software and associated
5+
documentation files (the "Software"), to deal in the
6+
Software without restriction, including without
7+
limitation the rights to use, copy, modify, merge,
8+
publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software
10+
is furnished to do so, subject to the following
11+
conditions:
12+
13+
The above copyright notice and this permission notice
14+
shall be included in all copies or substantial portions
15+
of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21+
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24+
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25+
DEALINGS IN THE SOFTWARE.

README.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 🦀🕸️ `wasm-pack-template`
2+
3+
A template for kick starting a Rust and WebAssembly project using
4+
[`wasm-pack`](https://github.com/rustwasm/wasm-pack).
5+
6+
This template is designed for compiling Rust libraries into WebAssembly and
7+
publishing the resulting package to NPM.
8+
9+
* Want to use the published NPM package in a Website? [Check out
10+
`create-wasm-app`.](https://github.com/rustwasm/create-wasm-app)
11+
* Want to make a monorepo-style Website without publishing to NPM? Check out
12+
[`rust-webpack-template`](https://github.com/rustwasm/rust-webpack-template)
13+
and/or
14+
[`rust-parcel-template`](https://github.com/rustwasm/rust-parcel-template).
15+
16+
## 🔋 Batteries Included
17+
18+
* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating
19+
between WebAssembly and JavaScript.
20+
* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook)
21+
for logging panic messages to the developer console.
22+
* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized
23+
for small code size.
24+
25+
## 🚴 Usage
26+
27+
### 🐑 Use `cargo generate` to Clone this Template
28+
29+
[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate)
30+
31+
```
32+
cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project
33+
cd my-project
34+
```
35+
36+
### 🛠️ Build with `wasm-pack build`
37+
38+
```
39+
wasm-pack build
40+
```
41+
42+
### 🔬 Test in Headless Browsers with `wasm-pack test`
43+
44+
```
45+
wasm-pack test --headless --firefox
46+
```
47+
48+
### 🎁 Publish to NPM with `wasm-pack publish`
49+
50+
```
51+
wasm-pack publish
52+
```

package-lock.json

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

src/lib.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
extern crate cfg_if;
2+
extern crate wasm_bindgen;
3+
4+
mod utils;
5+
6+
use cfg_if::cfg_if;
7+
use wasm_bindgen::prelude::*;
8+
9+
cfg_if! {
10+
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
11+
// allocator.
12+
if #[cfg(feature = "wee_alloc")] {
13+
extern crate wee_alloc;
14+
#[global_allocator]
15+
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
16+
}
17+
}
18+
19+
#[wasm_bindgen]
20+
extern {
21+
fn alert(s: &str);
22+
}
23+
24+
#[wasm_bindgen]
25+
pub fn greet(name: &str) {
26+
alert(&format!("Hello, {}!", name));
27+
}
28+
29+
#[wasm_bindgen]
30+
pub struct Accumulator {
31+
pub current: u32
32+
}
33+
34+
#[wasm_bindgen]
35+
impl Accumulator {
36+
pub fn add(&mut self, num: u32) -> u32 {
37+
self.current += num;
38+
self.current
39+
}
40+
}

src/utils.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use cfg_if::cfg_if;
2+
3+
cfg_if! {
4+
// When the `console_error_panic_hook` feature is enabled, we can call the
5+
// `set_panic_hook` function at least once during initialization, and then
6+
// we will get better error messages if our code ever panics.
7+
//
8+
// For more details see
9+
// https://github.com/rustwasm/console_error_panic_hook#readme
10+
if #[cfg(feature = "console_error_panic_hook")] {
11+
extern crate console_error_panic_hook;
12+
pub use self::console_error_panic_hook::set_once as set_panic_hook;
13+
} else {
14+
#[inline]
15+
pub fn set_panic_hook() {}
16+
}
17+
}

tests/web.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! Test suite for the Web and headless browsers.
2+
3+
#![cfg(target_arch = "wasm32")]
4+
5+
extern crate wasm_bindgen_test;
6+
use wasm_bindgen_test::*;
7+
8+
extern crate hello;
9+
use hello::Accumulator;
10+
11+
wasm_bindgen_test_configure!(run_in_browser);
12+
13+
#[wasm_bindgen_test]
14+
fn pass() {
15+
assert_eq!(1 + 1, 2);
16+
}
17+
18+
#[wasm_bindgen_test]
19+
pub fn test_add() {
20+
let mut acc = Accumulator {current: 0};
21+
acc.add(2);
22+
acc.add(3);
23+
assert_eq!(acc.current, 5);
24+
}

0 commit comments

Comments
 (0)