wasm: hello world with rust, wasm-bindgen, webpack

This commit is contained in:
Joel Martin 2018-07-25 16:02:40 +09:00
parent a7ca8e5c1a
commit 62ad00fedf
8 changed files with 85 additions and 0 deletions

10
core/wasm/Cargo.toml Normal file
View File

@ -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"

9
core/wasm/Dockerfile Normal file
View File

@ -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

17
core/wasm/README.md Normal file
View File

@ -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
```

8
core/wasm/index.html Normal file
View File

@ -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>

6
core/wasm/index.js Normal file
View File

@ -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!'));

10
core/wasm/package.json Normal file
View File

@ -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"
}
}

15
core/wasm/src/lib.rs Normal file
View File

@ -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));
}

View File

@ -0,0 +1,10 @@
const path = require('path');
module.exports = {
entry: './index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
},
mode: 'development'
};