2021-11-11 08:17:54 +08:00
|
|
|
|
# vite-react-electron
|
2021-11-10 21:03:59 +08:00
|
|
|
|
|
2022-06-10 09:16:43 +08:00
|
|
|
|
[](https://github.com/vitejs/awesome-vite)
|
2021-11-11 08:17:54 +08:00
|
|
|
|

|
|
|
|
|

|
|
|
|
|

|
2021-11-10 21:03:59 +08:00
|
|
|
|
[](https://nodejs.org/about/releases)
|
|
|
|
|
|
2022-06-10 09:16:43 +08:00
|
|
|
|
[English](README.md) | 简体中文
|
2021-11-10 21:03:59 +08:00
|
|
|
|
|
2021-12-18 10:23:56 +08:00
|
|
|
|
## 概述
|
|
|
|
|
|
2022-06-10 09:16:43 +08:00
|
|
|
|
📦 开箱即用
|
2022-06-13 09:54:41 +08:00
|
|
|
|
🎯 基于 [react-ts](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) 模板, 低侵入性
|
2022-06-10 09:16:43 +08:00
|
|
|
|
🌱 结构清晰,可塑性强
|
2022-06-13 09:54:41 +08:00
|
|
|
|
💪 支持在渲染进程中使用 Electron、Node.js API
|
|
|
|
|
🔩 支持 C/C++ 模块
|
2022-06-10 09:16:43 +08:00
|
|
|
|
🖥 很容易实现多窗口
|
2021-12-18 10:23:56 +08:00
|
|
|
|
|
2022-04-25 08:51:42 +08:00
|
|
|
|
## 快速开始
|
2021-11-10 21:03:59 +08:00
|
|
|
|
|
2022-03-13 08:45:27 +08:00
|
|
|
|
```sh
|
|
|
|
|
npm create electron-vite
|
|
|
|
|
```
|
2021-11-10 21:03:59 +08:00
|
|
|
|
|
2022-06-10 09:16:43 +08:00
|
|
|
|

|
2022-04-25 08:51:42 +08:00
|
|
|
|
|
|
|
|
|
## 调试
|
|
|
|
|
|
2022-06-10 09:16:43 +08:00
|
|
|
|

|
2021-11-10 21:03:59 +08:00
|
|
|
|
|
|
|
|
|
## 目录
|
|
|
|
|
|
2022-06-10 09:16:43 +08:00
|
|
|
|
*🚨 默认情况下, `electron` 文件夹下的文件将会被构建到 `dist/electron`*
|
2021-12-29 09:33:21 +08:00
|
|
|
|
|
2021-11-10 21:03:59 +08:00
|
|
|
|
```tree
|
2022-06-10 09:16:43 +08:00
|
|
|
|
├── electron Electron 源码文件夹
|
2022-06-28 10:43:17 +08:00
|
|
|
|
│ ├── main Main-process 源码
|
|
|
|
|
│ ├── preload Preload-script 源码
|
|
|
|
|
│ └── resources 应用打包的资源文件夹
|
|
|
|
|
│ ├── icon.icns 应用图标(macOS)
|
|
|
|
|
│ ├── icon.ico 应用图标
|
|
|
|
|
│ ├── installerIcon.ico 安装图标
|
|
|
|
|
│ └── uninstallerIcon.ico 卸载图标
|
|
|
|
|
│
|
2022-06-10 09:16:43 +08:00
|
|
|
|
├── release 构建后生成程序目录
|
2022-06-28 10:43:17 +08:00
|
|
|
|
│ └──{version}
|
|
|
|
|
│ ├── {os}-unpacked 未打包的程序(绿色运行版)
|
|
|
|
|
│ └── Setup.{ext} 应用安装文件
|
|
|
|
|
│
|
2022-06-10 09:16:43 +08:00
|
|
|
|
├── public 同 Vite 模板的 public
|
|
|
|
|
└── src 渲染进程源码、React代码
|
2021-11-12 09:01:59 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-06-28 10:41:23 +08:00
|
|
|
|
## 🚨 依赖放到 dependencies 还是 devDependencies
|
2022-01-21 17:22:02 +08:00
|
|
|
|
|
2022-06-28 10:41:23 +08:00
|
|
|
|
**Node.js 包放到 `dependencies` 中**
|
2022-06-27 10:17:20 +08:00
|
|
|
|
|
2022-07-04 09:17:05 +08:00
|
|
|
|
**e.g.** `electron-store` `sqlite3` `serilaport` `mongodb` ...others
|
2022-06-28 10:41:23 +08:00
|
|
|
|
|
|
|
|
|
**Web 包放到 in `devDependencies` 中**
|
|
|
|
|
|
2022-07-04 09:17:05 +08:00
|
|
|
|
**e.g.** `react` `react-dom` `react-router` `mobx` `zustand` `antd` `axios` ...others
|
2022-06-28 10:41:23 +08:00
|
|
|
|
|
2022-07-04 09:17:05 +08:00
|
|
|
|
看看这 👉 [dependencies vs devDependencies](https://github.com/electron-vite/vite-plugin-electron-renderer#dependencies-vs-devdependencies)
|
2022-06-28 10:41:23 +08:00
|
|
|
|
|
2022-07-04 09:17:05 +08:00
|
|
|
|
## 🚨 ESM packages
|
2022-06-28 10:41:23 +08:00
|
|
|
|
|
2022-07-04 09:17:05 +08:00
|
|
|
|
**e.g.** `node-fetch` `execa` `got` ...others
|
2022-06-27 10:17:20 +08:00
|
|
|
|
|
2022-07-04 09:17:05 +08:00
|
|
|
|
1. `npm i vite-plugin-esmodule -D`
|
|
|
|
|
2. Configure in vite.config.ts
|
2022-06-27 10:17:20 +08:00
|
|
|
|
|
2022-07-04 09:17:05 +08:00
|
|
|
|
```ts
|
|
|
|
|
import esmodule from 'vite-plugin-esmodule'
|
|
|
|
|
export default {
|
|
|
|
|
plugins: [
|
|
|
|
|
esmodule(['got', 'execa', 'node-fetch']),
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
```
|
2022-06-28 10:41:23 +08:00
|
|
|
|
|
2022-06-27 10:17:20 +08:00
|
|
|
|
<!--
|
2022-01-21 17:22:02 +08:00
|
|
|
|
  对待 **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 体积,一举两得
|
2022-06-27 10:17:20 +08:00
|
|
|
|
-->
|