diff options
author | Joel Martin <github@martintribe.org> | 2018-07-25 16:02:40 +0900 |
---|---|---|
committer | Joel Martin <github@martintribe.org> | 2018-07-25 16:02:40 +0900 |
commit | 62ad00fedf63f9653769be0b034c10098d4709f7 (patch) | |
tree | 25b864242a7b3fd4be03cb7b5d4708c0e8e0a2f9 | |
parent | a7ca8e5c1a709b1ca70b173d4f610cf5fe3f8aba (diff) | |
download | novnc-62ad00fedf63f9653769be0b034c10098d4709f7.tar.gz |
wasm: hello world with rust, wasm-bindgen, webpack
-rw-r--r-- | core/wasm/Cargo.toml | 10 | ||||
-rw-r--r-- | core/wasm/Dockerfile | 9 | ||||
-rw-r--r-- | core/wasm/README.md | 17 | ||||
-rw-r--r-- | core/wasm/index.html | 8 | ||||
-rw-r--r-- | core/wasm/index.js | 6 | ||||
-rw-r--r-- | core/wasm/package.json | 10 | ||||
-rw-r--r-- | core/wasm/src/lib.rs | 15 | ||||
-rw-r--r-- | core/wasm/webpack.config.js | 10 |
8 files changed, 85 insertions, 0 deletions
diff --git a/core/wasm/Cargo.toml b/core/wasm/Cargo.toml new file mode 100644 index 0000000..bdd8c55 --- /dev/null +++ b/core/wasm/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "novnc" +version = "0.1.0" +authors = ["Joel Martin <github@martintribe.org>"] + +[lib] +crate-type = ["cdylib"] + +[dependencies] +wasm-bindgen = "0.2" diff --git a/core/wasm/Dockerfile b/core/wasm/Dockerfile new file mode 100644 index 0000000..e1c36a6 --- /dev/null +++ b/core/wasm/Dockerfile @@ -0,0 +1,9 @@ +FROM rustlang/rust:nightly + +RUN rustup target add wasm32-unknown-unknown --toolchain nightly + +RUN cargo install wasm-bindgen-cli + +RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - +RUN apt -y install nodejs + diff --git a/core/wasm/README.md b/core/wasm/README.md new file mode 100644 index 0000000..f73c666 --- /dev/null +++ b/core/wasm/README.md @@ -0,0 +1,17 @@ +## Prep: + +``` +docker build -t rust-wasm ./core/wasm + +docker run -it -v `pwd`:/novnc -w /novnc/core/wasm -p 8080:8080 rust-wasm bash + +npm install +``` + +Build: + +``` +cargo +nightly build --target wasm32-unknown-unknown +wasm-bindgen target/wasm32-unknown-unknown/debug/novnc.wasm --out-dir . +npm run serve # then visit localhost:8080 outside the container +``` diff --git a/core/wasm/index.html b/core/wasm/index.html new file mode 100644 index 0000000..bad47a7 --- /dev/null +++ b/core/wasm/index.html @@ -0,0 +1,8 @@ +<html> + <head> + <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/> + </head> + <body> + <script src='./index.js'></script> + </body> +</html> diff --git a/core/wasm/index.js b/core/wasm/index.js new file mode 100644 index 0000000..e8e86fb --- /dev/null +++ b/core/wasm/index.js @@ -0,0 +1,6 @@ +// Note that a dynamic `import` statement here is required due to +// webpack/webpack#6615, but in theory `import { greet } from './hello_world';` +// will work here one day as well! +const rust = import('./novnc'); + +rust.then(m => m.greet('World!')); diff --git a/core/wasm/package.json b/core/wasm/package.json new file mode 100644 index 0000000..5cd0cb4 --- /dev/null +++ b/core/wasm/package.json @@ -0,0 +1,10 @@ +{ + "scripts": { + "serve": "webpack-dev-server --host 0.0.0.0" + }, + "devDependencies": { + "webpack": "^4.16.2", + "webpack-cli": "^3.1.0", + "webpack-dev-server": "^3.1.0" + } +} diff --git a/core/wasm/src/lib.rs b/core/wasm/src/lib.rs new file mode 100644 index 0000000..1199c7f --- /dev/null +++ b/core/wasm/src/lib.rs @@ -0,0 +1,15 @@ +#![feature(wasm_custom_section, wasm_import_module, use_extern_macros)] + +extern crate wasm_bindgen; + +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern { + fn alert(s: &str); +} + +#[wasm_bindgen] +pub fn greet(name: &str) { + alert(&format!("Hi, {}!", name)); +} diff --git a/core/wasm/webpack.config.js b/core/wasm/webpack.config.js new file mode 100644 index 0000000..dce2714 --- /dev/null +++ b/core/wasm/webpack.config.js @@ -0,0 +1,10 @@ +const path = require('path'); + +module.exports = { + entry: './index.js', + output: { + path: path.resolve(__dirname, 'dist'), + filename: 'index.js', + }, + mode: 'development' +}; |