electron-vite-react/README.zh-CN.md

121 lines
4.4 KiB
Markdown
Raw Normal View History

2021-11-11 08:17:54 +08:00
# vite-react-electron
2021-11-10 21:03:59 +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-10 21:03:59 +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)
**[English](README.md) | 简体中文**
2021-12-18 10:23:56 +08:00
## 概述
2022-01-31 12:49:04 +08:00
- 十分简单的 Vite, React, Electron 整合模板
2021-12-18 10:23:56 +08:00
2022-01-31 12:49:04 +08:00
- 只包含最基本的依赖
2021-12-18 10:23:56 +08:00
2022-01-31 12:49:04 +08:00
- 扩展十分灵活
2021-12-18 10:23:56 +08:00
2021-11-10 21:03:59 +08:00
## 运行
```bash
# clone the project
2022-02-03 08:10:10 +08:00
git clone https://github.com/caoxiemeihao/vite-react-electron.git
2021-11-10 21:03:59 +08:00
# enter the project directory
2021-11-11 08:17:54 +08:00
cd vite-react-electron
2021-11-10 21:03:59 +08:00
2021-11-15 18:12:39 +08:00
# install dependency
npm install
2021-11-10 21:03:59 +08:00
# develop
2021-11-15 18:12:39 +08:00
npm run dev
2021-11-10 21:03:59 +08:00
```
## 目录
2021-12-29 09:33:21 +08:00
一旦启动或打包脚本执行过,会在根目录产生 **`dist` 文件夹,里面的文件夹同 `src` 一模一样**;在使用一些路径计算时,尤其是相对路径计算;`dist` 与 `src` 里面保持相同的目录结构能避开好多问题
2021-11-10 21:03:59 +08:00
```tree
2022-01-31 09:31:14 +08:00
├── dist 构建后,根据 src 目录生成
2021-12-29 09:33:21 +08:00
├ ├── main
├ ├── preload
├ ├── renderer
2021-11-10 21:03:59 +08:00
├── scripts
2022-01-31 09:31:14 +08:00
├ ├── build.mjs 项目构建脚本,对应 npm run build
├ ├── vite.config.mjs 主进程, 预加载脚本源码 vite 配置
├ ├── watch.mjs 项目开发脚本,对应 npm run dev
2021-11-10 21:03:59 +08:00
├── src
2022-01-31 09:31:14 +08:00
├ ├── main 主进程源码
├ ├── preload 预加载脚本源码
├ ├── renderer 渲染进程源码
├ ├── vite.config.ts 渲染进程 vite 配置
2021-11-10 21:03:59 +08:00
2021-11-12 09:01:59 +08:00
```
2022-01-21 17:22:02 +08:00
## 依赖放到 dependencies 还是 devDependencies
  对待 **Electron-Main、Preload-Script** 时 vite 会以 lib 形式打包 commonjs 格式代码;
如果碰 node 环境的包可以直接放到 dependencies 中vite 会解析为 require('xxxx')
electron-builder 打包时候会将 dependencies 中的包打包到 app.asar 里面
  对待 **Electron-Renderer** 时 vite 会以 ESM 格式解析代码;
像 vue、react 这种前端用的包可以直接被 vite 构建,所以不需要 vue、react 源码;
现实情况 vue、react 放到 dependencies 或 devDependencies 中都可以被正确构建;
但是放到 dependencies 会被 electron-builder 打包到 app.asar 里面导致包体变大;
所以放到 devDependencies 既能被正确构建还可以减小 app.asar 体积,一举两得
2021-12-21 09:30:05 +08:00
## 渲染进程使用 NodeJs API
2022-01-21 17:22:02 +08:00
> 🚧 因为安全的原因 Electron 默认不支持在 渲染进程 中使用 NodeJs API但是有些小沙雕就是想这么干拦都拦不住实在想那么干的话用另一个模板更方便 👉 **[electron-vite-boilerplate](https://github.com/caoxiemeihao/electron-vite-boilerplate)**
2021-12-21 09:30:05 +08:00
2022-01-21 17:20:36 +08:00
**推荐所有的 NodeJs、Electron API 通过 `Preload-script` 注入到 渲染进程中,例如:**
2021-12-21 09:30:05 +08:00
2022-01-07 08:39:23 +08:00
* **src/preload/index.ts**
```typescript
import fs from 'fs'
import { contextBridge, ipcRenderer } from 'electron'
2022-01-21 17:20:36 +08:00
// --------- Expose some API to Renderer-process. ---------
contextBridge.exposeInMainWorld('fs', fs)
contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer)
2022-01-07 08:39:23 +08:00
```
* **src/renderer/src/global.d.ts**
```typescript
// Defined on the window
interface Window {
2022-01-21 17:20:36 +08:00
fs: typeof import('fs')
ipcRenderer: import('electron').IpcRenderer
2022-01-07 08:39:23 +08:00
}
```
2022-01-21 17:20:36 +08:00
* **src/renderer/main.ts**
2022-01-07 08:39:23 +08:00
```typescript
// Use Electron, NodeJs API in Renderer-process
2022-01-21 17:20:36 +08:00
console.log('fs', window.fs)
console.log('ipcRenderer', window.ipcRenderer)
2022-01-07 08:39:23 +08:00
```
2022-01-21 17:22:02 +08:00
**如果你真的在这个模板中开启了 `nodeIntegration: true` `contextIsolation: false` 我不拦着
🚧 但是要提醒你做两件事儿**
1. `preload/index.ts` 中的 `exposeInMainWorld` 删掉,已经没有用了
```diff
- contextBridge.exposeInMainWorld('fs', fs)
- contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer)
```
2. `configs/vite-renderer.config` 中有个 `resolveElectron` **最好了解下**
👉 这里有个 `issues` [请教一下vite-renderer.config中的resolveElectron函数](https://github.com/caoxiemeihao/electron-vue-vite/issues/52)
2021-11-10 21:03:59 +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" />