electron-vite-template
v1.1.12
Published
Reusable Electron + Vite starter template
Readme
Electron + Vite Template
A minimal, modern Electron application template built with Vite for fast development and TypeScript support.
Features
- ⚡ Vite for fast development and hot reload
- 🔷 TypeScript support for both main and renderer processes
- 🔒 Security best practices with context isolation and preload scripts
- 🛠️ Concurrent development - Vite dev server and Electron run together
- 📦 Production builds with electron-builder
- 🎯 Clean project structure with separate configs for main/renderer
Project Structure
├── electron/ # Electron main process
│ ├── main.ts # Main process entry point
│ ├── preload.ts # Preload script for secure IPC
│ └── tsconfig.json # TypeScript config for main process
├── src/ # Renderer process (web app)
│ ├── main.ts # Renderer entry point
│ └── style.css # App styles
├── dist-electron/ # Compiled Electron files (auto-generated)
├── dist-renderer/ # Compiled renderer files (auto-generated)
├── index.html # HTML template
├── vite.config.ts # Vite configuration
├── tsconfig.json # TypeScript config for renderer
└── package.json # Dependencies and scriptsGetting Started
Use as an npm/bun template package
Generate a fresh app from this template without cloning:
npx electron-vite-template my-appor with Bun:
bunx electron-vite-template my-appThen run:
cd my-app
bun install
bun run devPrerequisites
- Node.js (v16 or higher)
- Bun
Installation
- Clone or download this template
- Install dependencies:
bun installPublish to npm
When you want to publish a new version:
bun install
bun run build
npm publish --access publicDevelopment
Start the development server with hot reload:
bun run devThis command:
- Compiles the Electron main process TypeScript files
- Starts the Vite dev server on
http://localhost:5173 - Launches Electron which loads the dev server
- Opens developer tools automatically
Building
Build the renderer process only:
vite buildBuild the Electron main process:
bun run build:electronBuild everything for production:
bun run buildCreate distributable packages:
bun run distHow It Works
Development Mode Detection
The template automatically detects whether to run in development or production mode by checking if the built renderer files exist:
const rendererPath = path.join(__dirname, '../dist-renderer/index.html')
const isDev = !existsSync(rendererPath)
if (isDev) {
mainWindow.loadURL('http://localhost:5173') // Vite dev server
} else {
mainWindow.loadFile(rendererPath) // Built files
}Security Setup
The template follows Electron security best practices:
- Context Isolation: Enabled to isolate the main world from the isolated world
- Node Integration: Disabled in renderer for security
- Preload Script: Used for secure communication between main and renderer processes
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}TypeScript Configuration
Two separate TypeScript configurations:
- Main Process (
electron/tsconfig.json): Targets CommonJS for Node.js environment - Renderer Process (
tsconfig.json): Targets ESNext for modern browser environment
Vite Configuration
Configured for Electron with:
- Base path set to
./for relative asset loading - Output directory set to
dist-renderer - Development server on port 5173
Scripts
bun run dev- Start development with hot reloadbun run build- Build for productionbun run build:electron- Compile Electron main process onlybun run dist- Create distributable packagesbun run preview- Preview production build
Adding Features
IPC Communication
Add methods to the preload script for secure communication:
// electron/preload.ts
contextBridge.exposeInMainWorld('electronAPI', {
openFile: () => ipcRenderer.invoke('dialog:openFile'),
saveFile: (data: string) => ipcRenderer.invoke('file:save', data)
})Handle in main process:
// electron/main.ts
import { ipcMain, dialog } from 'electron'
ipcMain.handle('dialog:openFile', async () => {
const result = await dialog.showOpenDialog({})
return result.filePaths
})Adding Dependencies
For renderer process (web technologies):
bun add package-nameFor main process (Node.js):
bun add package-name
bun add -d @types/package-name # if TypeScript types neededCustomization
- Modify
src/main.tsandsrc/style.cssfor your app's UI - Update
electron/main.tsfor main process logic - Configure
vite.config.tsfor build customization - Update
package.jsonfor app metadata and build settings
License
MIT
