From 5cb79f28281fe31303b9d6c4b28f07284b111fec Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sat, 8 Sep 2018 23:38:34 -0400 Subject: [PATCH] Add a minimal and plain stdweb example --- minimal/.compilerc | 42 + minimal/.eslintrc | 9 + minimal/.gitignore | 4 + minimal/Cargo.toml | 30 + minimal/build.sh | 25 + minimal/package.json | 66 + minimal/src/lib.rs | 58 + {percy => minimal}/static/app_loader.js | 5 +- minimal/static/index.html | 10 + minimal/static/index.js | 52 + minimal/yarn.lock | 7736 ++++++++++++++++++++ percy/Cargo.toml | 2 +- percy/static/app.js | 5 - percy_patched_webpack/Cargo.toml | 4 +- percy_patched_webpack/static/app.js | 5 - percy_patched_webpack/static/app_loader.js | 7 +- stdweb/.compilerc | 42 + stdweb/.eslintrc | 9 + stdweb/.gitignore | 4 + stdweb/Cargo.toml | 16 + stdweb/build.sh | 20 + stdweb/package.json | 66 + stdweb/src/lib.rs | 18 + stdweb/static/app_loader.js | 9 + stdweb/static/index.html | 11 + stdweb/static/index.js | 52 + stdweb/yarn.lock | 7736 ++++++++++++++++++++ 27 files changed, 16026 insertions(+), 17 deletions(-) create mode 100644 minimal/.compilerc create mode 100644 minimal/.eslintrc create mode 100644 minimal/.gitignore create mode 100644 minimal/Cargo.toml create mode 100755 minimal/build.sh create mode 100644 minimal/package.json create mode 100644 minimal/src/lib.rs rename {percy => minimal}/static/app_loader.js (65%) create mode 100644 minimal/static/index.html create mode 100644 minimal/static/index.js create mode 100644 minimal/yarn.lock delete mode 100644 percy/static/app.js delete mode 100644 percy_patched_webpack/static/app.js create mode 100644 stdweb/.compilerc create mode 100644 stdweb/.eslintrc create mode 100644 stdweb/.gitignore create mode 100644 stdweb/Cargo.toml create mode 100755 stdweb/build.sh create mode 100644 stdweb/package.json create mode 100644 stdweb/src/lib.rs create mode 100644 stdweb/static/app_loader.js create mode 100644 stdweb/static/index.html create mode 100644 stdweb/static/index.js create mode 100644 stdweb/yarn.lock diff --git a/minimal/.compilerc b/minimal/.compilerc new file mode 100644 index 0000000..1ecba6f --- /dev/null +++ b/minimal/.compilerc @@ -0,0 +1,42 @@ +{ + "env": { + "development": { + "application/javascript": { + "presets": [ + [ + "env", + { + "targets": { + "electron": 2 + } + } + ], + "react" + ], + "plugins": [ + "transform-async-to-generator" + ], + "sourceMaps": "inline" + } + }, + "production": { + "application/javascript": { + "presets": [ + [ + "env", + { + "targets": { + "electron": 2 + } + } + ], + "react" + ], + "plugins": [ + "transform-async-to-generator" + ], + "sourceMaps": "none" + } + } + } +} \ No newline at end of file diff --git a/minimal/.eslintrc b/minimal/.eslintrc new file mode 100644 index 0000000..64f1252 --- /dev/null +++ b/minimal/.eslintrc @@ -0,0 +1,9 @@ +{ + "extends": "eslint-config-airbnb", + "rules": { + "import/extensions": 0, + "import/no-extraneous-dependencies": 0, + "import/no-unresolved": [2, { "ignore": ["electron"] }], + "linebreak-style": 0 + } +} diff --git a/minimal/.gitignore b/minimal/.gitignore new file mode 100644 index 0000000..fa518e4 --- /dev/null +++ b/minimal/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +Cargo.lock +target/ +dist/ \ No newline at end of file diff --git a/minimal/Cargo.toml b/minimal/Cargo.toml new file mode 100644 index 0000000..2d778f4 --- /dev/null +++ b/minimal/Cargo.toml @@ -0,0 +1,30 @@ +[package] +authors = ["Bradlee Speice"] +categories = ["wasm"] +description = "A no-framework WASM Electron app" +license = "Apache-2.0/MIT" +name = "rust_webpack" +readme = "./README.md" +repository = "https://github.com/rustwasm/rust-webpack-template" +version = "0.1.0" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +cfg-if = "0.1.5" +wasm-bindgen = "0.2.19" + +# 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.5", 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. +wee_alloc = { version = "0.4.2", optional = true } + +[features] +default-features = ["console_error_panic_hook", "wee_alloc"] diff --git a/minimal/build.sh b/minimal/build.sh new file mode 100755 index 0000000..3426a2a --- /dev/null +++ b/minimal/build.sh @@ -0,0 +1,25 @@ +DIR="$(dirname $0)" +WASM_DIR="$DIR/target/wasm32-unknown-unknown" +WASM_NAME="$(cat "$DIR/Cargo.toml" | grep name | sed 's/name = "//' | sed 's/"//g')" +APP_DIR="$DIR/dist/" + +if [ ! -d "$APP_DIR" ]; then + mkdir "$APP_DIR" +fi +cp "$DIR/static/"* "$APP_DIR" + +if [ -z "$(which cargo)" ]; then + echo 'Must install `cargo` before proceeding. Please see https://rustup.rs/ for more information.' + exit 1 +fi + +if [ -z "$(which wasm-bindgen)" ]; then + echo "Installing wasm-bindgen-cli" + cargo install wasm-bindgen-cli +fi + +cargo +nightly build --target=wasm32-unknown-unknown && \ + wasm-bindgen "$WASM_DIR/debug/$WASM_NAME.wasm" --out-dir "$APP_DIR" --no-typescript && \ + # Have to use --mode=development so we can patch out the call to instantiateStreaming + "$DIR/node_modules/webpack-cli/bin/cli.js" --mode=development "$APP_DIR/app_loader.js" -o "$APP_DIR/bundle.js" && \ + sed -i 's/.*instantiateStreaming.*//g' "$APP_DIR/bundle.js" diff --git a/minimal/package.json b/minimal/package.json new file mode 100644 index 0000000..410bc45 --- /dev/null +++ b/minimal/package.json @@ -0,0 +1,66 @@ +{ + "name": "electron_percy_wasm", + "productName": "electron_percy_wasm", + "version": "1.0.0", + "description": "My Electron application description", + "main": "dist/index.js", + "scripts": { + "prestart": "./build.sh", + "start": "electron-forge start", + "package": "electron-forge package", + "make": "electron-forge make", + "lint": "eslint src" + }, + "keywords": [], + "author": "bspeice", + "license": "MIT", + "config": { + "forge": { + "make_targets": { + "win32": [ + "squirrel" + ], + "darwin": [ + "zip" + ], + "linux": [ + "deb", + "rpm" + ] + }, + "electronPackagerConfig": { + "packageManager": "yarn" + }, + "electronWinstallerConfig": { + "name": "electron_percy_wasm" + }, + "electronInstallerDebian": {}, + "electronInstallerRedhat": {}, + "github_repository": { + "owner": "", + "name": "" + }, + "windowsStoreConfig": { + "packageName": "", + "name": "electron_percy_wasm" + } + } + }, + "dependencies": { + "electron-compile": "^6.4.3", + "webpack": "^4.17.1", + "webpack-cli": "^3.1.0" + }, + "devDependencies": { + "babel-plugin-transform-async-to-generator": "^6.24.1", + "babel-preset-env": "^1.7.0", + "babel-preset-react": "^6.24.1", + "electron-forge": "^5.2.2", + "electron-prebuilt-compile": "2.0.7", + "eslint": "^3", + "eslint-config-airbnb": "^15", + "eslint-plugin-import": "^2", + "eslint-plugin-jsx-a11y": "^5", + "eslint-plugin-react": "^7" + } +} diff --git a/minimal/src/lib.rs b/minimal/src/lib.rs new file mode 100644 index 0000000..6f0be4f --- /dev/null +++ b/minimal/src/lib.rs @@ -0,0 +1,58 @@ +#[macro_use] +extern crate cfg_if; + +extern crate wasm_bindgen; +use wasm_bindgen::prelude::*; + +cfg_if! { + // When the `console_error_panic_hook` feature is enabled, we can call the + // `set_panic_hook` function to get better error messages if we ever panic. + if #[cfg(feature = "console_error_panic_hook")] { + extern crate console_error_panic_hook; + use console_error_panic_hook::set_once as set_panic_hook; + } +} + +cfg_if! { + // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global + // allocator. + if #[cfg(feature = "wee_alloc")] { + extern crate wee_alloc; + #[global_allocator] + static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + } +} + +// Definitions of the functionality available in JS, which wasm-bindgen will +// generate shims for today (and eventually these should be near-0 cost!) +// +// These definitions need to be hand-written today but the current vision is +// that we'll use WebIDL to generate this `extern` block into a crate which you +// can link and import. There's a tracking issue for this at +// https://github.com/rustwasm/wasm-bindgen/issues/42 +// +// In the meantime these are written out by hand and correspond to the names and +// signatures documented on MDN, for example +#[wasm_bindgen] +extern "C" { + type HTMLDocument; + static document: HTMLDocument; + #[wasm_bindgen(method)] + fn createElement(this: &HTMLDocument, tagName: &str) -> Element; + #[wasm_bindgen(method, getter)] + fn body(this: &HTMLDocument) -> Element; + + type Element; + #[wasm_bindgen(method, setter = innerHTML)] + fn set_inner_html(this: &Element, html: &str); + #[wasm_bindgen(method, js_name = appendChild)] + fn append_child(this: &Element, other: Element); +} + +// Called by our JS entry point to run the example +#[wasm_bindgen] +pub fn run() { + let val = document.createElement("p"); + val.set_inner_html("IT IS ALIVE"); + document.body().append_child(val); +} diff --git a/percy/static/app_loader.js b/minimal/static/app_loader.js similarity index 65% rename from percy/static/app_loader.js rename to minimal/static/app_loader.js index 6485ea7..e5fd37b 100644 --- a/percy/static/app_loader.js +++ b/minimal/static/app_loader.js @@ -1,6 +1,7 @@ -const app = import("./app") -app.then(() => { +const app = import("./rust_webpack") +app.then(module => { console.log("Finished resolving application bundle") + module.run() }, (e) => { console.log("Unable to resolve application bundle: ") console.log(e) diff --git a/minimal/static/index.html b/minimal/static/index.html new file mode 100644 index 0000000..0cf787a --- /dev/null +++ b/minimal/static/index.html @@ -0,0 +1,10 @@ + + + + + + + + +