Commit e1aef2c6dba438c9eb972ae24639dc5b37316766

Authored by Georg Hopp
1 parent 6539d87e

starting point for wasm tutorial

  1 +/target
  2 +**/*.rs.bk
  3 +Cargo.lock
  4 +bin/
  5 +pkg/
  6 +wasm-pack/
  7 +wasm-pack.log
... ...
  1 +[package]
  2 +name = "wasm-game-of-life"
  3 +version = "0.1.0"
  4 +authors = ["hopp@silpion.de"]
  5 +edition = "2018"
  6 +
  7 +[lib]
  8 +crate-type = ["cdylib", "rlib"]
  9 +
  10 +[features]
  11 +default = ["console_error_panic_hook"]
  12 +
  13 +[dependencies]
  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"
... ...
  1 +<div align="center">
  2 +
  3 + <h1><code>wasm-pack-template</code></h1>
  4 +
  5 + <strong>A template for kick starting a Rust and WebAssembly project using <a href="https://github.com/rustwasm/wasm-pack">wasm-pack</a>.</strong>
  6 +
  7 + <p>
  8 + <a href="https://travis-ci.org/rustwasm/wasm-pack-template"><img src="https://img.shields.io/travis/rustwasm/wasm-pack-template.svg?style=flat-square" alt="Build Status" /></a>
  9 + </p>
  10 +
  11 + <h3>
  12 + <a href="https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html">Tutorial</a>
  13 + <span> | </span>
  14 + <a href="https://discordapp.com/channels/442252698964721669/443151097398296587">Chat</a>
  15 + </h3>
  16 +
  17 + <sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub>
  18 +</div>
  19 +
  20 +## About
  21 +
  22 +[**📚 Read this template tutorial! 📚**][template-docs]
  23 +
  24 +This template is designed for compiling Rust libraries into WebAssembly and
  25 +publishing the resulting package to NPM.
  26 +
  27 +Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other
  28 +templates and usages of `wasm-pack`.
  29 +
  30 +[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html
  31 +[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html
  32 +
  33 +## 🚴 Usage
  34 +
  35 +### 🐑 Use `cargo generate` to Clone this Template
  36 +
  37 +[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate)
  38 +
  39 +```
  40 +cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project
  41 +cd my-project
  42 +```
  43 +
  44 +### 🛠️ Build with `wasm-pack build`
  45 +
  46 +```
  47 +wasm-pack build
  48 +```
  49 +
  50 +### 🔬 Test in Headless Browsers with `wasm-pack test`
  51 +
  52 +```
  53 +wasm-pack test --headless --firefox
  54 +```
  55 +
  56 +### 🎁 Publish to NPM with `wasm-pack publish`
  57 +
  58 +```
  59 +wasm-pack publish
  60 +```
  61 +
  62 +## 🔋 Batteries Included
  63 +
  64 +* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating
  65 + between WebAssembly and JavaScript.
  66 +* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook)
  67 + for logging panic messages to the developer console.
  68 +* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized
  69 + for small code size.
... ...
  1 +mod utils;
  2 +
  3 +use wasm_bindgen::prelude::*;
  4 +
  5 +// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
  6 +// allocator.
  7 +#[cfg(feature = "wee_alloc")]
  8 +#[global_allocator]
  9 +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
  10 +
  11 +#[wasm_bindgen]
  12 +extern {
  13 + fn alert(s: &str);
  14 +}
  15 +
  16 +#[wasm_bindgen]
  17 +pub fn greet() {
  18 + alert("Hello, {{project-name}}!");
  19 +}
... ...
  1 +pub fn set_panic_hook() {
  2 + // When the `console_error_panic_hook` feature is enabled, we can call the
  3 + // `set_panic_hook` function at least once during initialization, and then
  4 + // we will get better error messages if our code ever panics.
  5 + //
  6 + // For more details see
  7 + // https://github.com/rustwasm/console_error_panic_hook#readme
  8 + #[cfg(feature = "console_error_panic_hook")]
  9 + console_error_panic_hook::set_once();
  10 +}
... ...
  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 +wasm_bindgen_test_configure!(run_in_browser);
  9 +
  10 +#[wasm_bindgen_test]
  11 +fn pass() {
  12 + assert_eq!(1 + 1, 2);
  13 +}
... ...
Please register or login to post a comment