vite-react-neutralino-template
May 25, 2024 ยท View on GitHub
Boilerplate for a desktop react app with Vite + React + Neutralino
Start with yarn run init and then use yarn start for development and yarn build for production.
Check the branches of this repo for different version combinations.
Initialize
Before first use, you should run command yarn run init which will essentially run neu update. This command downloads the Neutralino binaries based on neutralino.config.json's cli.binaryVersion property.
Note that you can also install custom versions of the binaries with neu update <version>. If you run neu update --latest, the latest version will be downloaded regardless of the property and the property will be updated.
Development
During development, please run command yarn start. Let's see what it does:
neu runis run with some parameters overridden likewindow-enable-inspectortotruefor DevTools.neu runalso runs the corresponding command in theneutralino.config.json'scli.frontendLibrary.devCommandproperty- the value of this property is
yarn vite-devwhich runs thevite-devnpm script or basically thevitecommand to start a Vite development server which listens for changes
- the value of this property is
index.htmlis patched (thanks to theneutralino.config.json'scli.frontendLibrary.patchFileproperty) which changes./__neutralino_globals.jstohttp://localhost:<port>/__neutralino_globals.jswith the correct Neutralino port.- Once the Neutralino window is closed, this value is reverted. If you terminate it in some other way, you need to manually revert the value when building the final app.
- Neutralino starts listening for the Development server's url defined in
neutralino.config.json'scli.frontendLibrary.devUrlproperty - While it waits, the parallel vite server is starting at port 5173, defined in
vite.config.js. When the server is ready, the Neutralino window opens. - Neutralino will open the server's URL
- It will load the
__neutralino_globals.jsfile for global configs like NL_PORT and NL_TOKEN for authentication of the communication between Neutralino's server and the Vite server. - Sometimes this communication does not work directly (NL_TOKEN is empty) and that's why we have an if statement in
main.jsx:if (import.meta.env.DEV && !window.NL_TOKEN) {(run only in development mode). - After calling
init()inmain.jsx, Neutralino tries to open a WebSocket connection using NL_TOKEN and NL_PORT - You do your development, freely save files and Neutralino refreshes them.
- At the end, you click the X on the Neutralino window, which reverts the changes in
index.htmland terminates (operations defined inmain.jsx; this is a workaround for a bug withneutralino.config.json'smodes.window.exitProcessOnCloseproperty)
Building for production
When you are building for production, you run yarn build which runs neu build
- The
buildCommandfor that isyarn vite-buildorNODE_ENV=production vite build- This command tells Vite to build the project in
/dist
- This command tells Vite to build the project in
- Neutralino then takes these build files and builds the Neutralino final product in
/dist/neutralinojs