electron-vite-react/README.md

157 lines
5.7 KiB
Markdown
Raw Normal View History

2021-11-11 08:17:54 +08:00
# vite-react-electron
2021-11-06 22:27:23 +08:00
2021-11-11 08:17:54 +08:00
![GitHub stars](https://img.shields.io/github/stars/caoxiemeihao/vite-react-electron?color=fa6470&style=flat)
![GitHub issues](https://img.shields.io/github/issues/caoxiemeihao/vite-react-electron?color=d8b22d&style=flat)
![GitHub license](https://img.shields.io/github/license/caoxiemeihao/vite-react-electron?style=flat)
2021-11-07 18:10:55 +08:00
[![Required Node.JS >= v14.17.0](https://img.shields.io/static/v1?label=node&message=%3E=14.17.0&logo=node.js&color=3f893e&style=flat)](https://nodejs.org/about/releases)
2021-11-06 22:27:23 +08:00
2021-11-10 21:19:02 +08:00
**English | [简体中文](README.zh-CN.md)**
2021-12-18 10:23:56 +08:00
## Overview
2022-04-26 08:28:53 +08:00
This is a `Vite`-integrated `Electron` template built with simplification in mind.
2021-12-18 10:23:56 +08:00
2022-04-26 08:28:53 +08:00
The repo contains only the most basic files, dependencies and functionalities to ensure flexibility for various scenarios.
2021-12-18 10:23:56 +08:00
2022-04-26 08:28:53 +08:00
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. 😋
2021-12-18 10:23:56 +08:00
2022-04-25 08:51:42 +08:00
## Quick start
2022-03-13 08:45:27 +08:00
```sh
npm create electron-vite
```
2022-04-25 08:51:42 +08:00
![electron-vite-react.gif](https://github.com/electron-vite/electron-vite-react/blob/main/packages/renderer/public/electron-vite-react.gif?raw=true)
## Debug
![electron-vite-react-debug.gif](https://github.com/electron-vite/electron-vite-react/blob/main/packages/renderer/public/electron-vite-react-debug.gif?raw=true)
2022-03-13 08:45:27 +08:00
2022-04-25 08:51:42 +08:00
<!--
2022-03-13 08:45:27 +08:00
```sh
# clone the project
2022-02-03 08:10:10 +08:00
git clone https://github.com/caoxiemeihao/vite-react-electron.git
2021-11-06 22:27:23 +08:00
# open the project directory
cd vite-react-electron
2021-11-06 22:27:23 +08:00
# install dependencies
npm install
2021-11-06 22:27:23 +08:00
# start the application
npm run dev
2021-11-06 22:27:23 +08:00
# make a production build
npm run build
```
2022-04-25 08:51:42 +08:00
-->
## Directory structure
2021-11-10 21:19:02 +08:00
2022-02-09 09:05:13 +08:00
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.
2021-12-29 09:33:21 +08:00
2021-11-10 21:19:02 +08:00
```tree
├── build Resources for the production build
2022-04-25 08:51:42 +08:00
| ├── 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
|
2022-02-09 09:05:13 +08:00
├── dist Generated after build according to the "packages" directory
2022-04-25 08:51:42 +08:00
| ├── main
| ├── preload
| └── renderer
|
├── release Generated after production build, contains executables
2022-04-25 08:51:42 +08:00
| └──{version}
| ├── win-unpacked Contains unpacked application executable
| └── Setup.exe Installer for the application
|
2021-11-10 21:19:02 +08:00
├── scripts
2022-04-25 08:51:42 +08:00
| ├── build.mjs Develop script -> npm run build
| └── watch.mjs Develop script -> npm run dev
|
2022-02-09 09:05:13 +08:00
├── packages
2022-04-25 08:51:42 +08:00
| ├── main Main-process source code
| | └── vite.config.ts
| ├── preload Preload-script source code
| | └── vite.config.ts
| └── renderer Renderer-process source code
| └── vite.config.ts
2021-11-10 21:19:02 +08:00
```
## Use Electron and NodeJS API
2021-12-21 09:30:05 +08:00
> 🚧 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)**
2021-12-21 09:30:05 +08:00
#### Invoke Electron and NodeJS API in `Preload-script`
2021-12-21 09:30:05 +08:00
2022-02-09 09:05:13 +08:00
- **packages/preload/index.ts**
2022-01-07 08:39:23 +08:00
```typescript
import fs from "fs"
import { contextBridge, ipcRenderer } from "electron"
2022-01-07 08:39:23 +08:00
// --------- Expose some API to Renderer-process. ---------
contextBridge.exposeInMainWorld("fs", fs)
contextBridge.exposeInMainWorld("ipcRenderer", ipcRenderer)
```
2022-01-07 08:39:23 +08:00
2022-02-09 09:05:13 +08:00
- **packages/renderer/src/global.d.ts**
2022-01-07 08:39:23 +08:00
```typescript
// Defined in the window
interface Window {
2022-02-03 08:36:51 +08:00
fs: typeof import("fs")
ipcRenderer: import("electron").IpcRenderer
}
```
2022-01-07 08:39:23 +08:00
2022-02-09 09:05:13 +08:00
- **packages/renderer/src/main.ts**
2022-01-07 08:39:23 +08:00
```typescript
// Use Electron and NodeJS API in the Renderer-process
console.log("fs", window.fs)
console.log("ipcRenderer", window.ipcRenderer)
```
2022-01-07 08:39:23 +08:00
## Use SerialPort, SQLite3, or other node-native addons in the Main-process
2022-01-31 12:49:04 +08:00
2022-02-05 08:01:45 +08:00
- 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.
2022-01-31 12:49:04 +08:00
2022-02-05 08:01:45 +08:00
- 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.
2022-01-31 12:49:04 +08:00
2022-02-09 09:05:13 +08:00
**Click to see more** 👉 [packages/main/vite.config.ts](https://github.com/caoxiemeihao/vite-react-electron/blob/main/packages/main/vite.config.ts)
2022-01-31 12:49:04 +08:00
```js
export default {
2022-02-03 08:36:51 +08:00
build: {
// built lib for Main-process, Preload-script
lib: {
entry: "index.ts",
formats: ["cjs"],
fileName: () => "[name].js",
},
rollupOptions: {
// configuration here
external: ["serialport", "sqlite3"],
},
},
2022-01-31 12:49:04 +08:00
}
```
## `dependencies` vs `devDependencies`
2022-02-05 08:01:45 +08:00
- First, you need to know if your dependencies are needed after the application is packaged.
2022-01-31 12:49:04 +08:00
2022-02-05 08:01:45 +08:00
- 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.
2022-01-31 12:49:04 +08:00
2022-02-05 08:01:45 +08:00
- 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.
2022-01-31 12:49:04 +08:00
2022-04-25 08:51:42 +08:00
<!--
## Result
2021-11-08 21:23:40 +08:00
2021-11-11 08:17:54 +08:00
<img width="400px" src="https://raw.githubusercontent.com/caoxiemeihao/blog/main/vite-react-electron/react-win.png" />
2022-04-25 08:51:42 +08:00
-->