Electron is an open - source framework developed by GitHub. It uses Chromium and Node.js to enable developers to build cross - platform desktop applications with web technologies. The main process in Electron is responsible for creating and managing browser windows, while the renderer process runs the web pages in those windows. Communication between the main and renderer processes is possible through IPC (Inter - Process Communication).
TypeScript is a programming language developed and maintained by Microsoft. It adds static typing to JavaScript, which helps catch errors early in the development process. TypeScript code is transpiled into plain JavaScript that can be run in any JavaScript - compatible environment.
An Electron TypeScript boilerplate is a project template that comes with pre - configured settings for using Electron and TypeScript together. It typically includes a proper project structure, TypeScript configuration files (like tsconfig.json
), build scripts, and often a development server for hot - reloading.
There are several popular Electron TypeScript boilerplates available on GitHub. For example, electron - forge
with TypeScript support. You can use the following command to create a new project using electron - forge
:
npx create - electron - app my - app --template=typescript
cd my - app
The typical project structure of an Electron TypeScript boilerplate might look like this:
my - app/
├── src/
│ ├── main/
│ │ └── main.ts # Main process code
│ └── renderer/
│ └── renderer.ts # Renderer process code
├── public/
│ └── index.html # Main HTML file
├── package.json
├── tsconfig.json
└── webpack.config.js (if using webpack)
To start the development server and run the Electron application, use the following command:
npm start
To build the application for different platforms, you can use the build command:
npm run make
When working with Electron, communication between the main and renderer processes is crucial. Here is an example of using IPC in a TypeScript - based Electron application:
Main process (main.ts
)
import { app, BrowserWindow, ipcMain } from 'electron';
let mainWindow: BrowserWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
mainWindow.loadFile('public/index.html');
ipcMain.on('message - from - renderer', (event, arg) => {
console.log(arg);
event.sender.send('message - from - main', 'Hello from main process');
});
}
app.whenReady().then(createWindow);
app.on('window - all - closed', () => {
if (process.platform!== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
Renderer process (renderer.ts
)
import { ipcRenderer } from 'electron';
ipcRenderer.send('message - from - renderer', 'Hello from renderer process');
ipcRenderer.on('message - from - main', (event, arg) => {
console.log(arg);
});
Proper error handling is essential in both the main and renderer processes. In TypeScript, you can use try - catch blocks to handle errors gracefully. For example, when loading a file in the main process:
import { app, BrowserWindow } from 'electron';
function createWindow() {
let mainWindow: BrowserWindow;
try {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
mainWindow.loadFile('public/index.html');
} catch (error) {
console.error('Error creating window:', error);
}
}
app.whenReady().then(createWindow);
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
});
<meta http - equiv="Content - Security - Policy" content="default - src'self'; script - src'self'">
// component.ts
export function renderComponent() {
const element = document.createElement('div');
element.textContent = 'This is a component';
document.body.appendChild(element);
}
// renderer.ts
import { renderComponent } from './component';
renderComponent();
async function loadModule() {
const { someFunction } = await import('./module');
someFunction();
}
Electron TypeScript boilerplates provide a convenient way to start building cross - platform desktop applications. By understanding the fundamental concepts, following the usage methods, adopting common practices, and implementing best practices, developers can create robust, secure, and high - performance applications. Whether you are a beginner or an experienced developer, leveraging these boilerplates can significantly speed up your development process.