From dad130cdbb640f938fd1261bb3171882e7b84304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1=20de=20Mello?= <3285133+asmello@users.noreply.github.com> Date: Sun, 7 May 2023 18:13:22 +0100 Subject: [PATCH 1/2] modernize --- template/Cargo.toml | 18 +++++++----------- template/package.json | 16 ++++++++-------- template/tests/app.rs | 23 ++++++++--------------- template/webpack.config.js | 20 +++++++++++--------- 4 files changed, 34 insertions(+), 43 deletions(-) diff --git a/template/Cargo.toml b/template/Cargo.toml index 71d764c..ea476a1 100644 --- a/template/Cargo.toml +++ b/template/Cargo.toml @@ -6,7 +6,7 @@ version = "0.1.0" authors = ["You <you@example.com>"] categories = ["wasm"] readme = "README.md" -edition = "2018" +edition = "2021" [lib] crate-type = ["cdylib"] @@ -30,22 +30,18 @@ wasm-bindgen = "0.2.45" # allocator, so it's not enabled by default. wee_alloc = { version = "0.4.2", optional = true } +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. +console_error_panic_hook = "0.1.5" + # The `web-sys` crate allows you to interact with the various browser APIs, # like the DOM. [dependencies.web-sys] version = "0.3.22" features = ["console"] -# 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 it's only enabled -# in debug mode. -[target."cfg(debug_assertions)".dependencies] -console_error_panic_hook = "0.1.5" - # These crates are used for running unit tests. [dev-dependencies] -wasm-bindgen-test = "0.2.45" -futures = "0.1.27" +wasm-bindgen-test = "0.3.34" js-sys = "0.3.22" -wasm-bindgen-futures = "0.3.22" +wasm-bindgen-futures = "0.4.34" diff --git a/template/package.json b/template/package.json index 26c4708..a753c75 100644 --- a/template/package.json +++ b/template/package.json @@ -4,15 +4,15 @@ "version": "0.1.0", "scripts": { "build": "rimraf dist pkg && webpack", - "start": "rimraf dist pkg && webpack-dev-server --open -d", - "test": "cargo test && wasm-pack test --headless" + "start": "rimraf dist pkg && webpack serve --open", + "test": "cargo test && wasm-pack test --node --headless" }, "devDependencies": { - "@wasm-tool/wasm-pack-plugin": "^1.1.0", - "copy-webpack-plugin": "^5.0.3", - "webpack": "^4.42.0", - "webpack-cli": "^3.3.3", - "webpack-dev-server": "^3.7.1", - "rimraf": "^3.0.0" + "@wasm-tool/wasm-pack-plugin": "^1.7.0", + "copy-webpack-plugin": "^11.0.0", + "rimraf": "^5.0.0", + "webpack": "^5.54.0", + "webpack-cli": "^5.1.0", + "webpack-dev-server": "^4.15.0" } } diff --git a/template/tests/app.rs b/template/tests/app.rs index 9223aa3..0b6fc10 100644 --- a/template/tests/app.rs +++ b/template/tests/app.rs @@ -1,35 +1,28 @@ -use wasm_bindgen_test::{wasm_bindgen_test_configure, wasm_bindgen_test}; -use futures::prelude::*; use wasm_bindgen::JsValue; use wasm_bindgen_futures::JsFuture; +use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure}; wasm_bindgen_test_configure!(run_in_browser); - // This runs a unit test in native Rust, so it can only use Rust APIs. #[test] fn rust_test() { assert_eq!(1, 1); } - // This runs a unit test in the browser, so it can use browser APIs. #[wasm_bindgen_test] fn web_test() { assert_eq!(1, 1); } - -// This runs a unit test in the browser, and in addition it supports asynchronous Future APIs. -#[wasm_bindgen_test(async)] -fn async_test() -> impl Future<Item = (), Error = JsValue> { - // Creates a JavaScript Promise which will asynchronously resolve with the value 42. +// This runs a unit test in the browser, and in addition it supports asynchronous functions +#[wasm_bindgen_test] +async fn my_async_test() { + // Create a promise that is ready on the next tick of the micro task queue. let promise = js_sys::Promise::resolve(&JsValue::from(42)); - // Converts that Promise into a Future. - // The unit test will wait for the Future to resolve. - JsFuture::from(promise) - .map(|x| { - assert_eq!(x, 42); - }) + // Convert that promise into a future and make the test wait on it. + let x = JsFuture::from(promise).await.unwrap(); + assert_eq!(x, 42); } diff --git a/template/webpack.config.js b/template/webpack.config.js index d35da09..ec26b1e 100644 --- a/template/webpack.config.js +++ b/template/webpack.config.js @@ -5,24 +5,26 @@ const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin"); const dist = path.resolve(__dirname, "dist"); module.exports = { - mode: "production", + mode: "development", + experiments: { + futureDefaults: true, + }, entry: { - index: "./js/index.js" + index: "./js/index.js", }, output: { path: dist, - filename: "[name].js" + filename: "[name].js", }, devServer: { - contentBase: dist, + static: dist, }, plugins: [ - new CopyPlugin([ - path.resolve(__dirname, "static") - ]), - + new CopyPlugin({ + patterns: [path.resolve(__dirname, "static")], + }), new WasmPackPlugin({ crateDirectory: __dirname, }), - ] + ], }; From ec46e40740ea85ead7d637264948be507a69be9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1=20de=20Mello?= <3285133+asmello@users.noreply.github.com> Date: Tue, 4 Jul 2023 18:33:27 +0100 Subject: [PATCH 2/2] use more targeted flag and tweak tests command --- template/package.json | 2 +- template/webpack.config.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/template/package.json b/template/package.json index a753c75..1d46e3e 100644 --- a/template/package.json +++ b/template/package.json @@ -5,7 +5,7 @@ "scripts": { "build": "rimraf dist pkg && webpack", "start": "rimraf dist pkg && webpack serve --open", - "test": "cargo test && wasm-pack test --node --headless" + "test": "cargo test && wasm-pack test --headless" }, "devDependencies": { "@wasm-tool/wasm-pack-plugin": "^1.7.0", diff --git a/template/webpack.config.js b/template/webpack.config.js index ec26b1e..556a38a 100644 --- a/template/webpack.config.js +++ b/template/webpack.config.js @@ -7,7 +7,7 @@ const dist = path.resolve(__dirname, "dist"); module.exports = { mode: "development", experiments: { - futureDefaults: true, + asyncWebAssembly: true, }, entry: { index: "./js/index.js",