Note: This template project of the master branch was migrated to use NEON version! If you want to use the node-ffi-napi version, then switch to the branch/node-ffi-napi.
This is the template project with these stacks:
Rustnative code library project for backend business logicsNode.jswithreact+rescripts+electron+electron-rebuildfor frontend presentation logics- -> see also
package.jsonandsrc/,public/,assets/directories.resctiptsrc.js,.webpack.config.jsfiles
- -> see also
Prepare these toolchains in advance:
- Rust toolchain -> https://www.rust-lang.org/tools/install
- Node.js toolchain -> https://nodejs.org/en/download/
And, these are optional technologies:
And, you will need if you development with Windows(It will be required by electron-rebuild):
windows-build-tools;npm -g i windows-build-tools- with
Visual C++; The standard toolchain ofNode.jsof Windows version
- with
Note: You can install manually or use a platform installers such as choco, brew, pacman(Arch Linux), apt(debian, Ubuntu) or etc.
- Method (a):
cargo-generate; Recommended
cargo generate --git https://github.com/usagi/template-rust-backend-with-electron-frontend.gitor
- Method (b): For general; But not recommended
- Copy ( or
git clone) this repos to your working directory. - Remove
cargo-generate.tomlfile. - Modify
Cargo.tomlto your project.
Cargo.tomland several setting files for Rust technology stack:Cargo.toml:[workspace]/membersandCargo.tomlfor each workspace.rust-toolchain; Change tostableorbetaif you need. The default isnightly..rustfmt.toml; Change to your style or remove it for simple if you like the standard rustfmt style. The default is like the Allman style.
package.jsonfor Node.js technology stack:namedescriptionversionprivateauthornameemailurl
buildappIdproductNamecopyrightmac- and add
win,linux, ... if needed.
- and add
scripts/electron-pack; Change the platform argument-wto your target platform such as-wmlif you need.
cargoecosystemcargo checkcargo test
neon-cli ( ≃cargo+ building suppliments ) ecosystemneon build; (†1)
yarnecosystem; It's customized for this stacksyarn dev≃neon build+start+electronyarn build(†1)yarn testyarn electron-pack≃cargo build --release+build+electron-builder
(†1): If you using a Windows environment then see also neon-bindings/neon#463 . And choose from below:
- To use
yarn dev.releaseand a release version scripts. - Or, modify the neon dependencies to
{ git = "https://github.com/usagi/neon", branch = "fix-463-debug-build-mode-in-windows" }these areneonandneon-buildin thedependenciesandbuild-dependenciesin thenative/Cargo.toml. - Or, wait merge the fixing and new releasing.
- Change ( or create or remove for simple )
README.mdandLICENSEfiles.
Note: This process is very important if you don't want to apply the MIT licensing for your app derived from this template. You don't forget it.
yarn upgrade
Note: The Rust technology stack part has no dependencies, thus you would not need update packages. Maybe, cargo-update is useful if you need a Rust technology stack part of your app derived this template in a future.
Well, done! Congratulations, you can begin to develop your app now.
- Unit testing only the backend:
yarn test=cargo test - Build the frontend and the backend:
yarn build=neon build - Run the app with build:
yarn dev=yarn build+yarn start - Package the app with build:
yarn electron-pack=build.release+ ... - And,
- You can use any
cargoandyarn(npm,npx, etc. ) or, Rust and Node.js technologies! - To see src.rs/lib/src/lib.rs and try to modify it at the first if you are newbie for Rust FFI technologies!
- To see public/preload.js and src/App.js and try to modify these at the first if you are newbie for Electron FFI technologies!
- You can use any
Good luck!!
- You don't need
ejectofcreate-react-appbecause this template usingrescripts - You can choose to any licensing for your app derived from this template. I author of this template applied MIT licensing for this template project only.
(Q1) How to add a more native lib?
cd src.rscargo new extra_my_lib --lib- Add the new workspace definition into the top level
Cargo.toml - Modify
Cargo.tomlof the new workspace; Maybe at least, you will needcdylibconfiguration. - Modify
package.json; Maybe at least, you will need add the new definition intobuild.{win|mac|linux}.extraFiles. - Well done! You can use your new extra lib in your front-end of electron.
(Q2) How to add a resource file for use from a native lib?
- Maybe, you will need add the new definition into
build.extraFiles.
(Q3) Why it use NEON, not node-ffi-napi?
See: https://github.com/usagi/neon-vs-node-ffi-napi#neon-vs-node-ffi-napi
- Rust side:
- The Rust FFI Omnibus; Very useful for FFI marsharing basics!
- docs:
- std::os::raw ;
c_char,c_ulonglong, ... - std::ffi ;
CString,CStr, ...
- std::os::raw ;
- Node.js sise:
- blackhat ASIA 2019 DOYENSEC Preloading Insecurity In Your Electron; Useful to your understanding for process isolation and IPC mechanical of Electron's preload feature
- Electron Documentation:
- npm:
Note: This licensing is applying to this original template only. It is not a copy-left. You choose any licensing to your app derived this template project! And I author of this original template would not be effect your derived project anything!
USAGI.NETWORK / Usagi Ito https://usagi.network/
