Skip to content

Instantly share code, notes, and snippets.

@banyudu
Last active August 25, 2021 03:02
Show Gist options
  • Save banyudu/194a317e1254067bd023dbdd6ce10ac1 to your computer and use it in GitHub Desktop.
Save banyudu/194a317e1254067bd023dbdd6ce10ac1 to your computer and use it in GitHub Desktop.
rust-hello-wasm-nodejs
[package]
name = "hello-wasm"
version = "0.1.2"
authors = ["Yudu Ban <[email protected]>"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.63"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.6", optional = true }
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true }
log = "0.4.14"
wasm-bindgen-console-logger = "0.1.1"
[dev-dependencies]
wasm-bindgen-test = "0.3.13"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
mod utils;
use log::{info};
use wasm_bindgen::prelude::*;
use wasm_bindgen_console_logger::DEFAULT_LOGGER;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
pub fn greet() {
log::set_logger(&DEFAULT_LOGGER).unwrap();
log::set_max_level(log::LevelFilter::Info);
info!("Hello Wasm!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment