I have just successfully built a desktop app from an angular application, and below are the steps :
1. Change directory to your angular app and install electron
cd my-angular-app/
npm i -D electron@latest
2. Create electron entry file
create a main.js file in the root directory of your project. This file will be the main entry point for the electron app and will contain the main API for the desktop app.
main.js content:
const { app, BrowserWindow } = require("electron");
const path = require("path");
const url = require("url");
let win;
function createWindow() {
win = new BrowserWindow({ width: 800, height: 600 });
// load the dist folder from Angular
win.loadURL(
url.format({
pathname: path.join(__dirname, '/dist/index.html'), // compiled verion of our app
protocol: "file:",
slashes: true
})
);
// The following is optional and will open the DevTools:
// win.webContents.openDevTools()
win.on("closed", () => {
win = null;
});
}
app.on("ready", createWindow);
// on macOS, closing the window doesn't quit the app
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
3. update package.json with your app details
Update the package.json file to point to electron main entry point, and fill in information about your app (app name, version, description, and author)
package.json
{
"name": "my-angular-app",
"version": "0.0.1",
"main": "main.js",
"author" : "my-name",
"description": "The app is about foo and bar",
...
}
4. Update the script in the package.json
Add a new NPM script which will first create a build and then launch the desktop application from the dist folder.
{
...
"scripts": {
"electron": "ng build --base-href ./ && electron .",
...
}
...
}
5. Update angular to set the build directory
In angular.json, set the build directory as we have set /dist/index.html in the main.js file.
...,
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist",
6. run the app
All things being equal, you should see the app lunched within a desktop window now after running the run command:
npm run electron
7. Building (packaging) the APP :
With tooling
You can use the following tools to distribute your application:
electron-forge
electron-builder
electron-packager
These tools will take care of all the steps you need to take to end up with a distributable Electron application, such as bundling your application, rebranding the executable, and setting the right icons.
Below is how you can package with electron-forge
1. Add Electron Forge as a development dependency of your app, and use its import command to set up Forge's scaffolding:
npm install --save-dev @electron-forge/cli
npx electron-forge import
2. Create a distributable using Forge's make command:
npm run make
Electron Forge creates the out folder where your package will be located:
// Example for macOS
out/
├── out/make/zip/darwin/x64/my-electron-app-darwin-x64-1.0.0.zip
├── ...
└── out/my-electron-app-darwin-x64/my-electron-app.app/Contents/MacOS/my-electron-app
I hope this helps a lot of people.