forked from MemeCalculate/moyin-creator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
120 lines (112 loc) · 4.57 KB
/
Copy pathvite.config.ts
File metadata and controls
120 lines (112 loc) · 4.57 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
import { defineConfig, type Plugin } from 'vite'
import path from 'node:path'
import electron from 'vite-plugin-electron/simple'
import react from '@vitejs/plugin-react'
/**
* Vite 插件:API CORS 代理
*
* 在开发服务器上注册 /__api_proxy 中间件,
* 将浏览器端的外部 API 请求由服务端转发,绕过 CORS 限制。
*
* 用法(前端):
* fetch('/__api_proxy?url=' + encodeURIComponent('https://example.com/api'))
*/
function apiCorsProxyPlugin(): Plugin {
return {
name: 'api-cors-proxy',
configureServer(server) {
server.middlewares.use('/__api_proxy', async (req, res) => {
// 处理 OPTIONS 预检请求
if (req.method === 'OPTIONS') {
res.writeHead(204, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': '*',
});
res.end();
return;
}
// 解析目标 URL
const urlParam = new URL(req.url || '', 'http://localhost').searchParams.get('url');
if (!urlParam) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Missing ?url= parameter' }));
return;
}
try {
// 读取请求体
const bodyChunks: Buffer[] = [];
for await (const chunk of req) {
bodyChunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk);
}
const body = bodyChunks.length > 0 ? Buffer.concat(bodyChunks) : undefined;
// 解包 x-proxy-headers 中的原始请求头
const proxyHeadersRaw = req.headers['x-proxy-headers'];
let forwardHeaders: Record<string, string> = {};
if (typeof proxyHeadersRaw === 'string') {
try {
forwardHeaders = JSON.parse(proxyHeadersRaw);
} catch { /* ignore parse errors */ }
}
// 服务端转发请求
const response = await fetch(urlParam, {
method: req.method || 'GET',
headers: forwardHeaders,
body: req.method !== 'GET' && req.method !== 'HEAD' ? body : undefined,
});
// 将远程响应转发回浏览器
const respBody = await response.arrayBuffer();
const headers: Record<string, string> = {
'Access-Control-Allow-Origin': '*',
};
// 转发 content-type
const ct = response.headers.get('content-type');
if (ct) headers['Content-Type'] = ct;
res.writeHead(response.status, headers);
res.end(Buffer.from(respBody));
} catch (err: any) {
console.error('[api-cors-proxy] Proxy error:', err?.message || err);
res.writeHead(502, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
});
res.end(JSON.stringify({ error: 'Proxy request failed', detail: err?.message }));
}
});
},
};
}
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'@opencut/ai-core/services/prompt-compiler': path.resolve(__dirname, './src/packages/ai-core/services/prompt-compiler.ts'),
'@opencut/ai-core/api/task-poller': path.resolve(__dirname, './src/packages/ai-core/api/task-poller.ts'),
'@opencut/ai-core/protocol': path.resolve(__dirname, './src/packages/ai-core/protocol/index.ts'),
'@opencut/ai-core': path.resolve(__dirname, './src/packages/ai-core/index.ts'),
},
},
plugins: [
apiCorsProxyPlugin(),
react(),
electron({
main: {
// Shortcut of `build.lib.entry`.
entry: 'electron/main.ts',
},
preload: {
// Shortcut of `build.rollupOptions.input`.
// Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`.
input: path.join(__dirname, 'electron/preload.ts'),
},
// Ployfill 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: process.env.NODE_ENV === 'test'
// https://github.com/electron-vite/vite-plugin-electron-renderer/issues/78#issuecomment-2053600808
? undefined
: {},
}),
],
})