refactor: upgrade deps, setup tailwind4 and correct ci (#258)

* refresh

* e2e test

* Change debugger type from 'pwa-chrome' to 'chrome'

* ci: align workflows with electron-vite-vue#540

* chore: update test, tsconfig and deps

* chore: update deps and debug config

* chore: refresh style

* docs: update readme

* docs: make linter happy

* ci: bump versions

* chore: refactor update handling and improve UI elements

* 1.0.0
This commit is contained in:
Song authored and GitHub committed 2026-05-31 22:42:38 +08:00
1 parent 86250a53d6
commit e803167b31
39 files changed
+854 -915

No files matched your search

+19 -7
View File
@@ -1,6 +1,7 @@
name: Build
on:
workflow_dispatch:
push:
branches: [main]
paths-ignore:
@@ -14,6 +15,9 @@ on:
- ".github/**"
- "!.github/workflows/build.yml"
permissions:
contents: read
jobs:
build:
runs-on: ${{ matrix.os }}
@@ -24,24 +28,32 @@ jobs:
steps:
- name: Checkout Code
uses: actions/checkout@v3
uses: actions/checkout@v6
with:
persist-credentials: false
- name: Setup pnpm
uses: pnpm/action-setup@v6
with:
version: 10
- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v6
with:
node-version: 18
node-version: 22
cache: pnpm
- name: Install Dependencies
run: npm install
run: pnpm install --frozen-lockfile
- name: Build Release Files
run: npm run build
run: pnpm build
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v7
with:
name: release_on_${{ matrix. os }}
name: release_on_${{ matrix.os }}
path: release/
retention-days: 5
+13 -65
View File
@@ -1,81 +1,29 @@
name: CI
on:
pull_request_target:
pull_request:
branches:
- main
paths:
- '*.md'
- '**/*.md'
permissions:
pull-requests: write
contents: read
concurrency:
group: ci-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
job1:
name: Check Not Allowed File Changes
runs-on: ubuntu-latest
outputs:
markdown_change: ${{ steps.filter_markdown.outputs.change }}
markdown_files: ${{ steps.filter_markdown.outputs.change_files }}
steps:
- name: Check Not Allowed File Changes
uses: dorny/paths-filter@v2
id: filter_not_allowed
with:
list-files: json
filters: |
change:
- 'package-lock.json'
- 'yarn.lock'
- 'pnpm-lock.yaml'
# ref: https://github.com/github/docs/blob/main/.github/workflows/triage-unallowed-contributions.yml
- name: Comment About Changes We Can't Accept
if: ${{ steps.filter_not_allowed.outputs.change == 'true' }}
uses: actions/github-script@v6
with:
script: |
let workflowFailMessage = "It looks like you've modified some files that we can't accept as contributions."
try {
const badFilesArr = [
'package-lock.json',
'yarn.lock',
'pnpm-lock.yaml',
]
const badFiles = badFilesArr.join('\n- ')
const reviewMessage = `👋 Hey there spelunker. It looks like you've modified some files that we can't accept as contributions. The complete list of files we can't accept are:\n- ${badFiles}\n\nYou'll need to revert all of the files you changed in that list using [GitHub Desktop](https://docs.github.com/en/free-pro-team@latest/desktop/contributing-and-collaborating-using-github-desktop/managing-commits/reverting-a-commit) or \`git checkout origin/main <file name>\`. Once you get those files reverted, we can continue with the review process. :octocat:\n\nMore discussion:\n- https://github.com/electron-vite/electron-vite-vue/issues/192`
createdComment = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.number,
body: reviewMessage,
})
workflowFailMessage = `${workflowFailMessage} Please see ${createdComment.data.html_url} for details.`
} catch(err) {
console.log("Error creating comment.", err)
}
core.setFailed(workflowFailMessage)
- name: Check Not Linted Markdown
if: ${{ always() }}
uses: dorny/paths-filter@v2
id: filter_markdown
with:
list-files: shell
filters: |
change:
- added|modified: '*.md'
job2:
lint-markdown:
name: Lint Markdown
runs-on: ubuntu-latest
needs: job1
if: ${{ always() && needs.job1.outputs.markdown_change == 'true' }}
steps:
- name: Checkout Code
uses: actions/checkout@v3
uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
- name: Lint markdown
run: npx markdownlint-cli ${{ needs.job1.outputs.markdown_files }} --ignore node_modules
run: npx --yes markdownlint-cli@0.48.0 '**/*.md' --ignore node_modules --disable MD013
+65
View File
@@ -0,0 +1,65 @@
name: PR Guard
on:
pull_request_target:
branches:
- main
permissions:
contents: read
pull-requests: write
concurrency:
group: pr-guard-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
validate:
name: Validate Pull Request
runs-on: ubuntu-latest
steps:
- name: Detect Changed Files
uses: dorny/paths-filter@v3
id: changes
with:
list-files: shell
filters: |
lockfiles:
- 'package-lock.json'
- 'yarn.lock'
- 'pnpm-lock.yaml'
- 'bun.lock'
# ref: https://github.com/github/docs/blob/main/.github/workflows/triage-unallowed-contributions.yml
- name: Comment About Changes We Can't Accept
if: ${{ steps.changes.outputs.lockfiles == 'true' }}
uses: actions/github-script@v7
with:
script: |
try {
const badFilesArr = [
'package-lock.json',
'yarn.lock',
'pnpm-lock.yaml',
'bun.lock',
]
const badFiles = badFilesArr.join('\n- ')
const reviewMessage = `👋 Hey there spelunker. It looks like you've modified some files that we can't accept as contributions. The complete list of files we can't accept are:\n- ${badFiles}\n\nYou'll need to revert all of the files you changed in that list using [GitHub Desktop](https://docs.github.com/en/free-pro-team@latest/desktop/contributing-and-collaborating-using-github-desktop/managing-commits/reverting-a-commit) or \`git checkout origin/main <file name>\`. Once you get those files reverted, we can continue with the review process. :octocat:\n\nMore discussion:\n- https://github.com/electron-vite/electron-vite-vue/issues/192`
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: reviewMessage,
})
} catch(err) {
core.warning(`Failed to create PR comment: ${err.message}`)
}
- name: Fail on Restricted Lockfile Changes
if: ${{ steps.changes.outputs.lockfiles == 'true' }}
run: |
echo "Lockfiles are managed by maintainers and should not be changed in PRs."
echo "Changed lockfiles:"
echo "${{ steps.changes.outputs.lockfiles_files }}"
exit 1
+2
View File
@@ -31,3 +31,5 @@ yarn.lock
/test-results/
/playwright-report/
/playwright/.cache/
test/screenshots
-24
View File
@@ -1,24 +0,0 @@
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { createRequire } from 'node:module'
import { spawn } from 'node:child_process'
const pkg = createRequire(import.meta.url)('../package.json')
const __dirname = path.dirname(fileURLToPath(import.meta.url))
// write .debug.env
const envContent = Object.entries(pkg.debug.env).map(([key, val]) => `${key}=${val}`)
fs.writeFileSync(path.join(__dirname, '.debug.env'), envContent.join('\n'))
// bootstrap
spawn(
// TODO: terminate `npm run dev` when Debug exits.
process.platform === 'win32' ? 'npm.cmd' : 'npm',
['run', 'dev'],
{
shell: true,
stdio: 'inherit',
env: Object.assign(process.env, { VSCODE_DEBUG: 'true' }),
},
)
-7
View File
@@ -1,7 +0,0 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"mrmlnc.vscode-json5"
]
}
+22 -37
View File
@@ -1,54 +1,39 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"compounds": [
{
"name": "Debug App",
"preLaunchTask": "Before Debug",
"configurations": [
"Debug Main Process",
"Debug Renderer Process"
],
"presentation": {
"hidden": false,
"group": "",
"order": 1
},
"stopAll": true
}
],
"configurations": [
{
"name": "Debug Main Process",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/vite",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/vite.cmd"
},
"runtimeArgs": [
"--no-sandbox",
"--remote-debugging-port=9229",
"."
],
"envFile": "${workspaceFolder}/.vscode/.debug.env",
"console": "integratedTerminal"
"runtimeArgs": ["dev"],
"env": {
"REMOTE_DEBUGGING_PORT": "9222"
}
},
{
"name": "Debug Renderer Process",
"port": 9229,
"port": 9222,
"request": "attach",
"type": "chrome",
"webRoot": "${workspaceFolder}/src",
"timeout": 60000,
"skipFiles": [
"<node_internals>/**",
"${workspaceRoot}/node_modules/**",
"${workspaceRoot}/dist-electron/**",
// Skip files in host(VITE_DEV_SERVER_URL)
"http://127.0.0.1:7777/**"
]
},
"presentation": {
"hidden": true
}
}
],
"compounds": [
{
"name": "Debug All",
"configurations": ["Debug Main Process", "Debug Renderer Process"],
"presentation": {
"order": 1
}
}
]
}
-3
View File
@@ -1,10 +1,7 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.tsc.autoDetect": "off",
"json.schemas": [
{
"fileMatch": [
"/*electron-builder.json5",
"/*electron-builder.json"
],
"url": "https://json.schemastore.org/electron-builder"
+14 -7
View File
@@ -6,19 +6,26 @@
{
"label": "Before Debug",
"type": "shell",
"command": "node .vscode/.debug.script.mjs",
"command": "node --enable-source-maps .vscode/.debug.script.mjs",
"isBackground": true,
"options": {
"cwd": "${workspaceFolder}"
},
"presentation": {
"reveal": "always",
"panel": "shared",
"clear": false
},
"problemMatcher": {
"owner": "typescript",
"fileLocation": "relative",
"pattern": {
// TODO: correct "regexp"
"regexp": "^([a-zA-Z]\\:\/?([\\w\\-]\/?)+\\.\\w+):(\\d+):(\\d+): (ERROR|WARNING)\\: (.*)$",
"regexp": "^(.*):(\\d+):(\\d+): (ERROR|WARNING): (.*)$",
"file": 1,
"line": 3,
"column": 4,
"code": 5,
"message": 6
"line": 2,
"column": 3,
"code": 4,
"message": 5
},
"background": {
"activeOnStart": true,
+46 -58
View File
@@ -1,24 +1,23 @@
# electron-vite-react
[![awesome-vite](https://awesome.re/mentioned-badge.svg)](https://github.com/vitejs/awesome-vite)
![GitHub stars](https://img.shields.io/github/stars/caoxiemeihao/vite-react-electron?color=fa6470)
![GitHub issues](https://img.shields.io/github/issues/caoxiemeihao/vite-react-electron?color=d8b22d)
![GitHub license](https://img.shields.io/github/license/caoxiemeihao/vite-react-electron)
[![Required Node.JS >= 14.18.0 || >=16.0.0](https://img.shields.io/static/v1?label=node&message=14.18.0%20||%20%3E=16.0.0&logo=node.js&color=3f893e)](https://nodejs.org/about/releases)
![GitHub stars](https://img.shields.io/github/stars/electron-vite/electron-vite-react?color=fa6470)
![GitHub issues](https://img.shields.io/github/issues/electron-vite/electron-vite-react?color=d8b22d)
![GitHub license](https://img.shields.io/github/license/electron-vite/electron-vite-react)
[![Required Node.js >= 20.19.0 || >= 22.12.0](https://img.shields.io/static/v1?label=node&message=%3E=20.19.0%20||%20%3E=22.12.0&logo=node.js&color=3f893e)](https://nodejs.org/about/releases)
English | [简体中文](README.zh-CN.md)
## 👀 Overview
## Overview
📦 Ready out of the box
🎯 Based on the official [template-react-ts](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts), project structure will be familiar to you
🌱 Easily extendable and customizable
💪 Supports Node.js API in the renderer process
🔩 Supports C/C++ native addons
🐞 Debugger configuration included
🖥 Easy to implement multiple windows
- Ready out of the box.
- Based on the official [template-react-ts](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts).
- Supports Electron and Node.js APIs in the renderer process.
- Supports C/C++ native addons.
- Includes debugger configuration.
- Easy to extend to multiple windows.
## 🛫 Quick Setup
## Quick Start
```sh
# clone the project
@@ -27,65 +26,54 @@ git clone https://github.com/electron-vite/electron-vite-react.git
# enter the project directory
cd electron-vite-react
# install dependency
npm install
# install dependencies
pnpm install
# develop
npm run dev
# start development
pnpm dev
```
## 🐞 Debug
## Available Scripts
![electron-vite-react-debug.gif](/electron-vite-react-debug.gif)
- `pnpm dev`: start the Vite dev server.
- `pnpm build`: build the renderer and package the app with electron-builder.
- `pnpm preview`: preview the production web build locally.
- `pnpm test`: run Vitest unit tests.
- `pnpm test:e2e`: build the test mode bundle and run Playwright tests.
- `pnpm typecheck`: run the TypeScript type checker.
## 📂 Directory structure
Familiar React application structure, just with `electron` folder on the top :wink:
*Files in this folder will be separated from your React application and built into `dist-electron`*
## Project Structure
```tree
├── electron Electron-related code
│ ├── main Main-process source code
│ └── preload Preload-scripts source code
│
├── release Generated after production build, contains executables
│ └── {version}
│ ├── {os}-{os_arch} Contains unpacked application executable
│ └── {app_name}_{version}.{ext} Installer for the application
│
├── public Static assets
└── src Renderer source code, your React application
├── build/ Packaging assets
├── dist-electron/ Compiled Electron output
├── electron/ Main-process and preload source
│ ├── main/
│ └── preload/
├── public/ Static assets
├── src/ Renderer source code
│ ├── components/
│ │ └── update/
│ ├── demos/
│ └── type/
└── test/ Unit and end-to-end tests
└── e2e/
```
<!--
## 🚨 Be aware
Files under `electron/` are compiled into `dist-electron/`.
This template integrates Node.js API to the renderer process by default. If you want to follow **Electron Security Concerns** you might want to disable this feature. You will have to expose needed API by yourself.
## Security Note
To get started, remove the option as shown below. This will [modify the Vite configuration and disable this feature](https://github.com/electron-vite/vite-plugin-electron-renderer#config-presets-opinionated).
The `renderer: {}` preset in `vite.config.ts` is only a Vite adapter that polyfills Electron, Node.js APIs and native modules for the renderer process. It is not the same as enabling Node integration. If you want direct Node.js access in the renderer, enable `nodeIntegration` in the `BrowserWindow` webPreferences in the main process and review the security impact carefully.
```diff
# vite.config.ts
## Features
export default {
plugins: [
...
- // Use Node.js API in the Renderer-process
- renderer({
- nodeIntegration: true,
- }),
...
],
}
```
-->
1. Electron auto update with docs in [src/components/update/README.md](src/components/update/README.md).
2. Vitest unit tests and Playwright end-to-end tests.
3. TailwindCSS v4.
## 🔧 Additional features
1. electron-updater 👉 [see docs](src/components/update/README.md)
1. playwright
## ❔ FAQ
## Resources
- Auto-update docs: [English](src/components/update/README.md) | [简体中文](src/components/update/README.zh-CN.md)
- [C/C++ addons, Node.js modules - Pre-Bundling](https://github.com/electron-vite/vite-plugin-electron-renderer#dependency-pre-bundling)
- [dependencies vs devDependencies](https://github.com/electron-vite/vite-plugin-electron-renderer#dependencies-vs-devdependencies)
+48 -60
View File
@@ -1,91 +1,79 @@
# vite-react-electron
# electron-vite-react
[![awesome-vite](https://awesome.re/mentioned-badge.svg)](https://github.com/vitejs/awesome-vite)
![GitHub stars](https://img.shields.io/github/stars/caoxiemeihao/vite-react-electron?color=fa6470)
![GitHub issues](https://img.shields.io/github/issues/caoxiemeihao/vite-react-electron?color=d8b22d)
![GitHub license](https://img.shields.io/github/license/caoxiemeihao/vite-react-electron)
[![Required Node.JS >= 14.18.0 || >=16.0.0](https://img.shields.io/static/v1?label=node&message=14.18.0%20||%20%3E=16.0.0&logo=node.js&color=3f893e)](https://nodejs.org/about/releases)
![GitHub stars](https://img.shields.io/github/stars/electron-vite/electron-vite-react?color=fa6470)
![GitHub issues](https://img.shields.io/github/issues/electron-vite/electron-vite-react?color=d8b22d)
![GitHub license](https://img.shields.io/github/license/electron-vite/electron-vite-react)
[![Required Node.js >= 20.19.0 || >= 22.12.0](https://img.shields.io/static/v1?label=node&message=%3E=20.19.0%20||%20%3E=22.12.0&logo=node.js&color=3f893e)](https://nodejs.org/about/releases)
[English](README.md) | 简体中文
## 概述
## 概览
📦 开箱即用
🎯 基于官方的 [template-react-ts](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts), 低侵入性
🌱 结构清晰,可塑性强
💪 支持在渲染进程中使用 Electron、Node.js API
🔩 支持 C/C++ 模块
🖥 很容易实现多窗口
- 开箱即用。
- 基于官方的 [template-react-ts](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts)。
- 支持在渲染进程中使用 Electron 和 Node.js API。
- 支持 C/C++ 原生模块。
- 包含调试配置。
- 易于扩展为多窗口应用。
## 快速开始
```sh
# clone the project
# 克隆项目
git clone https://github.com/electron-vite/electron-vite-react.git
# enter the project directory
# 进入项目目录
cd electron-vite-react
# install dependency
npm install
# 安装依赖
pnpm install
# develop
npm run dev
# 启动开发环境
pnpm dev
```
## 调试
## 可用脚本
![electron-vite-react-debug.gif](/electron-vite-react-debug.gif)
- `pnpm dev`:启动 Vite 开发服务器。
- `pnpm build`:构建渲染进程并使用 electron-builder 打包应用。
- `pnpm preview`:本地预览生产构建结果。
- `pnpm test`:运行 Vitest 单元测试。
- `pnpm test:e2e`:构建测试模式产物并运行 Playwright 测试。
- `pnpm typecheck`:运行 TypeScript 类型检查。
## 目录
*🚨 默认情况下, `electron` 文件夹下的文件将会被构建到 `dist-electron`*
## 项目结构
```tree
├── electron Electron 源码文件夹
│ ├── main Main-process 源码
│ └── preload Preload-scripts 源码
│
├── release 构建后生成程序目录
│ └── {version}
│ ├── {os}-{os_arch} 未打包的程序(绿色运行版)
│ └── {app_name}_{version}.{ext} 应用安装文件
│
├── public 同 Vite 模板的 public
└── src 渲染进程源码、React代码
├── build/ 打包资源
├── dist-electron/ 编译后的 Electron 输出
├── electron/ 主进程和 preload 源码
│ ├── main/
│ └── preload/
├── public/ 静态资源
├── src/ 渲染进程源码
│ ├── components/
│ │ └── update/
│ ├── demos/
│ └── type/
└── test/ 单元测试和端到端测试
└── e2e/
```
<!--
## 🚨 这需要留神
`electron/` 下的文件会被编译到 `dist-electron/`。
默认情况下,该模板在渲染进程中集成了 Node.js,如果你不需要它,你只需要删除下面的选项. [因为它会修改 Vite 默认的配置](https://github.com/electron-vite/vite-plugin-electron-renderer#config-presets-opinionated).
## 安全说明
```diff
# vite.config.ts
`vite.config.ts` 里的 `renderer: {}` 只是给 Vite 用的适配器,用来在渲染进程中修复 Electron、Node.js API 和原生模块的使用,它本身并不等同于开启 Node integration。若确实需要在渲染进程中直接使用 Node.js,请在主进程创建 `BrowserWindow` 时开启 `nodeIntegration`,并谨慎评估安全影响。
export default {
plugins: [
...
- // Use Node.js API in the Renderer-process
- renderer({
- nodeIntegration: true,
- }),
...
],
}
```
-->
## 功能
## 🔧 额外的功能
1. Electron 自动更新,文档见 [src/components/update/README.md](src/components/update/README.md)。
2. Vitest 单元测试和 Playwright 端到端测试。
3. TailwindCSS v4。
1. Electron 自动更新 👉 [阅读文档](src/components/update/README.zh-CN.md)
2. Playwright 测试
## ❔ FAQ
## 资源
- 自动更新文档:[English](src/components/update/README.md) | [简体中文](src/components/update/README.zh-CN.md)
- [C/C++ addons, Node.js modules - Pre-Bundling](https://github.com/electron-vite/vite-plugin-electron-renderer#dependency-pre-bundling)
- [dependencies vs devDependencies](https://github.com/electron-vite/vite-plugin-electron-renderer#dependencies-vs-devdependencies)
## 🍵 🍰 🍣 🍟
<img width="270" src="https://github.com/caoxiemeihao/blog/blob/main/assets/$qrcode/$.png?raw=true">
+4 -3
View File
@@ -34,8 +34,9 @@
"deleteAppDataOnUninstall": false
},
"publish": {
"provider": "generic",
"channel": "latest",
"url": "https://github.com/electron-vite/electron-vite-react/releases/download/v0.9.9/"
"provider": "github",
"owner": "electron-vite",
"repo": "electron-vite-react",
"releaseType": "release"
}
}
+33 -10
View File
@@ -1,14 +1,15 @@
import { app, ipcMain } from 'electron'
import { createRequire } from 'node:module'
import type {
ProgressInfo,
UpdateDownloadedEvent,
UpdateInfo,
CancellationToken
} from 'electron-updater'
// Pure cjs module does not support named exports, so we need to import the default export and access the autoUpdater property
import updater from 'electron-updater'
const { autoUpdater } = createRequire(import.meta.url)('electron-updater');
let cancellationToken = new CancellationToken()
const autoUpdater = updater.autoUpdater
let cancellationToken = new updater.CancellationToken()
let isDownloading = false
export function update(win: Electron.BrowserWindow) {
@@ -36,17 +37,22 @@ export function update(win: Electron.BrowserWindow) {
}
try {
return await autoUpdater.checkForUpdatesAndNotify()
return await autoUpdater.checkForUpdates()
} catch (error) {
return { message: 'Network error', error }
const resolvedError = error instanceof Error ? error : new Error('Network error')
return { message: resolvedError.message, error: resolvedError }
}
})
// Start downloading and feedback on progress
ipcMain.handle('start-download', (event: Electron.IpcMainInvokeEvent) => {
if (isDownloading) return
isDownloading = true
startDownload(
(error, progressInfo) => {
if (error) {
isDownloading = false
// feedback download error message
event.sender.send('update-error', { message: error.message, error })
} else {
@@ -55,6 +61,7 @@ export function update(win: Electron.BrowserWindow) {
}
},
() => {
isDownloading = false
// feedback update downloaded message
event.sender.send('update-downloaded')
}
@@ -64,7 +71,7 @@ export function update(win: Electron.BrowserWindow) {
// Cancel downloading
ipcMain.handle('cancel-download', () => {
cancellationToken.cancel()
cancellationToken = new CancellationToken();
cancellationToken = new updater.CancellationToken();
})
// Install now
@@ -77,8 +84,24 @@ function startDownload(
callback: (error: Error | null, info: ProgressInfo | null) => void,
complete: (event: UpdateDownloadedEvent) => void,
) {
autoUpdater.on('download-progress', (info: ProgressInfo) => callback(null, info))
autoUpdater.on('error', (error: Error) => callback(error, null))
autoUpdater.on('update-downloaded', complete)
const onDownloadProgress = (info: ProgressInfo) => callback(null, info)
const onError = (error: Error) => {
cleanup()
callback(error, null)
}
const onDownloaded = (event: UpdateDownloadedEvent) => {
cleanup()
complete(event)
}
const cleanup = () => {
autoUpdater.off('download-progress', onDownloadProgress)
autoUpdater.off('error', onError)
autoUpdater.off('update-downloaded', onDownloaded)
}
autoUpdater.on('download-progress', onDownloadProgress)
autoUpdater.on('error', onError)
autoUpdater.once('update-downloaded', onDownloaded)
autoUpdater.downloadUpdate(cancellationToken)
}
+27 -31
View File
@@ -1,44 +1,40 @@
{
"name": "electron-vite-react",
"version": "2.2.0",
"main": "dist-electron/main/index.js",
"description": "Electron Vite React boilerplate.",
"author": "草鞋没号 <308487730@qq.com>",
"license": "MIT",
"version": "2.3.0",
"private": true,
"debug": {
"env": {
"VITE_DEV_SERVER_URL": "http://127.0.0.1:7777/"
}
},
"description": "Electron Vite React boilerplate.",
"license": "MIT",
"author": "草鞋没号 <308487730@qq.com>",
"type": "module",
"main": "dist-electron/main/index.js",
"scripts": {
"dev": "vite",
"build": "tsc && vite build && electron-builder",
"build": "vite build && electron-builder",
"preview": "vite preview",
"pretest": "vite build --mode=test",
"test": "vitest run"
"test": "vitest run",
"test:e2e": "pnpm pretest && playwright test",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"electron-updater": "^6.3.9"
"electron-updater": "^6.8.3"
},
"devDependencies": {
"@playwright/test": "^1.48.2",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@vitejs/plugin-react": "^4.3.3",
"autoprefixer": "^10.4.20",
"electron": "^33.2.0",
"electron-builder": "^24.13.3",
"postcss": "^8.4.49",
"postcss-import": "^16.1.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"tailwindcss": "^3.4.15",
"typescript": "^5.4.2",
"vite": "^5.4.11",
"vite-plugin-electron": "^0.29.0",
"vite-plugin-electron-renderer": "^0.14.6",
"vitest": "^2.1.5"
}
"@playwright/test": "^1.60.0",
"@tailwindcss/vite": "^4.3.0",
"@types/react": "^19.2.15",
"@types/react-dom": "^19.2.3",
"@vitejs/plugin-react": "^6.0.2",
"electron": "^42.1.0",
"electron-builder": "^26.8.1",
"react": "^19.2.6",
"react-dom": "^19.2.6",
"tailwindcss": "^4.3.0",
"typescript": "^6.0.3",
"vite": "^8.0.14",
"vite-plugin-electron": "^1.0.0",
"vite-plugin-electron-renderer": "^1.0.0",
"vitest": "^4.1.7"
},
"packageManager": "pnpm@10.32.1"
}
+11
View File
@@ -0,0 +1,11 @@
import { defineConfig } from '@playwright/test'
export default defineConfig({
testDir: './test/e2e',
fullyParallel: false,
workers: 1,
reporter: 'list',
use: {
trace: 'on-first-retry',
},
})
-8
View File
@@ -1,8 +0,0 @@
module.exports = {
plugins: {
'postcss-import': {},
'tailwindcss/nesting': {},
tailwindcss: {},
autoprefixer: {},
},
}
-52
View File
@@ -1,52 +0,0 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
.logo-box {
position: relative;
height: 9em;
}
.logo {
position: absolute;
left: calc(50% - 4.5em);
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: no-preference) {
.logo.electron {
animation: logo-spin infinite 20s linear;
}
}
.card {
padding: 2em;
}
.read-the-docs {
color: #888;
}
.flex-center {
display: flex;
align-items: center;
justify-content: center;
}
+94 -23
View File
@@ -2,35 +2,106 @@ import { useState } from 'react'
import UpdateElectron from '@/components/update'
import logoVite from './assets/logo-vite.svg'
import logoElectron from './assets/logo-electron.svg'
import './App.css'
import logoTailwind from './assets/logo-tailwindcss.svg'
function App() {
const [count, setCount] = useState(0)
return (
<div className='App'>
<div className='logo-box'>
<a href='https://github.com/electron-vite/electron-vite-react' target='_blank'>
<img src={logoVite} className='logo vite' alt='Electron + Vite logo' />
<img src={logoElectron} className='logo electron' alt='Electron + Vite logo' />
</a>
</div>
<h1>Electron + Vite + React</h1>
<div className='card'>
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className='read-the-docs'>
Click on the Electron + Vite logo to learn more
</p>
<div className='flex-center'>
Place static files into the<code>/public</code> folder <img style={{ width: '5em' }} src='./node.svg' alt='Node logo' />
<div className='relative min-h-screen overflow-hidden bg-slate-50 px-4 py-8 text-slate-900 sm:px-6 lg:px-8'>
<div className='pointer-events-none absolute inset-0'>
<div className='absolute -left-28 top-10 h-96 w-96 rounded-full bg-cyan-200/50 blur-3xl' />
<div className='absolute -right-24 top-28 h-96 w-96 rounded-full bg-sky-200/40 blur-3xl' />
<div className='absolute bottom-0 left-1/2 h-72 w-[46rem] -translate-x-1/2 bg-gradient-to-r from-cyan-200/0 via-cyan-300/45 to-cyan-200/0 blur-3xl' />
</div>
<UpdateElectron />
<div className='relative mx-auto flex w-full max-w-6xl flex-col gap-8'>
<section className='overflow-hidden rounded-[2rem] border border-slate-200 bg-white/90 shadow-[0_24px_70px_-40px_rgba(14,116,144,0.35)] backdrop-blur'>
<div className='grid gap-8 p-6 md:grid-cols-[1.15fr_0.85fr] md:p-10'>
<div className='flex flex-col justify-between gap-8'>
<div className='space-y-6'>
<div className='inline-flex items-center gap-2 rounded-full border border-cyan-200 bg-cyan-50 px-4 py-1.5 text-xs font-medium uppercase tracking-[0.24em] text-cyan-800'>
Electron + Vite + React + Tailwind
</div>
<div className='space-y-4'>
<h1 className='max-w-xl text-4xl font-semibold tracking-tight text-slate-900 sm:text-5xl'>
Modern starter, cleaner rhythm, unified visual language.
</h1>
<p className='max-w-2xl text-base leading-7 text-slate-600 sm:text-lg'>
Refined spacing, balanced contrast, and consistent cards make the page feel more polished while keeping all demo functionality intact.
</p>
</div>
</div>
<a
href='https://github.com/electron-vite/electron-vite-react'
target='_blank'
rel='noreferrer'
className='group inline-flex w-fit items-center gap-4 rounded-2xl border border-slate-200 bg-white p-4 shadow-sm transition-all hover:border-cyan-300 hover:shadow-md'
>
<span className='relative flex h-10 w-10 items-center justify-center'>
<img src={logoVite} className='h-8 w-8' alt='Vite logo' />
<img
src={logoElectron}
className='absolute h-8 w-8 motion-safe:animate-spin [animation-duration:20s]'
alt='Electron logo'
/>
</span>
<span className='pr-2 text-sm font-semibold text-slate-700 transition-colors group-hover:text-cyan-700'>
Open project repository
</span>
</a>
</div>
<div className='relative overflow-hidden rounded-[1.75rem] border border-slate-200 bg-gradient-to-br from-cyan-50 to-white p-6'>
<div className='pointer-events-none absolute -right-8 -top-8 h-28 w-28 rounded-full bg-cyan-200/60 blur-2xl' />
<div className='relative space-y-4'>
<div className='flex items-center justify-between'>
<div className='text-sm uppercase tracking-[0.3em] text-slate-500'>Counter demo</div>
<img src={logoTailwind} className='h-6 w-6 opacity-90' alt='Tailwind CSS logo' />
</div>
<div className='text-5xl font-semibold text-slate-900'>{count}</div>
<button
onClick={() => setCount((value) => value + 1)}
className='inline-flex items-center justify-center rounded-2xl bg-cyan-500 px-5 py-3 font-semibold text-white transition hover:bg-cyan-600 focus:outline-none focus:ring-2 focus:ring-cyan-300 focus:ring-offset-2 focus:ring-offset-white'
>
Increment counter
</button>
<p className='text-sm leading-6 text-slate-600'>
Edit <code>src/App.tsx</code> and save to test HMR.
</p>
</div>
</div>
</div>
</section>
<section className='grid gap-4 md:grid-cols-3'>
<div className='rounded-3xl border border-slate-200 bg-white p-6 text-slate-800 shadow-[0_18px_36px_-28px_rgba(15,23,42,0.35)]'>
<div className='text-sm uppercase tracking-[0.3em] text-slate-500'>Public assets</div>
<p className='mt-3 text-base leading-7'>
Place static files into the <code>/public</code> folder.
</p>
</div>
<div className='rounded-3xl border border-cyan-200 bg-gradient-to-br from-cyan-50 to-sky-50 p-6 text-slate-800 shadow-[0_18px_36px_-28px_rgba(14,116,144,0.4)]'>
<div className='flex items-center gap-2 text-sm uppercase tracking-[0.3em] text-cyan-700'>
<img src={logoTailwind} className='h-5 w-5' alt='Tailwind CSS logo' />
Tailwind system
</div>
<p className='mt-3 text-base leading-7 text-slate-700'>
Unified utility classes now drive layout, hierarchy, and component consistency across the app.
</p>
</div>
<div className='rounded-3xl border border-slate-200 bg-white p-6 text-slate-800 shadow-[0_18px_36px_-28px_rgba(15,23,42,0.35)]'>
<div className='text-sm uppercase tracking-[0.3em] text-slate-500'>Update panel</div>
<p className='mt-3 text-base leading-7'>
Built-in updater UI follows the same spacing and typography rules for a more harmonious experience.
</p>
</div>
</section>
<UpdateElectron />
</div>
</div>
)
}
+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128"><path d="M64.004 25.602c-17.067 0-27.73 8.53-32 25.597 6.398-8.531 13.867-11.73 22.398-9.597 4.871 1.214 8.352 4.746 12.207 8.66C72.883 56.629 80.145 64 96.004 64c17.066 0 27.73-8.531 32-25.602-6.399 8.536-13.867 11.735-22.399 9.602-4.87-1.215-8.347-4.746-12.207-8.66-6.27-6.367-13.53-13.738-29.394-13.738zM32.004 64c-17.066 0-27.73 8.531-32 25.602C6.402 81.066 13.87 77.867 22.402 80c4.871 1.215 8.352 4.746 12.207 8.66 6.274 6.367 13.536 13.738 29.395 13.738 17.066 0 27.73-8.53 32-25.597-6.399 8.531-13.867 11.73-22.399 9.597-4.87-1.214-8.347-4.746-12.207-8.66C55.128 71.371 47.868 64 32.004 64zm0 0" fill="#38bdf8"/></svg>

After

Width:  |  Height:  |  Size: 688 B

+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128"><path d="M64.004 25.602c-17.067 0-27.73 8.53-32 25.597 6.398-8.531 13.867-11.73 22.398-9.597 4.871 1.214 8.352 4.746 12.207 8.66C72.883 56.629 80.145 64 96.004 64c17.066 0 27.73-8.531 32-25.602-6.399 8.536-13.867 11.735-22.399 9.602-4.87-1.215-8.347-4.746-12.207-8.66-6.27-6.367-13.53-13.738-29.394-13.738zM32.004 64c-17.066 0-27.73 8.531-32 25.602C6.402 81.066 13.87 77.867 22.402 80c4.871 1.215 8.352 4.746 12.207 8.66 6.274 6.367 13.536 13.738 29.395 13.738 17.066 0 27.73-8.53 32-25.597-6.399 8.531-13.867 11.73-22.399 9.597-4.87-1.214-8.347-4.746-12.207-8.66C55.128 71.371 47.868 64 32.004 64zm0 0" fill="#38bdf8"/></svg>

After

Width:  |  Height:  |  Size: 689 B

-67
View File
@@ -1,67 +0,0 @@
import React, { ReactNode } from 'react'
import { createPortal } from 'react-dom'
import './modal.css'
const ModalTemplate: React.FC<React.PropsWithChildren<{
title?: ReactNode
footer?: ReactNode
cancelText?: string
okText?: string
onCancel?: () => void
onOk?: () => void
width?: number
}>> = props => {
const {
title,
children,
footer,
cancelText = 'Cancel',
okText = 'OK',
onCancel,
onOk,
width = 530,
} = props
return (
<div className='update-modal'>
<div className='update-modal__mask' />
<div className='update-modal__warp'>
<div className='update-modal__content' style={{ width }}>
<div className='content__header'>
<div className='content__header-text'>{title}</div>
<span
className='update-modal--close'
onClick={onCancel}
>
<svg
viewBox="0 0 1024 1024"
version="1.1" xmlns="http://www.w3.org/2000/svg"
>
<path d="M557.312 513.248l265.28-263.904c12.544-12.48 12.608-32.704 0.128-45.248-12.512-12.576-32.704-12.608-45.248-0.128l-265.344 263.936-263.04-263.84C236.64 191.584 216.384 191.52 203.84 204 191.328 216.48 191.296 236.736 203.776 249.28l262.976 263.776L201.6 776.8c-12.544 12.48-12.608 32.704-0.128 45.248 6.24 6.272 14.464 9.44 22.688 9.44 8.16 0 16.32-3.104 22.56-9.312l265.216-263.808 265.44 266.24c6.24 6.272 14.432 9.408 22.656 9.408 8.192 0 16.352-3.136 22.592-9.344 12.512-12.48 12.544-32.704 0.064-45.248L557.312 513.248z" p-id="2764" fill="currentColor">
</path>
</svg>
</span>
</div>
<div className='content__body'>{children}</div>
{typeof footer !== 'undefined' ? (
<div className='content__footer'>
<button onClick={onCancel}>{cancelText}</button>
<button onClick={onOk}>{okText}</button>
</div>
) : footer}
</div>
</div>
</div>
)
}
const Modal = (props: Parameters<typeof ModalTemplate>[0] & { open: boolean }) => {
const { open, ...omit } = props
return createPortal(
open ? ModalTemplate(omit) : null,
document.body,
)
}
export default Modal
-87
View File
@@ -1,87 +0,0 @@
.update-modal {
--primary-color: rgb(224, 30, 90);
.update-modal__mask {
width: 100vw;
height: 100vh;
position: fixed;
left: 0;
top: 0;
z-index: 9;
background: rgba(0, 0, 0, 0.45);
}
.update-modal__warp {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 19;
}
.update-modal__content {
box-shadow: 0 0 10px -4px rgb(130, 86, 208);
overflow: hidden;
border-radius: 4px;
.content__header {
display: flex;
line-height: 38px;
background-color: var(--primary-color);
.content__header-text {
font-weight: bold;
width: 0;
flex-grow: 1;
}
}
.update-modal--close {
width: 30px;
height: 30px;
margin: 4px;
line-height: 34px;
text-align: center;
cursor: pointer;
svg {
width: 17px;
height: 17px;
}
}
.content__body {
padding: 10px;
background-color: #fff;
color: #333;
}
.content__footer {
padding: 10px;
background-color: #fff;
display: flex;
justify-content: flex-end;
button {
padding: 7px 11px;
background-color: var(--primary-color);
font-size: 14px;
margin-left: 10px;
&:first-child {
margin-left: 0;
}
}
}
}
.icon {
padding: 0 15px;
width: 20px;
fill: currentColor;
&:hover {
color: rgba(0, 0, 0, 0.4);
}
}
}
-22
View File
@@ -1,22 +0,0 @@
import React from 'react'
import './progress.css'
const Progress: React.FC<React.PropsWithChildren<{
percent?: number
}>> = props => {
const { percent = 0 } = props
return (
<div className='update-progress'>
<div className='update-progress-pr'>
<div
className='update-progress-rate'
style={{ width: `${3 * percent}px` }}
/>
</div>
<span className='update-progress-num'>{(percent ?? 0).toString().substring(0, 4)}%</span>
</div>
)
}
export default Progress
@@ -1,21 +0,0 @@
.update-progress {
display: flex;
align-items: center;
.update-progress-pr {
border: 1px solid #000;
border-radius: 3px;
height: 6px;
width: 300px;
}
.update-progress-rate {
height: 6px;
border-radius: 3px;
background-image: linear-gradient(to right, rgb(130, 86, 208) 0%, var(--primary-color) 100%)
}
.update-progress-num {
margin: 0 10px;
}
}
+33 -27
View File
@@ -2,48 +2,54 @@
English | [简体中文](README.zh-CN.md)
> Use `electron-updater` to realize the update detection, download and installation of the electric program.
> Use `electron-updater` to detect, download, and install Electron app updates.
```sh
npm i electron-updater
pnpm add electron-updater
```
### Main logic
## Main Flow
1. ##### Configuration of the update the service address and update information script:
### 1. Configure the update server
Add a `publish` field to `electron-builder.json5` for configuring the update address and which strategy to use as the update service.
Add a `publish` field to `electron-builder.json` to define the update source and provider.
``` json5
{
"publish": {
"provider": "generic",
"channel": "latest",
"url": "https://foo.com/"
}
}
```
```json5
{
publish: {
provider: 'generic',
channel: 'latest',
url: 'https://foo.com/',
},
}
```
For more information, please refer to : [electron-builder.json5...](https://github.com/electron-vite/electron-vite-react/blob/2f2880a9f19de50ff14a0785b32a4d5427477e55/electron-builder.json5#L38)
For more information, please refer to [electron-builder.json](https://github.com/electron-vite/electron-vite-react/blob/main/electron-builder.json)
2. ##### The update logic of Electron:
### 2. Main-process update logic
- Checking if an update is available;
- Checking the version of the software on the server;
- Checking if an update is available;
- Downloading the new version of the software from the server (when an update is available);
- Installation method;
This project keeps `autoDownload` disabled, so users start downloads manually.
For more information, please refer to : [update...](https://github.com/electron-vite/electron-vite-react/blob/main/electron/main/update.ts)
- `check-update` calls `autoUpdater.checkForUpdates()` after packaging.
- The main process emits `update-can-available` when it receives `update-available` or `update-not-available`.
- `start-download` begins downloading the update and forwards download progress to the renderer.
- `cancel-download` stops the current download and resets the cancellation token.
- `quit-and-install` installs the downloaded update and restarts the app.
3. ##### Updating UI pages in Electron:
For more information, please refer to [update.ts](https://github.com/electron-vite/electron-vite-react/blob/main/electron/main/update.ts)
The main function is to provide a UI page for users to trigger the update logic mentioned in (2.) above. Users can click on the page to trigger different update functions in Electron.
### 3. Renderer UI
For more information, please refer to : [components/update...](https://github.com/electron-vite/electron-vite-react/blob/main/src/components/update/index.tsx)
The update page is the user-facing entry point for the flow above. Users click the page button to trigger update actions in Electron.
- Click **Check update** to trigger `check-update`.
- If an update is available, the page lets the user start or cancel the download.
- After the download completes, the page switches to **Install now**.
For more information, please refer to [components/update/index.tsx](https://github.com/electron-vite/electron-vite-react/blob/main/src/components/update/index.tsx)
---
Here it is recommended to trigger updates through user actions (in this project, Electron update function is triggered after the user clicks on the "Check for updates" button).
Updates should be triggered by user action. In this project, the flow starts after the user clicks **Check update**.
For more information on using `electron-updater` for Electron updates, please refer to the documentation : [auto-update](https://www.electron.build/.html)
For more information on using `electron-updater` for Electron updates, please refer to the [documentation](https://www.electron.build)
+32 -28
View File
@@ -1,51 +1,55 @@
# electron-auto-update
# electron-updater
[English](README.md) | 简体中文
使用`electron-updater`实现electron程序的更新检测、下载和安装等功能。
使用 `electron-updater` 检测、下载并安装 Electron 应用更新。
```sh
npm i electron-updater
pnpm add electron-updater
```
### 主要逻辑
## 主要流程
1. ##### 更新地址、更新信息脚本的配置
### 1. 配置更新服务
在`electron-builder.json5`添加`publish`字段,用来配置更新地址和使用哪种策略作为更新服务
在 `electron-builder.json` 中添加 `publish` 字段,用来配置更新地址和 provider。
``` json5
{
"publish": {
"provider": "generic", // 提供者、提供商
"channel": "latest", // 生成yml文件的名称
"url": "https://foo.com/" //更新地址
}
}
```
```json5
{
publish: {
provider: 'generic',
channel: 'latest',
url: 'https://foo.com/',
},
}
```
更多见 : [electron-builder.json5...](xxx)
更多详情请见:[electron-builder.json](https://github.com/electron-vite/electron-vite-react/blob/main/electron-builder.json)
2. ##### Electron更新逻辑
### 2. 主进程更新逻辑
- 检测更新是否可用;
本项目保持 `autoDownload` 关闭,因此下载需要由用户手动触发。
- 检测服务端的软件版本;
- `check-update` 会在打包后调用 `autoUpdater.checkForUpdates()`。
- 主进程在收到 `update-available` 或 `update-not-available` 时,会向渲染进程发送 `update-can-available`。
- `start-download` 会开始下载更新,并把下载进度转发到渲染进程。
- `cancel-download` 会取消当前下载,并重置取消令牌。
- `quit-and-install` 会安装已下载的更新并重启应用。
- 检测更新是否可用;
更多详情请见:[update.ts](https://github.com/electron-vite/electron-vite-react/blob/main/electron/main/update.ts)
- 下载服务端新版软件(当更新可用);
- 安装方式;
### 3. 渲染进程 UI
更多见 : [update...](https://github.com/electron-vite/electron-vite-react/blob/main/electron/main/update.ts)
更新页面是用户触发上述流程的入口,用户通过页面按钮触发 Electron 的更新动作。
3. ##### Electron更新UI页面
- 点击 **Check update** 触发 `check-update`。
- 如果发现新版本,页面会允许用户开始或取消下载。
- 下载完成后,页面会切换为 **Install now**。
主要功能是:用户触发上述(2.)更新逻辑的UI页面。用户可以通过点击页面触发electron更新的不同功能。
更多见 : [components/update.ts...](https://github.com/electron-vite/electron-vite-react/tree/main/src/components/update/index.tsx)
更多详情请见:[components/update/index.tsx](https://github.com/electron-vite/electron-vite-react/blob/main/src/components/update/index.tsx)
---
这里建议更新触发以用户操作触发(本项目的以用户点击 **更新检测** 后触发electron更新功能)
建议通过用户操作触发更新。在本项目中,流程从用户点击 **Check update** 开始。
关于更多使用`electron-updater`进行electron更新,见文档:[auto-update](https://www.electron.build/.html)
更多使用 `electron-updater` 的信息请参考 [官方文档](https://www.electron.build)
+59 -46
View File
@@ -1,8 +1,7 @@
import type { ProgressInfo } from 'electron-updater'
import { useCallback, useEffect, useState } from 'react'
import Modal from '@/components/update/Modal'
import Progress from '@/components/update/Progress'
import './update.css'
import Modal from '@/components/update/modal'
import Progress from '@/components/update/progress'
const Update = () => {
const [checking, setChecking] = useState(false)
@@ -36,35 +35,41 @@ const Update = () => {
}
}
const onUpdateCanAvailable = useCallback((_event: Electron.IpcRendererEvent, arg1: VersionInfo) => {
setVersionInfo(arg1)
setUpdateError(undefined)
// Can be update
if (arg1.update) {
setModalBtn(state => ({
...state,
cancelText: 'Cancel',
okText: 'Update',
onOk: () => window.ipcRenderer.invoke('start-download'),
}))
setUpdateAvailable(true)
} else {
setUpdateAvailable(false)
}
}, [])
const onUpdateCanAvailable = useCallback(
(_event: Electron.IpcRendererEvent, arg1: VersionInfo) => {
setVersionInfo(arg1)
setUpdateError(undefined)
// Can be update
if (arg1.update) {
setModalBtn((state) => ({
...state,
cancelText: 'Cancel',
okText: 'Update',
onOk: () => window.ipcRenderer.invoke('start-download'),
}))
setUpdateAvailable(true)
} else {
setUpdateAvailable(false)
}
},
[],
)
const onUpdateError = useCallback((_event: Electron.IpcRendererEvent, arg1: ErrorType) => {
setUpdateAvailable(false)
setUpdateError(arg1)
}, [])
const onDownloadProgress = useCallback((_event: Electron.IpcRendererEvent, arg1: ProgressInfo) => {
setProgressInfo(arg1)
}, [])
const onDownloadProgress = useCallback(
(_event: Electron.IpcRendererEvent, arg1: ProgressInfo) => {
setProgressInfo(arg1)
},
[],
)
const onUpdateDownloaded = useCallback((_event: Electron.IpcRendererEvent, ...args: any[]) => {
setProgressInfo({ percent: 100 })
setModalBtn(state => ({
setModalBtn((state) => ({
...state,
cancelText: 'Later',
okText: 'Install now',
@@ -95,34 +100,42 @@ const Update = () => {
okText={modalBtn?.okText}
onCancel={modalBtn?.onCancel}
onOk={modalBtn?.onOk}
footer={updateAvailable ? /* hide footer */null : undefined}
title="Updater"
footer={updateAvailable ? /* hide footer */ null : undefined}
>
<div className='modal-slot'>
{updateError
? (
<div>
<p>Error downloading the latest version.</p>
<p>{updateError.message}</p>
<div className="space-y-3">
{updateError ? (
<div className="text-sm leading-6 text-rose-700">
<p className="font-semibold text-rose-900">Error downloading the latest version.</p>
<p className="mt-1 max-h-40 overflow-auto">{updateError.message}</p>
</div>
) : updateAvailable ? (
<div className="space-y-3 text-sm text-slate-700">
<div className="text-base font-semibold text-slate-900">
The latest version is v{versionInfo?.newVersion}
</div>
) : updateAvailable
? (
<div>
<div>The last version is: v{versionInfo?.newVersion}</div>
<div className='new-version__target'>v{versionInfo?.version} -&gt; v{versionInfo?.newVersion}</div>
<div className='update__progress'>
<div className='progress__title'>Update progress:</div>
<div className='progress__bar'>
<Progress percent={progressInfo?.percent} ></Progress>
</div>
</div>
<div className="text-slate-600">
v{versionInfo?.version} -&gt; v{versionInfo?.newVersion}
</div>
<div className="flex items-center gap-3 pt-1">
<div className="shrink-0 font-medium text-slate-700">Update progress:</div>
<div className="min-w-0 flex-1">
<Progress percent={progressInfo?.percent}></Progress>
</div>
)
: (
<div className='can-not-available'>{JSON.stringify(versionInfo ?? {}, null, 2)}</div>
)}
</div>
</div>
) : (
<pre className="overflow-auto text-left text-xs leading-6 text-slate-700">
{JSON.stringify(versionInfo ?? {}, null, 2)}
</pre>
)}
</div>
</Modal>
<button disabled={checking} onClick={checkUpdate}>
<button
disabled={checking}
onClick={checkUpdate}
className="inline-flex items-center justify-center rounded-2xl border border-cyan-700/15 bg-cyan-600 px-5 py-3 font-semibold text-white shadow-sm shadow-cyan-800/20 transition hover:bg-cyan-700 disabled:cursor-not-allowed disabled:bg-cyan-300 disabled:text-cyan-700"
>
{checking ? 'Checking...' : 'Check update'}
</button>
</>
+88
View File
@@ -0,0 +1,88 @@
import React, { type ReactNode } from 'react'
import { createPortal } from 'react-dom'
const ModalTemplate: React.FC<
React.PropsWithChildren<{
title?: ReactNode
footer?: ReactNode
cancelText?: string
okText?: string
onCancel?: () => void
onOk?: () => void
width?: number
}>
> = (props) => {
const {
title,
children,
footer,
cancelText = 'Cancel',
okText = 'OK',
onCancel,
onOk,
width = 460,
} = props
return (
<div className="fixed inset-0 z-50">
<div className="absolute inset-0 bg-slate-900/14 backdrop-blur-sm" onClick={onCancel} />
<div className="absolute left-1/2 top-1/2 w-[min(calc(100vw-2rem),_460px)] -translate-x-1/2 -translate-y-1/2">
<div
className="overflow-hidden rounded-[1.5rem] border border-slate-200 bg-white px-5 py-4 shadow-[0_24px_60px_-32px_rgba(15,23,42,0.28)]"
style={{ width }}
>
<div className="flex items-start gap-3 text-slate-900">
<div className="min-w-0 flex-1">
<div className="text-xs font-semibold uppercase tracking-[0.24em] text-cyan-700">System</div>
<div className="mt-1 text-2xl font-semibold tracking-tight">{title}</div>
</div>
<span
className="inline-flex h-10 w-10 cursor-pointer items-center justify-center rounded-full bg-slate-100 text-slate-700 transition hover:bg-slate-200"
onClick={onCancel}
>
<svg
viewBox="0 0 1024 1024"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
className="h-4 w-4"
>
<path
d="M557.312 513.248l265.28-263.904c12.544-12.48 12.608-32.704 0.128-45.248-12.512-12.576-32.704-12.608-45.248-0.128l-265.344 263.936-263.04-263.84C236.64 191.584 216.384 191.52 203.84 204 191.328 216.48 191.296 236.736 203.776 249.28l262.976 263.776L201.6 776.8c-12.544 12.48-12.608 32.704-0.128 45.248 6.24 6.272 14.464 9.44 22.688 9.44 8.16 0 16.32-3.104 22.56-9.312l265.216-263.808 265.44 266.24c6.24 6.272 14.432 9.408 22.656 9.408 8.192 0 16.352-3.136 22.592-9.344 12.512-12.48 12.544-32.704 0.064-45.248L557.312 513.248z"
p-id="2764"
fill="currentColor"
></path>
</svg>
</span>
</div>
<div className="pt-4 text-slate-700">{children}</div>
{footer !== undefined ? (
<div className="flex justify-end gap-2 pt-4">
<button
onClick={onCancel}
className="rounded-xl border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700 transition hover:bg-slate-50"
>
{cancelText}
</button>
<button
onClick={onOk}
className="rounded-xl bg-cyan-600 px-4 py-2 text-sm font-medium text-white transition hover:bg-cyan-700"
>
{okText}
</button>
</div>
) : (
footer
)}
</div>
</div>
</div>
)
}
const Modal = (props: Parameters<typeof ModalTemplate>[0] & { open: boolean }) => {
const { open, ...omit } = props
return createPortal(open ? <ModalTemplate {...omit} /> : null, document.body)
}
export default Modal
+26
View File
@@ -0,0 +1,26 @@
import React from 'react'
const Progress: React.FC<
React.PropsWithChildren<{
percent?: number
}>
> = (props) => {
const { percent = 0 } = props
const normalizedPercent = Math.min(Math.max(percent ?? 0, 0), 100)
return (
<div className="flex items-center gap-2">
<div className="h-1.5 flex-1 overflow-hidden rounded-full bg-slate-200">
<div
className="h-full rounded-full bg-cyan-600 transition-[width] duration-300 ease-out"
style={{ width: `${normalizedPercent}%` }}
/>
</div>
<span className="min-w-[3rem] text-right text-xs font-medium text-slate-600">
{normalizedPercent.toFixed(1)}%
</span>
</div>
)
}
export default Progress
-24
View File
@@ -1,24 +0,0 @@
.modal-slot {
.update-progress {
display: flex;
}
.new-version__target,
.update__progress {
margin-left: 40px;
}
.progress__title {
margin-right: 10px;
}
.progress__bar {
width: 0;
flex-grow: 1;
}
.can-not-available {
padding: 20px;
text-align: center;
}
}
+74 -84
View File
@@ -1,94 +1,84 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
@import "tailwindcss";
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
@theme {
--font-sans: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
code {
background-color: #1a1a1a;
padding: 2px 4px;
margin: 0 4px;
border-radius: 4px;
}
.card {
padding: 2em;
}
#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
@media (prefers-color-scheme: light) {
@layer base {
:root {
color: #213547;
background-color: #ffffff;
color-scheme: light;
font-family: var(--font-sans);
line-height: 1.5;
font-weight: 400;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}
a:hover {
color: #747bff;
html {
min-width: 320px;
min-height: 100%;
background: #f8fafc;
}
button {
background-color: #f9f9f9;
body {
min-width: 320px;
min-height: 100vh;
margin: 0;
background:
radial-gradient(circle at top, rgba(56, 189, 248, 0.16), transparent 34%),
radial-gradient(circle at bottom right, rgba(14, 165, 233, 0.12), transparent 28%),
#f8fafc;
color: #0f172a;
}
#root {
min-height: 100vh;
background: transparent;
}
a {
color: inherit;
text-decoration: none;
}
button,
input,
textarea,
select {
font: inherit;
}
code {
background-color: #f9f9f9;
border-radius: 0.375rem;
border: 1px solid rgba(148, 163, 184, 0.35);
background: #f1f5f9;
padding: 0.125rem 0.375rem;
color: #0f172a;
}
* {
scrollbar-color: #94a3b8 #e2e8f0;
}
*::-webkit-scrollbar {
width: 10px;
height: 10px;
}
*::-webkit-scrollbar-track {
background: #e2e8f0;
}
*::-webkit-scrollbar-thumb {
border: 2px solid #e2e8f0;
border-radius: 9999px;
background: #94a3b8;
}
*::-webkit-scrollbar-thumb:hover {
background: #64748b;
}
}
-14
View File
@@ -1,14 +0,0 @@
/** @type {import('tailwindcss').Config} */
export default {
content: [
'./index.html',
'./src/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {},
},
corePlugins: {
preflight: false,
},
plugins: [],
}
-64
View File
@@ -1,64 +0,0 @@
import path from 'node:path'
import {
type ElectronApplication,
type Page,
type JSHandle,
_electron as electron,
} from 'playwright'
import type { BrowserWindow } from 'electron'
import {
beforeAll,
afterAll,
describe,
expect,
test,
} from 'vitest'
const root = path.join(__dirname, '..')
let electronApp: ElectronApplication
let page: Page
if (process.platform === 'linux') {
// pass ubuntu
test(() => expect(true).true)
} else {
beforeAll(async () => {
electronApp = await electron.launch({
args: ['.', '--no-sandbox'],
cwd: root,
env: { ...process.env, NODE_ENV: 'development' },
})
page = await electronApp.firstWindow()
const mainWin: JSHandle<BrowserWindow> = await electronApp.browserWindow(page)
await mainWin.evaluate(async (win) => {
win.webContents.executeJavaScript('console.log("Execute JavaScript with e2e testing.")')
})
})
afterAll(async () => {
await page.screenshot({ path: 'test/screenshots/e2e.png' })
await page.close()
await electronApp.close()
})
describe('[electron-vite-react] e2e tests', async () => {
test('startup', async () => {
const title = await page.title()
expect(title).eq('Electron + Vite + React')
})
test('should be home page is load correctly', async () => {
const h1 = await page.$('h1')
const title = await h1?.textContent()
expect(title).eq('Electron + Vite + React')
})
test('should be count button can click', async () => {
const countButton = await page.$('button')
await countButton?.click()
const countValue = await countButton?.textContent()
expect(countValue).eq('count is 1')
})
})
}
+95
View File
@@ -0,0 +1,95 @@
import path from 'node:path'
import { spawn, type ChildProcess } from 'node:child_process'
import {
type ElectronApplication,
type Page,
type JSHandle,
expect,
test,
_electron as electron,
} from '@playwright/test'
import type { BrowserWindow } from 'electron'
const root = path.resolve(import.meta.dirname, '..', '..')
let electronApp: ElectronApplication
let page: Page
let xvfbProcess: ChildProcess | undefined
function startXvfbOnLinux(): Promise<void> {
if (process.platform !== 'linux' || process.env.DISPLAY) {
return Promise.resolve()
}
return new Promise((resolve, reject) => {
xvfbProcess = spawn('Xvfb', [':99', '-screen', '0', '1280x720x24', '-ac'], {
stdio: 'ignore',
detached: true,
})
xvfbProcess.once('error', reject)
setTimeout(() => {
process.env.DISPLAY = ':99'
resolve()
}, 500)
})
}
test.beforeAll(async () => {
test.setTimeout(30000)
await startXvfbOnLinux()
electronApp = await electron.launch({
args: ['.', '--no-sandbox'],
cwd: root,
env: { ...process.env, NODE_ENV: 'development' },
})
page = await electronApp.firstWindow()
const mainWin: JSHandle<BrowserWindow> = await electronApp.browserWindow(page)
await mainWin.evaluate(async (win) => {
win.webContents.executeJavaScript('console.log("Execute JavaScript with e2e testing.")')
})
})
test.afterAll(async () => {
if (page) {
await page.screenshot({ path: 'test/screenshots/e2e.png' })
await page.close()
}
if (electronApp) {
await electronApp.close()
}
if (xvfbProcess?.pid) {
process.kill(-xvfbProcess.pid)
xvfbProcess = undefined
}
})
test.describe('[electron-vite-react] e2e tests', () => {
test('startup', async () => {
const title = await page.title()
expect(title).toBe('Electron + Vite + React')
})
test('should be home page is load correctly', async () => {
const h1 = await page.$('h1')
const title = await h1?.textContent()
expect(title).toBe('A sharp starter with Tailwind-first styling.')
})
test('should be count button can click', async () => {
const countButton = await page.$('button:has-text("Increment counter")')
const countValue = await page.$('div.text-5xl')
const valueBeforeClick = await countValue?.textContent()
expect(valueBeforeClick).toBe('0')
await countButton?.click()
const valueAfterClick = await countValue?.textContent()
expect(valueAfterClick).toBe('1')
})
})
+11
View File
@@ -0,0 +1,11 @@
import { describe, it, expect } from 'vitest'
describe('vitest smoke', () => {
it('runs a normal unit test', () => {
expect(1 + 1).toBe(2)
})
it('has test mode enabled while running tests', () => {
expect(process.env.NODE_ENV).toBe('test')
})
})
+8 -6
View File
@@ -3,25 +3,27 @@
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"types": ["vite/client"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"verbatimModuleSyntax": true,
"moduleDetection": "force",
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"baseUrl": "./",
"paths": {
"@/*": [
"src/*"
"./src/*"
]
},
}
},
"include": ["src", "electron"],
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
+6 -2
View File
@@ -2,9 +2,13 @@
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"moduleResolution": "bundler",
"moduleDetection": "force",
"verbatimModuleSyntax": true,
"types": ["node"],
"skipLibCheck": true,
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts", "package.json"]
"include": ["electron", "vite.config.ts", "vitest.config.ts", "package.json"]
}
+20 -28
View File
@@ -2,9 +2,15 @@ import { rmSync } from 'node:fs'
import path from 'node:path'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import electron from 'vite-plugin-electron/simple'
import tailwindcss from '@tailwindcss/vite'
import { electronSimple } from 'vite-plugin-electron/multi-env'
import { notBundle } from 'vite-plugin-electron/plugin'
import pkg from './package.json'
const external = Object.keys(
'dependencies' in pkg ? (pkg.dependencies as Record<string, string>) : {},
)
// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
rmSync('dist-electron', { recursive: true, force: true })
@@ -16,44 +22,37 @@ export default defineConfig(({ command }) => {
return {
resolve: {
alias: {
'@': path.join(__dirname, 'src')
'@': path.join(__dirname, 'src'),
},
},
plugins: [
react(),
electron({
tailwindcss(),
electronSimple({
main: {
// Shortcut of `build.lib.entry`
entry: 'electron/main/index.ts',
onstart(args) {
if (process.env.VSCODE_DEBUG) {
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
} else {
args.startup()
}
},
vite: {
input: 'electron/main/index.ts',
plugins: [notBundle()],
options: {
build: {
sourcemap,
minify: isBuild,
outDir: 'dist-electron/main',
rollupOptions: {
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
rolldownOptions: {
external,
},
},
},
},
preload: {
// Shortcut of `build.rollupOptions.input`.
// Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`.
input: 'electron/preload/index.ts',
vite: {
plugins: [notBundle()],
options: {
build: {
sourcemap: sourcemap ? 'inline' : undefined, // #332
minify: isBuild,
outDir: 'dist-electron/preload',
rollupOptions: {
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
rolldownOptions: {
external,
},
},
},
@@ -61,16 +60,9 @@ export default defineConfig(({ command }) => {
// Polyfill the Electron and Node.js API for Renderer process.
// If you want use Node.js in Renderer process, the `nodeIntegration` needs to be enabled in the Main process.
// See 👉 https://github.com/electron-vite/vite-plugin-electron-renderer
renderer: {},
// renderer: {},
}),
],
server: process.env.VSCODE_DEBUG && (() => {
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
return {
host: url.hostname,
port: +url.port,
}
})(),
clearScreen: false,
}
})
+2
View File
@@ -4,6 +4,8 @@ export default defineConfig({
test: {
root: __dirname,
include: ['test/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
exclude: ['test/e2e.spec.ts'],
passWithNoTests: true,
testTimeout: 1000 * 29,
},
})