electron-vite-template 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +219 -0
- package/bun.lock +953 -0
- package/electron/main.ts +40 -0
- package/electron/preload.ts +8 -0
- package/electron/tsconfig.json +15 -0
- package/index.html +13 -0
- package/package.json +43 -0
- package/scripts/create.cjs +92 -0
- package/src/main.ts +19 -0
- package/src/style.css +65 -0
- package/template.gitignore +37 -0
- package/tsconfig.json +19 -0
- package/vite.config.ts +11 -0
package/README.md
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# Electron + Vite Template
|
|
2
|
+
|
|
3
|
+
A minimal, modern Electron application template built with Vite for fast development and TypeScript support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ⚡ **Vite** for fast development and hot reload
|
|
8
|
+
- 🔷 **TypeScript** support for both main and renderer processes
|
|
9
|
+
- 🔒 **Security best practices** with context isolation and preload scripts
|
|
10
|
+
- 🛠️ **Concurrent development** - Vite dev server and Electron run together
|
|
11
|
+
- 📦 **Production builds** with electron-builder
|
|
12
|
+
- 🎯 **Clean project structure** with separate configs for main/renderer
|
|
13
|
+
|
|
14
|
+
## Project Structure
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
├── electron/ # Electron main process
|
|
18
|
+
│ ├── main.ts # Main process entry point
|
|
19
|
+
│ ├── preload.ts # Preload script for secure IPC
|
|
20
|
+
│ └── tsconfig.json # TypeScript config for main process
|
|
21
|
+
├── src/ # Renderer process (web app)
|
|
22
|
+
│ ├── main.ts # Renderer entry point
|
|
23
|
+
│ └── style.css # App styles
|
|
24
|
+
├── dist-electron/ # Compiled Electron files (auto-generated)
|
|
25
|
+
├── dist-renderer/ # Compiled renderer files (auto-generated)
|
|
26
|
+
├── index.html # HTML template
|
|
27
|
+
├── vite.config.ts # Vite configuration
|
|
28
|
+
├── tsconfig.json # TypeScript config for renderer
|
|
29
|
+
└── package.json # Dependencies and scripts
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Getting Started
|
|
33
|
+
|
|
34
|
+
### Use as an npm/bun template package
|
|
35
|
+
|
|
36
|
+
Generate a fresh app from this template without cloning:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npx electron-vite-template my-app
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
or with Bun:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
bunx electron-vite-template my-app
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Then run:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
cd my-app
|
|
52
|
+
bun install
|
|
53
|
+
bun run dev
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Prerequisites
|
|
57
|
+
|
|
58
|
+
- Node.js (v16 or higher)
|
|
59
|
+
- Bun
|
|
60
|
+
|
|
61
|
+
### Installation
|
|
62
|
+
|
|
63
|
+
1. Clone or download this template
|
|
64
|
+
2. Install dependencies:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
bun install
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Publish to npm
|
|
71
|
+
|
|
72
|
+
When you want to publish a new version:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
bun install
|
|
76
|
+
bun run build
|
|
77
|
+
npm publish --access public
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Development
|
|
81
|
+
|
|
82
|
+
Start the development server with hot reload:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
bun run dev
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
This command:
|
|
89
|
+
1. Compiles the Electron main process TypeScript files
|
|
90
|
+
2. Starts the Vite dev server on `http://localhost:5173`
|
|
91
|
+
3. Launches Electron which loads the dev server
|
|
92
|
+
4. Opens developer tools automatically
|
|
93
|
+
|
|
94
|
+
### Building
|
|
95
|
+
|
|
96
|
+
Build the renderer process only:
|
|
97
|
+
```bash
|
|
98
|
+
vite build
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Build the Electron main process:
|
|
102
|
+
```bash
|
|
103
|
+
bun run build:electron
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Build everything for production:
|
|
107
|
+
```bash
|
|
108
|
+
bun run build
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Create distributable packages:
|
|
112
|
+
```bash
|
|
113
|
+
bun run dist
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## How It Works
|
|
117
|
+
|
|
118
|
+
### Development Mode Detection
|
|
119
|
+
|
|
120
|
+
The template automatically detects whether to run in development or production mode by checking if the built renderer files exist:
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
const rendererPath = path.join(__dirname, '../dist-renderer/index.html')
|
|
124
|
+
const isDev = !existsSync(rendererPath)
|
|
125
|
+
|
|
126
|
+
if (isDev) {
|
|
127
|
+
mainWindow.loadURL('http://localhost:5173') // Vite dev server
|
|
128
|
+
} else {
|
|
129
|
+
mainWindow.loadFile(rendererPath) // Built files
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Security Setup
|
|
134
|
+
|
|
135
|
+
The template follows Electron security best practices:
|
|
136
|
+
|
|
137
|
+
- **Context Isolation**: Enabled to isolate the main world from the isolated world
|
|
138
|
+
- **Node Integration**: Disabled in renderer for security
|
|
139
|
+
- **Preload Script**: Used for secure communication between main and renderer processes
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
webPreferences: {
|
|
143
|
+
nodeIntegration: false,
|
|
144
|
+
contextIsolation: true,
|
|
145
|
+
preload: path.join(__dirname, 'preload.js')
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### TypeScript Configuration
|
|
150
|
+
|
|
151
|
+
Two separate TypeScript configurations:
|
|
152
|
+
|
|
153
|
+
- **Main Process** (`electron/tsconfig.json`): Targets CommonJS for Node.js environment
|
|
154
|
+
- **Renderer Process** (`tsconfig.json`): Targets ESNext for modern browser environment
|
|
155
|
+
|
|
156
|
+
### Vite Configuration
|
|
157
|
+
|
|
158
|
+
Configured for Electron with:
|
|
159
|
+
- Base path set to `./` for relative asset loading
|
|
160
|
+
- Output directory set to `dist-renderer`
|
|
161
|
+
- Development server on port 5173
|
|
162
|
+
|
|
163
|
+
## Scripts
|
|
164
|
+
|
|
165
|
+
- `bun run dev` - Start development with hot reload
|
|
166
|
+
- `bun run build` - Build for production
|
|
167
|
+
- `bun run build:electron` - Compile Electron main process only
|
|
168
|
+
- `bun run dist` - Create distributable packages
|
|
169
|
+
- `bun run preview` - Preview production build
|
|
170
|
+
|
|
171
|
+
## Adding Features
|
|
172
|
+
|
|
173
|
+
### IPC Communication
|
|
174
|
+
|
|
175
|
+
Add methods to the preload script for secure communication:
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
// electron/preload.ts
|
|
179
|
+
contextBridge.exposeInMainWorld('electronAPI', {
|
|
180
|
+
openFile: () => ipcRenderer.invoke('dialog:openFile'),
|
|
181
|
+
saveFile: (data: string) => ipcRenderer.invoke('file:save', data)
|
|
182
|
+
})
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Handle in main process:
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
// electron/main.ts
|
|
189
|
+
import { ipcMain, dialog } from 'electron'
|
|
190
|
+
|
|
191
|
+
ipcMain.handle('dialog:openFile', async () => {
|
|
192
|
+
const result = await dialog.showOpenDialog({})
|
|
193
|
+
return result.filePaths
|
|
194
|
+
})
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Adding Dependencies
|
|
198
|
+
|
|
199
|
+
For renderer process (web technologies):
|
|
200
|
+
```bash
|
|
201
|
+
bun add package-name
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
For main process (Node.js):
|
|
205
|
+
```bash
|
|
206
|
+
bun add package-name
|
|
207
|
+
bun add -d @types/package-name # if TypeScript types needed
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Customization
|
|
211
|
+
|
|
212
|
+
- Modify `src/main.ts` and `src/style.css` for your app's UI
|
|
213
|
+
- Update `electron/main.ts` for main process logic
|
|
214
|
+
- Configure `vite.config.ts` for build customization
|
|
215
|
+
- Update `package.json` for app metadata and build settings
|
|
216
|
+
|
|
217
|
+
## License
|
|
218
|
+
|
|
219
|
+
MIT
|