-
Notifications
You must be signed in to change notification settings - Fork 822
Expand file tree
/
Copy pathvite.config.ts
More file actions
161 lines (153 loc) · 4.76 KB
/
Copy pathvite.config.ts
File metadata and controls
161 lines (153 loc) · 4.76 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
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/
const devPort = 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,
},
},
},
},
onstart() {
// Signal that the main process bundle is ready for electron to load
fs.writeFileSync('dist-electron/.electron-ready', '');
},
},
{
// 预加载脚本入口文件
entry: 'src/main/preload.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,
},
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/
ignored: ['**/vendor/**'],
},
},
optimizeDeps: {
exclude: ['electron', '@larksuite/openclaw-lark-tools', '@larksuite/openclaw-lark'],
esbuildOptions: {
define: {
__VERSION__: JSON.stringify(katexVersion),
},
},
},
clearScreen: false,
});