Add simple rust wasm test

Needs an initial "npm install" and then "npm start" will build and launch it in the browser.
This commit is contained in:
Simon Hausmann 2020-05-13 11:21:31 +02:00
parent 6110e4952d
commit e313f39490
8 changed files with 118 additions and 0 deletions

View file

@ -9,5 +9,6 @@ members = [
'tools/viewer', 'tools/viewer',
'examples/graphicstest', 'examples/graphicstest',
'examples/rusttest', 'examples/rusttest',
'examples/rustwasmtest',
'helper_crates/const-field-offset', 'helper_crates/const-field-offset',
] ]

2
examples/rustwasmtest/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
node_modules/
package-lock.json

View file

@ -0,0 +1,21 @@
[package]
name = "rustwasmtest"
version = "0.1.0"
authors = ["Sixty FPS <info@sixtyfps.io>"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
sixtyfps = { path = "../../api/sixtyfps-rs" }
wasm-bindgen = { version = "0.2" }
web_sys = { version = "0.3", package = "web-sys", 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"

View file

@ -0,0 +1 @@
import("../pkg/index.js").catch(console.error);

View file

@ -0,0 +1,18 @@
{
"author": "You <you@example.com>",
"name": "rust-webpack-template",
"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"
},
"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"
}
}

View file

@ -0,0 +1,36 @@
use wasm_bindgen::prelude::*;
// Using a macro for now. But there could be others ways to do that
sixtyfps::sixtyfps! {
SuperSimple = Rectangle {
color: white;
Rectangle {
width: 100;
height: 100;
color: blue;
}
Rectangle {
x: 100;
y: 100;
width: (100);
height: {100}
color: green;
}
Image {
x: 200;
y: 200;
source: "../../examples/graphicstest/logo.png";
}
}
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub fn wasm_main() {
// This provides better error messages in debug mode.
// It's disabled in release mode so it doesn't bloat up the file size.
#[cfg(debug_assertions)]
console_error_panic_hook::set_once();
SuperSimple::default().run();
}

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Rust + Webpack project!</title>
</head>
<body>
<canvas id="canvas" width="640" height="480"></canvas>
<script src="index.js"></script>
</body>
</html>

View file

@ -0,0 +1,28 @@
const path = require("path");
const CopyPlugin = require("copy-webpack-plugin");
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
const dist = path.resolve(__dirname, "dist");
module.exports = {
mode: "production",
entry: {
index: "./js/index.js"
},
output: {
path: dist,
filename: "[name].js"
},
devServer: {
contentBase: dist,
},
plugins: [
new CopyPlugin([
path.resolve(__dirname, "static")
]),
new WasmPackPlugin({
crateDirectory: __dirname,
}),
]
};