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

133 lines
5.6 KiB
Markdown
Raw Normal View History

2021-11-09 19:38:57 +08:00
# electron-vue-vite
2021-11-12 08:35:08 +08:00
![awesome-vite](https://camo.githubusercontent.com/abb97269de2982c379cbc128bba93ba724d8822bfbe082737772bd4feb59cb54/68747470733a2f2f63646e2e7261776769742e636f6d2f73696e647265736f726875732f617765736f6d652f643733303566333864323966656437386661383536353265336136336531353464643865383832392f6d656469612f62616467652e737667)
![GitHub license](https://img.shields.io/github/license/caoxiemeihao/electron-vue-vite?style=flat)
2021-11-09 19:38:57 +08:00
![GitHub stars](https://img.shields.io/github/stars/caoxiemeihao/electron-vue-vite?color=fa6470&style=flat)
![GitHub forks](https://img.shields.io/github/forks/caoxiemeihao/electron-vue-vite?style=flat)
2021-11-12 08:35:08 +08:00
2021-11-09 19:38:57 +08:00
2021-11-10 20:21:51 +08:00
**[English](README.md) | 简体中文**
2021-11-09 19:38:57 +08:00
2021-11-12 08:35:08 +08:00
🥳 `Electron` + `Vue3` + `Vite2` 整合模板 -- **结构简单,容易上手!**
2021-11-09 19:38:57 +08:00
2022-01-06 09:58:41 +08:00
## 概述
2022-01-07 17:20:40 +08:00
  这是一个追求精简的`Electron`类整合模板,只保持最基本的文件、最基本的依赖、最基本的功能;而不是大而全的、臃肿的设计。这样做的目的是能确保模板足够灵活。
2022-01-06 10:21:21 +08:00
2022-01-06 09:58:41 +08:00
所以说如果你是对 -- 工程模板追求精简的 Coder或者刚入世的小白想弄明白`Electron`整合类模板最基础的工作原理,亦或者你是大神只是想偷懒少干点活;那么这个模板最合适你不过了。
2022-01-06 10:21:21 +08:00
2022-01-06 09:58:41 +08:00
尽管如此,我还是希望你对`Electron` `Vite`有一定的基础;因为除了项目结构简单外,这份`README`也显得 “精简” 。
2022-01-06 10:21:21 +08:00
2022-01-06 09:58:41 +08:00
模板的具体实现细节我相信你看两遍源码就能把它吃透了 😋
2021-11-12 08:35:08 +08:00
## 运行项目
2021-11-09 19:56:52 +08:00
```bash
# clone the project
2022-02-03 08:15:25 +08:00
git clone https://github.com/caoxiemeihao/electron-vue-vite.git
2021-11-09 19:56:52 +08:00
# enter the project directory
cd electron-vue-vite
2021-12-11 10:43:21 +08:00
# install dependency
npm install
2021-11-09 19:56:52 +08:00
# develop
2021-12-11 10:43:21 +08:00
npm run dev
2021-11-09 19:56:52 +08:00
```
2021-11-11 18:29:18 +08:00
## 目录结构
2022-01-07 17:21:46 +08:00
  一旦启动或打包脚本执行过,会在根目录产生 **`dist` 文件夹,里面的文件夹同 `src` 一模一样**;在使用一些路径计算时,尤其是相对路径计算;`dist` 与 `src` 里面保持相同的目录结构能避开好多问题
2021-12-29 09:27:51 +08:00
2021-11-11 18:29:18 +08:00
```tree
2022-01-27 11:27:25 +08:00
├── dist 构建后,根据 src 目录生成
2021-12-29 09:27:51 +08:00
├ ├── main
├ ├── preload
├ ├── renderer
2021-11-11 18:29:18 +08:00
├── scripts
2022-01-27 11:27:25 +08:00
├ ├── build.mjs 项目构建脚本,对应 npm run build
├ ├── vite.config.mjs 主进程, 预加载脚本源码 vite 配置
├ ├── watch.mjs 项目开发脚本,对应 npm run dev
2021-11-11 18:29:18 +08:00
├── src
2022-01-27 11:27:25 +08:00
├ ├── main 主进程源码
├ ├── preload 预加载脚本源码
├ ├── renderer 渲染进程源码
├ ├── vite.config.ts 渲染进程 vite 配置
2021-11-11 18:29:18 +08:00
```
2021-11-09 19:38:57 +08:00
2022-01-21 17:00:42 +08:00
## 依赖放到 dependencies 还是 devDependencies
2021-11-09 19:38:57 +08:00
2022-01-21 17:00:42 +08:00
  对待 **Electron-Main、Preload-Script** 时 vite 会以 lib 形式打包 commonjs 格式代码;
如果碰 node 环境的包可以直接放到 dependencies 中vite 会解析为 require('xxxx')
electron-builder 打包时候会将 dependencies 中的包打包到 app.asar 里面
2021-11-09 19:56:52 +08:00
2022-01-21 17:00:42 +08:00
  对待 **Electron-Renderer** 时 vite 会以 ESM 格式解析代码;
像 vue、react 这种前端用的包可以直接被 vite 构建,所以不需要 vue、react 源码;
现实情况 vue、react 放到 dependencies 或 devDependencies 中都可以被正确构建;
但是放到 dependencies 会被 electron-builder 打包到 app.asar 里面导致包体变大;
所以放到 devDependencies 既能被正确构建还可以减小 app.asar 体积,一举两得
## 渲染进程使用 NodeJs API
2021-11-09 19:38:57 +08:00
2022-01-21 17:00:42 +08:00
> 🚧 因为安全的原因 Electron 默认不支持在 渲染进程 中使用 NodeJs API但是有些小沙雕就是想这么干拦都拦不住实在想那么干的话用另一个模板更方便 👉 **[electron-vite-boilerplate](https://github.com/caoxiemeihao/electron-vite-boilerplate)**
**推荐所有的 NodeJs、Electron API 通过 `Preload-script` 注入到 渲染进程中,例如:**
2021-11-09 19:38:57 +08:00
2021-11-11 18:29:18 +08:00
* **src/preload/index.ts**
2021-11-09 19:38:57 +08:00
2021-11-11 18:29:18 +08:00
```typescript
2021-12-20 09:48:32 +08:00
import fs from 'fs'
import { contextBridge, ipcRenderer } from 'electron'
// --------- Expose some API to Renderer-process. ---------
2021-11-11 18:29:18 +08:00
contextBridge.exposeInMainWorld('fs', fs)
contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer)
```
2021-11-09 19:38:57 +08:00
2022-01-06 09:21:29 +08:00
* **src/renderer/src/global.d.ts**
```typescript
// Defined on the window
interface Window {
fs: typeof import('fs')
ipcRenderer: import('electron').IpcRenderer
}
```
2021-11-11 18:29:18 +08:00
* **src/renderer/main.ts**
2021-11-09 19:38:57 +08:00
2021-11-11 18:29:18 +08:00
```typescript
2022-01-06 09:21:29 +08:00
// Use Electron, NodeJs API in Renderer-process
2021-11-11 18:29:18 +08:00
console.log('fs', window.fs)
console.log('ipcRenderer', window.ipcRenderer)
```
2021-11-09 19:38:57 +08:00
2022-01-21 17:00:42 +08:00
**如果你真的在这个模板中开启了 `nodeIntegration: true` `contextIsolation: false` 我不拦着
🚧 但是要提醒你做两件事儿**
2022-01-17 11:01:47 +08:00
2022-01-21 17:00:42 +08:00
1. `preload/index.ts` 中的 `exposeInMainWorld` 删掉,已经没有用了
2022-01-17 11:01:47 +08:00
2022-01-21 17:00:42 +08:00
```diff
- contextBridge.exposeInMainWorld('fs', fs)
- contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer)
```
2022-01-17 11:01:47 +08:00
2022-01-21 17:00:42 +08:00
2. `configs/vite-renderer.config` 中有个 `resolveElectron` **最好了解下**
👉 这里有个 `issues` [请教一下vite-renderer.config中的resolveElectron函数](https://github.com/caoxiemeihao/electron-vue-vite/issues/52)
2022-01-17 11:01:47 +08:00
2021-11-09 19:38:57 +08:00
## 运行效果
2021-11-12 08:35:08 +08:00
<img width="400px" src="https://raw.githubusercontent.com/caoxiemeihao/blog/main/electron-vue-vite/screenshot/electron-15.png" />
2021-11-09 19:38:57 +08:00
2021-11-13 09:02:37 +08:00
## 微信 | | 请我喝杯下午茶 🥳
2021-11-09 19:38:57 +08:00
2021-11-12 08:35:08 +08:00
<div style="display:flex;">
<img width="244px" src="https://raw.githubusercontent.com/caoxiemeihao/blog/main/assets/wechat/group/qrcode.jpg" />
&nbsp;&nbsp;&nbsp;&nbsp;
2021-11-13 08:47:12 +08:00
<img width="244px" src="https://raw.githubusercontent.com/caoxiemeihao/blog/main/assets/wechat/%24qrcode/%24.png" />
2021-11-12 08:35:08 +08:00
</div>