https://github.com/bonevidy/vite-electron
https://github.com/bonevidy/vite-electron
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/bonevidy/vite-electron
- Owner: boneVidy
- License: mit
- Created: 2022-05-10T15:18:22.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-10T15:21:45.000Z (about 4 years ago)
- Last Synced: 2025-02-09T09:43:20.948Z (over 1 year ago)
- Language: TypeScript
- Size: 12.9 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vite-react-electron



[](https://nodejs.org/about/releases)
**English | [įŽäŊ䏿](README.zh-CN.md)**
## Overview
This is a `Vite`-integrated `Electron` template built with simplification in mind.
The repo contains only the most basic files, dependencies and functionalities to ensure flexibility for various scenarios.
You need a basic understanding of `Electron` and `Vite` to get started. But that's not mandatory - you can learn almost all the details by reading through the source code. Trust me, this repo is not that complex. đ
## Quick start
```sh
npm create electron-vite
```

## Debug

## Directory structure
Once `dev` or `build` npm-script is executed, the `dist` folder will be generated. It has the same structure as the `packages` folder, the purpose of this design is to ensure the correct path calculation.
```tree
âââ build Resources for the production build
| âââ icon.icns Icon for the application on macOS
| âââ icon.ico Icon for the application
| âââ installerIcon.ico Icon for the application installer
| âââ uninstallerIcon.ico Icon for the application uninstaller
|
âââ dist Generated after build according to the "packages" directory
| âââ main
| âââ preload
| âââ renderer
|
âââ release Generated after production build, contains executables
| âââ{version}
| âââ win-unpacked Contains unpacked application executable
| âââ Setup.exe Installer for the application
|
âââ scripts
| âââ build.mjs Develop script -> npm run build
| âââ watch.mjs Develop script -> npm run dev
|
âââ packages
| âââ main Main-process source code
| | âââ vite.config.ts
| âââ preload Preload-script source code
| | âââ vite.config.ts
| âââ renderer Renderer-process source code
| âââ vite.config.ts
```
## Use Electron and NodeJS API
> đ§ By default, Electron doesn't support the use of API related to Electron and NodeJS in the Renderer process, but someone might need to use it. If so, you can see the template đ **[electron-vite-boilerplate](https://github.com/caoxiemeihao/electron-vite-boilerplate)**
#### Invoke Electron and NodeJS API in `Preload-script`
- **packages/preload/index.ts**
```typescript
import fs from "fs"
import { contextBridge, ipcRenderer } from "electron"
// --------- Expose some API to Renderer-process. ---------
contextBridge.exposeInMainWorld("fs", fs)
contextBridge.exposeInMainWorld("ipcRenderer", ipcRenderer)
```
- **packages/renderer/src/global.d.ts**
```typescript
// Defined in the window
interface Window {
fs: typeof import("fs")
ipcRenderer: import("electron").IpcRenderer
}
```
- **packages/renderer/src/main.ts**
```typescript
// Use Electron and NodeJS API in the Renderer-process
console.log("fs", window.fs)
console.log("ipcRenderer", window.ipcRenderer)
```
## Use SerialPort, SQLite3, or other node-native addons in the Main-process
- First, you need to make sure that the dependencies in the `package.json` are NOT in the "devDependencies". Because the project will need them after packaged.
- Main-process, Preload-script are also built with Vite, and they're built as [build.lib](https://vitejs.dev/config/#build-lib).
So they just need to configure Rollup.
**Click to see more** đ [packages/main/vite.config.ts](https://github.com/caoxiemeihao/vite-react-electron/blob/main/packages/main/vite.config.ts)
```js
export default {
build: {
// built lib for Main-process, Preload-script
lib: {
entry: "index.ts",
formats: ["cjs"],
fileName: () => "[name].js",
},
rollupOptions: {
// configuration here
external: ["serialport", "sqlite3"],
},
},
}
```
## `dependencies` vs `devDependencies`
- First, you need to know if your dependencies are needed after the application is packaged.
- Like [serialport](https://www.npmjs.com/package/serialport), [sqlite3](https://www.npmjs.com/package/sqlite3) they are node-native modules and should be placed in `dependencies`. In addition, Vite will not build them, but treat them as external modules.
- Dependencies like [Vue](https://www.npmjs.com/package/vue) and [React](https://www.npmjs.com/package/react), which are pure javascript modules that can be built with Vite, can be placed in `devDependencies`. This reduces the size of the application.