Skip to content

Commit b40caee

Browse files
authored
🔨 chore: add desktop pre-code to validate build process (lobehub#7261)
* add code * fix lint * fix tests
1 parent 5897d9e commit b40caee

File tree

29 files changed

+358
-40
lines changed

29 files changed

+358
-40
lines changed

next.config.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@ import ReactComponentName from 'react-scan/react-component-name/webpack';
66

77
const isProd = process.env.NODE_ENV === 'production';
88
const buildWithDocker = process.env.DOCKER === 'true';
9+
const isDesktop = process.env.NEXT_PUBLIC_IS_DESKTOP_APP === '1';
910
const enableReactScan = !!process.env.REACT_SCAN_MONITOR_API_KEY;
1011
const isUsePglite = process.env.NEXT_PUBLIC_CLIENT_DB === 'pglite';
1112

1213
// if you need to proxy the api endpoint to remote server
1314

1415
const basePath = process.env.NEXT_PUBLIC_BASE_PATH;
16+
const isStandaloneMode = buildWithDocker || isDesktop;
17+
18+
const standaloneConfig: NextConfig = {
19+
output: 'standalone',
20+
outputFileTracingIncludes: { '*': ['public/**/*', '.next/static/**/*'] },
21+
};
1522

1623
const nextConfig: NextConfig = {
24+
...(isStandaloneMode ? standaloneConfig : {}),
1725
basePath,
1826
compress: isProd,
1927
experimental: {
@@ -110,10 +118,6 @@ const nextConfig: NextConfig = {
110118
hmrRefreshes: true,
111119
},
112120
},
113-
output: buildWithDocker ? 'standalone' : undefined,
114-
outputFileTracingIncludes: buildWithDocker
115-
? { '*': ['public/**/*', '.next/static/**/*'] }
116-
: undefined,
117121
reactStrictMode: true,
118122
redirects: async () => [
119123
{
@@ -231,13 +235,14 @@ const noWrapper = (config: NextConfig) => config;
231235

232236
const withBundleAnalyzer = process.env.ANALYZE === 'true' ? analyzer() : noWrapper;
233237

234-
const withPWA = isProd
235-
? withSerwistInit({
236-
register: false,
237-
swDest: 'public/sw.js',
238-
swSrc: 'src/app/sw.ts',
239-
})
240-
: noWrapper;
238+
const withPWA =
239+
isProd && !isDesktop
240+
? withSerwistInit({
241+
register: false,
242+
swDest: 'public/sw.js',
243+
swSrc: 'src/app/sw.ts',
244+
})
245+
: noWrapper;
241246

242247
const hasSentry = !!process.env.NEXT_PUBLIC_SENTRY_DSN;
243248
const withSentry =

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"build-sitemap": "tsx ./scripts/buildSitemapIndex/index.ts",
3636
"build:analyze": "ANALYZE=true next build",
3737
"build:docker": "DOCKER=true next build && npm run build-sitemap",
38+
"build:electron": "NODE_OPTIONS=--max-old-space-size=6144 NEXT_PUBLIC_IS_DESKTOP_APP=1 next build ",
3839
"db:generate": "drizzle-kit generate && npm run db:generate-client && npm run workflow:dbml",
3940
"db:generate-client": "tsx ./scripts/migrateClientDB/compile-migrations.ts",
4041
"db:migrate": "MIGRATION_DB=1 tsx ./scripts/migrateServerDB/index.ts",
@@ -129,6 +130,8 @@
129130
"@icons-pack/react-simple-icons": "9.6.0",
130131
"@khmyznikov/pwa-install": "0.3.9",
131132
"@langchain/community": "^0.3.37",
133+
"@lobechat/electron-client-ipc": "workspace:*",
134+
"@lobechat/electron-server-ipc": "workspace:*",
132135
"@lobechat/web-crawler": "workspace:*",
133136
"@lobehub/charts": "^1.12.0",
134137
"@lobehub/chat-plugin-sdk": "^1.32.4",
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# @lobechat/electron-client-ipc
2+
3+
这个包是 LobeChat 在 Electron 环境中用于处理 IPC(进程间通信)的客户端工具包。
4+
5+
## 介绍
6+
7+
在 Electron 应用中,IPC(进程间通信)是连接主进程(Main Process)、渲染进程(Renderer Process)以及 NextJS 进程的桥梁。为了更好地组织和管理这些通信,我们将 IPC 相关的代码分成了两个包:
8+
9+
- `@lobechat/electron-client-ipc`**客户端 IPC 包**
10+
- `@lobechat/electron-server-ipc`**服务端 IPC 包**
11+
12+
## 主要区别
13+
14+
### electron-client-ipc(本包)
15+
16+
- 运行环境:在渲染进程(Renderer Process)中运行
17+
- 主要职责:
18+
- 提供渲染进程调用主进程方法的接口定义
19+
- 封装 `ipcRenderer.invoke` 相关方法
20+
- 处理与主进程的通信请求
21+
22+
### electron-server-ipc
23+
24+
- 运行环境:在 Electron 主进程和 Next.js 服务端进程中运行
25+
- 主要职责:
26+
- 提供基于 Socket 的 IPC 通信机制
27+
- 实现服务端(ElectronIPCServer)和客户端(ElectronIpcClient)通信组件
28+
- 处理跨进程的请求和响应
29+
- 提供自动重连和错误处理机制
30+
- 确保类型安全的 API 调用
31+
32+
## 使用场景
33+
34+
当渲染进程需要:
35+
36+
- 访问系统 API
37+
- 进行文件操作
38+
- 调用主进程特定功能
39+
40+
时,都需要通过 `electron-client-ipc` 包提供的方法来发起请求。
41+
42+
## 技术说明
43+
44+
这种分包设计遵循了关注点分离原则,使得:
45+
46+
- IPC 通信接口清晰可维护
47+
- 客户端和服务端代码解耦
48+
- TypeScript 类型定义共享,确保类型安全
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@lobechat/electron-client-ipc",
3+
"version": "1.0.0",
4+
"private": true,
5+
"main": "src/index.ts",
6+
"types": "src/index.ts"
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface DevtoolsDispatchEvents {
2+
/**
3+
* open the LobeHub Devtools
4+
*/
5+
openDevtools: () => void;
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { DevtoolsDispatchEvents } from './devtools';
2+
3+
/**
4+
* renderer -> main dispatch events
5+
*/
6+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
7+
export interface ClientDispatchEvents extends DevtoolsDispatchEvents {}
8+
9+
export type ClientDispatchEventKey = keyof ClientDispatchEvents;
10+
11+
export type ClientEventReturnType<T extends ClientDispatchEventKey> = ReturnType<
12+
ClientDispatchEvents[T]
13+
>;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './events';
2+
export * from './types';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type {
2+
ClientDispatchEventKey,
3+
ClientDispatchEvents,
4+
ClientEventReturnType,
5+
} from '../events';
6+
7+
export type DispatchInvoke = <T extends ClientDispatchEventKey>(
8+
event: T,
9+
...data: Parameters<ClientDispatchEvents[T]>
10+
) => Promise<ClientEventReturnType<T>>;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './dispatch';

packages/electron-server-ipc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ LobeHub 的 Electron 应用与服务端之间的 IPC(进程间通信)模块
44

55
## 📝 简介
66

7-
`@lobechat/electron-server-ipc` 是 LobeHub 桌面应用的核心组件,负责处理 Electron 进程与 nextjs 服务端之间的通信。它提供了一套简单而健壮的 API,用于在不同进程间传递数据和执行远程方法调用。
7+
`@lobechat/electron-server-ipc` 是 LobeHub 桌面应用的核心组件,负责处理 Electron 主进程与 nextjs 服务端之间的通信。它提供了一套简单而健壮的 API,用于在不同进程间传递数据和执行远程方法调用。
88

99
## 🛠️ 核心功能
1010

0 commit comments

Comments
 (0)