Effortlessly start your project using Angular 17 and Electron 27, and create your amazing desktop applications!
- 📦 Packaging:
electron-builder
is already configured for packaging and distributing applications; - 🔥 Hot Reload: Watch live changes in your Angular application without restarting the electron app, while developing!;
- 🛡 Safety: This template enforces context isolation principle, making Electron app safer;
- 🏎 High performances: both Angular and Electron applications are compiled using
esbuild
, reducing bundle size and improving app performance; - ⚙ Storage: Persist data through executions using embedded
AppStore
; - 🧪Test suite:
jest
is already configured for unit tests.
-
Create a repository from this template clicking on Use this template button at the top of this page;
-
Clone the newly generated repository;
-
Install dependencies
npm install
-
Set a proper
productName
andappId
inelectron-builder.json
file;
Et voila! You are ready to build your amazing desktop app with Angular!
If you want to start the application in development mode, enabling hot reload, you will just need to run npm start
.
As stated in the Features section, using Node packages directly in the renderer process is not possible, since context isolation is enforced.
But there is an easy way to enable our Angular application to use packages like fs
or childProcess
:
-
Configure
ipcMain
listener in Electron process (main.ts
inelectron
folder). For example:import {ipcMain} from 'electron'; import {spawn} from 'child_process'; ... ipcMain.on('start-process', (event, args) => spawn('myProcess', args))
-
Expose a new API in
preload.ts
file:import { contextBridge, ipcRenderer } from 'electron'; contextBridge.exposeInMainWorld('runProcess', { run: (...args: any[]) => ipcRenderer.send('start-process', ...args) });
-
Extend
window
object insrc/renderer.d.ts
file:export {}; interface RunProcessApi { run: (...args: any[]) => void; } declare global{ interface Window{ runProcess: RunProcessApi } }
-
Add
preload
option to yourBrowserWindow
inmain.ts
fileconst win = new BrowserWindow({ ... webPreferences: { ... preload: path.join(__dirname, './preload.js'), ... }, ... });
After that, you will be able to call window.runProcess.run(...)
in your Angular application.
You can different preload scripts for different windows, but don't forget to update esbuild-electron
script in package.json
, to include all the scripts to be compiled.