Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
210 lines (202 loc) · 6.32 KB
/
Copy pathvite.config.ts
File metadata and controls
210 lines (202 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import react from '@vitejs/plugin-react';
import fs from 'fs';
import path from 'path';
import { defineConfig, type Plugin } from 'vite';
import electron from 'vite-plugin-electron';
import renderer from 'vite-plugin-electron-renderer';
// https://vitejs.dev/config/
// PORT lets tooling (e.g. browser preview) assign a free port; electron:dev
// pins 5175 via the --port CLI flag, which overrides server.port anyway.
const devPort = Number(process.env.PORT ?? '') || 5175;
const katexVersion = process.env.npm_package_dependencies_katex?.replace(/^[~^]/, '') || '0.16.0';
const pdfJsAssetRoot = path.resolve(__dirname, 'node_modules/pdfjs-dist');
const pdfJsPublicPath = '/pdfjs/';
const pdfJsAssetDirs = [
{ route: `${pdfJsPublicPath}cmaps/`, source: path.join(pdfJsAssetRoot, 'cmaps'), output: 'cmaps' },
{ route: `${pdfJsPublicPath}standard_fonts/`, source: path.join(pdfJsAssetRoot, 'standard_fonts'), output: 'standard_fonts' },
];
function servePdfJsAsset(
reqUrl: string | undefined,
res: import('http').ServerResponse,
next: () => void,
): void {
if (!reqUrl) {
next();
return;
}
const pathname = new URL(reqUrl, 'http://localhost').pathname;
const assetDir = pdfJsAssetDirs.find(dir => pathname.startsWith(dir.route));
if (!assetDir) {
next();
return;
}
const relativePath = decodeURIComponent(pathname.slice(assetDir.route.length));
const assetPath = path.resolve(assetDir.source, relativePath);
if (!assetPath.startsWith(assetDir.source + path.sep)) {
res.statusCode = 403;
res.end('Forbidden');
return;
}
if (!fs.existsSync(assetPath) || !fs.statSync(assetPath).isFile()) {
res.statusCode = 404;
res.end('Not found');
return;
}
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
fs.createReadStream(assetPath).pipe(res);
}
function pdfJsStaticAssetsPlugin(): Plugin {
return {
name: 'pdfjs-static-assets',
configureServer(server) {
server.middlewares.use((req, res, next) => {
servePdfJsAsset(req.url, res, next);
});
},
writeBundle(outputOptions) {
const outputDir = outputOptions.dir || path.resolve(__dirname, 'dist');
const targetRoot = path.resolve(outputDir, `.${pdfJsPublicPath}`);
for (const assetDir of pdfJsAssetDirs) {
const targetDir = path.join(targetRoot, assetDir.output);
fs.rmSync(targetDir, { recursive: true, force: true });
fs.mkdirSync(path.dirname(targetDir), { recursive: true });
fs.cpSync(assetDir.source, targetDir, { recursive: true });
}
},
};
}
export default defineConfig({
define: {
// KaTeX ESM bundle references this compile-time constant.
__VERSION__: JSON.stringify(katexVersion),
},
plugins: [
react(),
pdfJsStaticAssetsPlugin(),
electron([
{
// 主进程入口文件
entry: 'src/main/main.ts',
vite: {
build: {
sourcemap: true,
outDir: 'dist-electron',
minify: false,
rollupOptions: {
external: (id) => {
const staticExternals = ['better-sqlite3', 'discord.js', 'zlib-sync', '@discordjs/opus', 'bufferutil', 'utf-8-validate', 'node-nim', 'nim-web-sdk-ng'];
if (staticExternals.includes(id)) return true;
if (id.startsWith('@larksuite/openclaw-lark-tools') || id.startsWith('@larksuite/openclaw-lark')) return true;
return false;
},
output: {
// Keep CJS format (default), but load via ESM loader.mjs
inlineDynamicImports: true,
},
},
},
},
// package.json starts Electron after all five output files stabilize.
onstart() {},
},
{
// 预加载脚本入口文件
entry: 'src/main/preload.ts',
vite: {
build: {
sourcemap: true,
outDir: 'dist-electron',
minify: false,
},
},
onstart() {},
},
{
// Sandboxed webview preload used only for browser annotations.
entry: 'src/main/browserAnnotationPreload.ts',
vite: {
build: {
sourcemap: true,
outDir: 'dist-electron',
minify: false,
},
},
onstart() {},
},
{
// Sandboxed preload used only by the isolated saved-credential login view.
entry: 'src/main/browserCredentials/agentBrowserCredentialPreload.ts',
vite: {
build: {
sourcemap: true,
outDir: 'dist-electron',
minify: false,
},
},
onstart() {},
},
{
// Sandboxed preload that observes manual login submissions without exposing secrets to pages.
entry: 'src/main/browserCredentials/manualCredentialCapturePreload.ts',
vite: {
build: {
sourcemap: true,
outDir: 'dist-electron',
minify: false,
},
},
onstart() {},
},
]),
renderer(),
],
base: process.env.NODE_ENV === 'development' ? '/' : './',
resolve: {
alias: {
'@shared': path.resolve(__dirname, './src/shared'),
'@': path.resolve(__dirname, './src/renderer'),
},
},
build: {
outDir: 'dist',
emptyOutDir: true,
sourcemap: true,
minify: false,
rollupOptions: {
// library-thumbnail.html is built by vite.thumbnail.config.ts so the
// sandboxed thumbnail page never shares chunks with the app entry.
input: {
main: path.resolve(__dirname, 'index.html'),
},
},
},
server: {
port: devPort,
strictPort: true,
host: true,
hmr: {
port: devPort,
},
watch: {
usePolling: false,
// Ignore vendor/ to prevent dev reload when plugins are installed into
// vendor/openclaw-runtime/.../third-party-extensions/
// Skip temporary trees (which may contain circular junctions) and Electron output.
ignored: [
'**/vendor/**',
'**/.work/**',
'**/artifacts/**',
'**/dist-electron/**',
],
},
},
optimizeDeps: {
exclude: ['electron', '@larksuite/openclaw-lark-tools', '@larksuite/openclaw-lark'],
esbuildOptions: {
define: {
__VERSION__: JSON.stringify(katexVersion),
},
},
},
clearScreen: false,
});