A minimal Electron + Rust example.
This repo is intended to offer an understandable example of calling into Rust from Electron.
If you plan to use this as a starting point for your project, definitely update the package.json
and Cargo.toml
with your information.
This template was created by synthesizing information from the following:
- Rust (≥ 1.51.0) 🦀
- wasm-pack:
cargo install wasm-pack
- Mandatory reading! 📚
Here's a one-liner:
git clone https://github.com/tylerdaddio/electron-rust-quick-start && cd electron-rust-quick-start && npm i && npm run wasm-build && npm start
And here's that one-linear broken into steps:
# Clone this repo
git clone https://github.com/tylerdaddio/electron-rust-quick-start
# Navigate into the folder
cd electron-rust-quick-start
# Download required npm packages listed in `package.json` (i.e. Electron & its dependencies)
npm install
# Build the sample `rust-lib` project into WebAssembly & JavaScript bindings. (See the full command in the `package.json`.)
npm run wasm-build
# Run the application. A window should open and an alert should appear.
npm start
The
Content-Security-Policy
in theindex.html
is set to enableunsafe-eval
. That seems unsafe. Why is that unsafe thing there?
Good question!
At time of writing, unsafe-eval
is required in Chromium if you want to call into a WASM module. This shouldn't be required once wasm-eval
is standardized, but only maybe (?).
As Electron is built on a web brower and has access to the full resources of your computer, it is severely unsafe by default. Before getting started, please perform the mandatory reading 📚.
Here's some more stuff I've found helpful, inspiring, or both: