environment constructed and working

This commit is contained in:
aj 2020-10-10 22:41:31 +01:00
commit 805e79f64d
14 changed files with 5527 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log

41
Cargo.toml Normal file
View File

@ -0,0 +1,41 @@
[package]
name = "wazem"
version = "0.1.0"
authors = ["aj <andrewjpack@gmail.com>"]
edition = "2018"
repository = "https://github.com/Sarsoo/wazem"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.68"
# 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 isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.6", optional = true }
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true }
[dependencies.web-sys]
version = "0.3.45"
features = [
"console",
]
[dev-dependencies]
wasm-bindgen-test = "0.3.13"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

14
README.md Normal file
View File

@ -0,0 +1,14 @@
wazem
=======
Playing with client-side rust libraries for speedy types.
* `wasm-pack`
* `create-wasm-app`-bootstraoo dev server
## Usage
* Build the rust crate using `wasm-pack build`
* Js dev site/server in `www/`
* Build frontend using `npm run build`
* Start dev server using `npm start`

32
src/lib.rs Normal file
View File

@ -0,0 +1,32 @@
mod utils;
use wasm_bindgen::prelude::*;
extern crate web_sys;
macro_rules! log {
( $( $t:tt )* ) => {
web_sys::console::log_1(&format!( $( $t )* ).into());
}
}
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, Wazem!");
}
#[wasm_bindgen]
pub fn test_log() {
log!("Hello browser, Love Wazem!");
}

10
src/utils.rs Normal file
View File

@ -0,0 +1,10 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

13
tests/web.rs Normal file
View File

@ -0,0 +1,13 @@
//! Test suite for the Web and headless browsers.
#![cfg(target_arch = "wasm32")]
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
}

2
www/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
dist

5
www/README.md Normal file
View File

@ -0,0 +1,5 @@
wazem dev server
=======
* Build frontend using `npm run build`
* Start dev server using `npm start`

5
www/bootstrap.js vendored Normal file
View File

@ -0,0 +1,5 @@
// A dependency graph that contains any wasm must all be imported
// asynchronously. This `bootstrap.js` file does the single async import, so
// that no one else needs to worry about it again.
import("./index.js")
.catch(e => console.error("Error importing `index.js`:", e));

12
www/index.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello Wazem!</title>
</head>
<body>
<noscript>This page contains webassembly and javascript content, please enable javascript in your browser.</noscript>
<script src="./bootstrap.js"></script>
<button id="clickMe">Click ME!</button>
</body>
</html>

10
www/index.js Normal file
View File

@ -0,0 +1,10 @@
import * as wasm from "wazem";
wasm.test_log();
document.getElementById("clickMe").onclick = OnButtonClick;
export function OnButtonClick(){
wasm.greet();
}
export default wasm;

5328
www/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

35
www/package.json Normal file
View File

@ -0,0 +1,35 @@
{
"name": "wazem",
"version": "0.1.0",
"description": "Playing with rusty web libraries",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.config.js",
"start": "webpack-dev-server"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Sarsoo/wazem.git"
},
"keywords": [
"webassembly",
"wasm",
"rust",
"webpack"
],
"author": "sarsoo",
"bugs": {
"url": "https://github.com/Sarsoo/wazem/issues"
},
"homepage": "https://github.com/Sarsoo/wazem#readme",
"dependencies": {
"wazem": "file:../pkg"
},
"devDependencies": {
"copy-webpack-plugin": "^5.1.2",
"wazem": "^0.1.0",
"webpack": "^4.44.2",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
}
}

14
www/webpack.config.js Normal file
View File

@ -0,0 +1,14 @@
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require('path');
module.exports = {
entry: "./bootstrap.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bootstrap.js",
},
mode: "development",
plugins: [
new CopyWebpackPlugin(['index.html'])
],
};