Select theme:
Radzen applications are usually deployed on a web server. Sometimes it makes more sense for an application to run on the user machine. In this post I will show you how to create a desktop application from an existing Angular one via Electron.
First of all what is Electron? It is a GitHub project that enables web developers to create desktop applications with JavaScript and HTML. You are probably using an electron application right now - Visual Studio Code, Slack, Skype or Discord.
The first step is to make a production build of your Radzen Angular application. The whole procedure is documented in the manual build help article.
I will use the Radzen CRM Angular sample application.
client
directory of your Radzen application.npm install
.src/environments/environment.prod.ts
file and change all localhost references to the location of your production web server.
For our example I will update environment.prod.ts like this:export const environment = {
serverMethodsUrl: 'https://crm.radzen.com/',
crm: 'https://crm.radzen.com/odata/CRM',
securityUrl: 'https://crm.radzen.com/auth',
production: true
};
.\node_modules\.bin\ng build --base-href="./" --prod
./node_modules/.bin/ng build --base-href="./" --prod
That's it! The Radzen application is now built for production (well at least the Angular part of it).
The second step is to create the Electron application.
client\dist
directory which contains the distribution of the Angular application created in the previous step.package.json
file in the dist
directory:{
"name": "radzen-crm",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^7.1.8"
}
}
dist
directory as main.js
:const {app, BrowserWindow} = require('electron');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;
function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 1280,
height: 960,
webPreferences: {
nodeIntegration: true
}
});
// and load the index.html of the app.
win.loadFile('index.html');
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow();
}
});
npm install
in the dist
directory to install the Electron dependencies.npm start
.Now the final part - to create distributions (installers) for macOS and Windows.
For this purpose we will use Electron Builder.
dist\package.json
file and update it with the following content:{
"name": "radzen-crm",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"dist:mac": "electron-builder --mac",
"dist:win": "electron-builder --win",
"start": "electron ."
},
"devDependencies": {
"electron": "^7.1.8",
"electron-builder": "^21.2.0"
}
}
npm install
again to instal Electron Builder.npm run dist:win
. To create a macOS DMG run npm run dist:mac
(you should be running on macOS though).After Electron Builder does its business you will see the distributions in the dist
directory:
dist\radzen-crm-1.0.0.dmg
dist\radzen-crm Setup 1.0.0.exe
If your Radzen application uses security you will hit an issue during logout - the application screen will be empty.
This can be addressed by editing the client\src\app\security.service.ts
file and setting the logout method of the SecurityService class to:
logout() {
localStorage.removeItem(TOKEN);
this.router.navigateByUrl('/login');
}
You can add this file to Radzen's code generation ignore list. Also rebuild the application and create new distributions.
The package.json and main.js files used in this blog post are available here.
Cheers!
Radzen is free to use. You can also test the premium features for 15 days.
Download NowSelect theme: