wasm: hello world with rust, wasm-bindgen, webpack
This commit is contained in:
parent
a7ca8e5c1a
commit
62ad00fedf
|
@ -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"
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
```
|
|
@ -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>
|
|
@ -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!'));
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
|
@ -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));
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
entry: './index.js',
|
||||||
|
output: {
|
||||||
|
path: path.resolve(__dirname, 'dist'),
|
||||||
|
filename: 'index.js',
|
||||||
|
},
|
||||||
|
mode: 'development'
|
||||||
|
};
|
Loading…
Reference in New Issue