当前位置:网站首页>Electron application development best practices
Electron application development best practices
2022-08-09 10:16:00 【InfoQ】
一、背景



- Consists of a main process and N A rendering process of
- Main process for leading role,To complete a variety of cross-platform and native interaction
- Rendering process can be more,使用 Web 技术开发,Through the browser kernel rendering the page
- The main process and the rendering process done through interprocess communication functions


二、应用技术选型
2.1 编程语言 Typescript
- 针对开发者
- Javascript 的超集 - Seamless support for all es2020+ 所有的特性,学习成本小
- 编译生成的 JavaScript Keep a good readability of code to
- Maintainability significantly enhance
- 完整的 OOP 的支持 - extends, interface, private, protect, public等
- 类型即文档
- 类型的约束,Less unit test coverage
- 更安全的代码
- 针对工具
- Better refactoring capability
- Static analysis automatically guide package
- 代码错误检查
- 代码跳转
- Code hinting is lacking
- 社区
2.2 构建工具 Electron-Forge

2.3 Web 方案 Vue3 + Vite
2.4 monorepo方案 pnpm + turbo

- 目前最好的包管理工具, pnpm 吸收了 npm 、 yarn 、 lerna 等主流工具的精华,并去其糟粕.
- 生态、社区活跃且强大
- 结合 workspace 可以完成 monorepo 最佳设计和实践
- 在管理多项目的包依赖、代码风格、代码质量、组件库复用等场景下,表现出色
- 在框架、库的开发、调试、维护方面,表现出色
- 它是一个高性能构建系统,拥有增量构建、云缓存、并行执行、运行时零开销、任务管道、精简子集等特性
- 具有非常优秀的任务编排能力,可以弥补 pnpm 在任务编排上的短板
2.5 数据库 lowdb
- 生态(使用者数量、维护频率、Version stability)
- 能力
- 性能
- 其他(And consumer technology compatibility)
- github 的 issues、commit、fork、star
- sourcegraph Keyword search results for
- npm 包下载量、版本发布
- Website and blog
- lowdb: 生态、能力、Do a good job of three performance, json In the form of storage structure, 支持 lodash 、 ramda 等 api 操作,To backup and call
- sqlite3: 生态、能力、Do a good job of three performance, Nodejs A relational database for the first option
- nedb: 能力、Do a good job of three performance,The disadvantage is that the basic do not maintain,但底子还在,Especially the operation is MongoDB 的子集,对于熟悉 MongoDB The user is the perfect choice for.
- electron-store: The ecological performance good,Lightweight persistence,简单易用
2.6 脚本工具 zx
- 自带 fetch 、 chalk 等常用库,使用方便快捷
- More child process convenient and quick、执行远端脚本、解析 md 、 xml 文件脚本、支持 ts ,功能丰富且强大
- 谷歌出品,大厂背景,生态非常活跃
三、构建
- Application icon to generate
- The binary file building
- 按需构建
- 性能优化
- 跨平台兼容
3.1 Application icon to generate
- 软件生成: icofx3
- 网页生成: https://tool.520101.com/diannao/ico/(opens new window)
- 软件生成: icofx3
- 网页生成: https://tool.520101.com/diannao/ico/(opens new window)
- 命令行生成: 使用 sips 和 iconutil 生成
3.2 The binary file building
const os = require('os')
const platform = os.platform()
const config = {
packagerConfig: {
// 可以将 ffmpeg 目录打包到 asar 目录外面
extraResource: [`./src/main/ffmpeg/`]
}
}import { app } from 'electron'
import os from 'os'
import path from 'path'
const platform = os.platform()
const dir = app.getAppPath()
let basePath = ''
if(app.isPackaged) basePath = path.join(process.resourcesPath)
else basePath = path.join(dir, 'ffmpeg')
const isWin = platform === 'win32'
// ffmpeg 二进制程序路径
const ffmpegPath = path.join(basePath, `${platform}`, `ffmpeg${isWin ? '.exe' : ''}`)3.3 按需构建
const platform = os.platform()
const config = {
packagerConfig: {
extraResource: [`./src/main/ffmpeg/${platform}`]
},
}3.4 性能优化


- 将 web The building needed to rely on all in devDependencies 中,只将在 electron The need to rely on in dependencies
- Has nothing to do with the production code and file from the build of
- The cross-platform use binary file,如 ffmpeg For on-demand build(On-demand construction has been introduced above)
- 对 node_modules To clean streamline
四、更新
- 全量更新
- 增量更新
4.1 全量更新

- 开发服务端接口,To return to use the latest version information
- 渲染进程使用 axios 等工具请求接口,获取最新版本信息
- Encapsulation update logic,To the interface returns the version information comprehensive comparison,判断是否更新
- 通过 ipc Communication will be updated information to the master
- 主进程通过 electron-updater 进行全量更新
- Will update the information by ipc Push to the rendering process
- Rendering process to show the user update information,若更新成功,The pop-up Windows tell users to restart the application,完成软件更新
4.2 增量更新

- Rendering process regularly inform main process updates
- Main process testing updates
- 需要更新,The pull line latest package
- Delete the old version package,Copying online latest package,Complete the incremental updating
- 通知渲染进程,Prompting the user to restart the application to complete the update
五、性能优化
- 构建优化
- Startup optimization
- 运行时优化
5.1 Startup optimization
- 使用 v8-compile-cache 缓存编译代码
- Priority loading core function,Non-core functions of dynamic loading
- 使用多进程,多线程技术
- 采用 asar 打包:Will speed up the start
- Increase visual transition:loading + 骨架屏
5.1.1 使用 v8-compile-cache 缓存编译代码
require('v8-compile-cache')5.1.2 Priority loading core function,Non-core functions of dynamic loading
export function share() {
const kun = require('kun')
kun()
}5.2 运行时优化
- The rendering process 进行 Web 性能优化
- The main process of lightweight thin body
5.2.1 The rendering process 进行 Web 性能优化

5.2.2 The main process of lightweight thin body
const { fork } = require('child_process')
let { app } = require('electron')
function createProcess(socketName) {
process = fork(`xxxx/server.js`, [
'--subprocess',
app.getVersion(),
socketName
])
}
const initApp = async () => {
// 其他初始化代码...
let socket = await findSocket()
createProcess(socket)
}
app.on('ready', initApp)六、质量保障

- 自动化测试
- 崩溃监控
- 崩溃治理
6.1 自动化测试


6.2 崩溃监控

- Rendering process after the collapse,Prompt the user to reload
- 通过 preload Unified initialization collapse monitoring
- 主进程、渲染进程通过 process.crash() Simulated collapse
- To collect and analyze crash log
6.3 崩溃治理
- The positioning error stack difficult:Native 错误栈,Without the operating context
- Debug the high threshold:C++ 、 IIdb/GDB
- 运行环境复杂:机器型号、系统、其他软件
- 及时升级 electron
- User operation logs and system information
- Repetition and positioning problem is more important than governance
- Put the question to the community to solve,Community fast response
- 善于用 devtool Analysis and governance memory problems
七、安全
- 源码泄漏
- asar
- 源码保护
- 应用安全
- 编码安全
7.1 源码泄漏

7.2 asar

7.3 源码保护
- asar
- 代码混淆
- WebAssembly
- Language bindings

fn sum(a: f64, b: f64) -> f64 {
a + b
}use napi_derive::napi;
#[napi]
fn sum(a: f64, b: f64) -> f64 {
a + b
}import { sum as rsSum } from '@rebebuca/native'
// 输出 7
console.log(rsSum(2, 5))7.4 应用安全

- Application equipment fingerprint generation:Above can be used in this paper, the napi-rs Scheme to realize
- User authentication information and binding equipment fingerprint:Use the server to implement
7.5 编码安全
- 常用的 web 安全,比如防 xss 、 csrf
- 设置 node 可执行环境
- The form open security options
- Limit links jump
八、总结
边栏推荐
猜你喜欢

Attentional Feature Fusion
今天做了手机播放器的均衡器

快速解决MySQL插入中文数据时报错或乱码问题

Demand side power load forecasting (Matlab code implementation)

ArrayList和LinkedList

【MySQL】mysql因为字符集导致left join出现Using join buffer (Block Nested Loop)
![[贴装专题] 视觉贴装平台与贴装流程介绍](/img/ec/870af3b56a487a5ca3a32a611234ff.png)
[贴装专题] 视觉贴装平台与贴装流程介绍

Nodejs服务端
![[Machine Learning] Detailed explanation of web crawler combat](/img/ac/f00f0c81e66ba526ac39ee60fad72b.png)
[Machine Learning] Detailed explanation of web crawler combat

第二周作业
随机推荐
学习NET-SNMP之一 ---------编译NET-SNMP程序。
【八大排序①】插入排序(直接插入排序、希尔排序)
Tom Morgan | Twenty-One Rules of Life
KMP& sunday
2021-01-11-雪碧图做表情管理器
小程序员的发展计划
函数组件和类组件和dva视图更新问题
Attentional Feature Fusion
开源SPL,WebService/Restful广泛应用于程序间通讯,如微服务、数据交换、公共或私有的数据服务等。
LeetCode179:最大数(C语言)代码简洁!
浏览器的报错分类
How to quickly get through the mirror release process?
1004 成绩排名 (20 分)
tuple dictionary collection
主从postition变化无法锁定_Slave_IO_Running显示No_Slave_Sql_Running显示No---Mysql主从复制同步002
基于信号量与环形队列实现读写异步缓存队列
basic operator
技术分享 | 使用 cURL 发送请求
EndNoteX9 OR X 20 Guide
自定义类型:结构体,枚举,联合