mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +08:00
chore(web): add Web WASM development workflow
This commit is contained in:
1 parent
8ca8b4f623
commit
73c6de364e
17 files changed
+74
-42
No files matched your search
@@ -11,7 +11,7 @@
|
||||
"files": [
|
||||
"bin/",
|
||||
"dist/",
|
||||
"dist-web/",
|
||||
"dist-cli-web/",
|
||||
"LICENSE"
|
||||
],
|
||||
"engines": {
|
||||
@@ -36,8 +36,8 @@
|
||||
"scripts": {
|
||||
"cli": "node scripts/ensure-native.mjs && tsx src/cli.ts",
|
||||
"build": "tsup",
|
||||
"build:full": "pnpm -w run build:web && pnpm run build && pnpm run prepare:web",
|
||||
"prepare:web": "rm -rf dist-web && cp -r ../../dist-web dist-web",
|
||||
"build:full": "pnpm -w run build:cli-web && pnpm run build && pnpm run prepare:cli-web",
|
||||
"prepare:cli-web": "rm -rf dist-cli-web && cp -r ../../dist-cli-web dist-cli-web",
|
||||
"ensure-native": "node scripts/ensure-native.mjs",
|
||||
"rebuild-native": "bash scripts/rebuild-native.sh",
|
||||
"rebuild-sqlite": "npm rebuild better-sqlite3",
|
||||
|
||||
+2
-2
@@ -140,11 +140,11 @@ program
|
||||
|
||||
let webRoot: string | undefined
|
||||
if (!options.headless) {
|
||||
const webDir = resolveCliPath('dist-web')
|
||||
const webDir = resolveCliPath('dist-cli-web')
|
||||
if (fs.existsSync(webDir)) {
|
||||
webRoot = webDir
|
||||
} else {
|
||||
console.warn('Warning: dist-web/ not found, starting in API-only mode')
|
||||
console.warn('Warning: dist-cli-web/ not found, starting in API-only mode')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ export interface HttpServerOptions {
|
||||
port?: number
|
||||
host?: string
|
||||
token?: string
|
||||
/** dist-web/ 目录路径,启用后托管 Web SPA 静态资源 */
|
||||
/** dist-cli-web/ 目录路径,启用后托管 CLI Web SPA 静态资源 */
|
||||
webRoot?: string
|
||||
/** When true, /_web/* also requires Bearer token (for server/headless deployments) */
|
||||
requireAuth?: boolean
|
||||
@@ -202,7 +202,7 @@ export async function startHttpServer(options?: HttpServerOptions): Promise<{
|
||||
|
||||
if (options?.webRoot && fs.existsSync(options.webRoot)) {
|
||||
// 注册反向代理:将 /_proxy/chatlab.fun/* 转发至 https://chatlab.fun,
|
||||
// 行为与 vite dev proxy 一致,解决浏览器 CORS 问题(见 vite.web.config.mts:138-144)。
|
||||
// 行为与 vite dev proxy 一致,解决浏览器 CORS 问题(见 vite.cli-web.config.mts)。
|
||||
// 必须在 @fastify/static 之前注册,确保显式路由优先于静态文件/SPA fallback。
|
||||
registerProxyRoutes(server)
|
||||
const fastifyStatic = await import('@fastify/static')
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* 反向代理路由 — /_proxy/chatlab.fun/*
|
||||
*
|
||||
* 在 Web 生产模式(chatlab start)下,将 /_proxy/chatlab.fun/* 代理到
|
||||
* https://chatlab.fun,行为与 vite.web.config.mts 中的 dev proxy 保持一致,
|
||||
* https://chatlab.fun,行为与 vite.cli-web.config.mts 中的 dev proxy 保持一致,
|
||||
* 以解决浏览器 CORS 限制。
|
||||
*
|
||||
* 目标 host 硬编码为 https://chatlab.fun,不允许外部指定,避免开放代理风险。
|
||||
@@ -15,12 +15,8 @@ const PROXY_TARGET = 'https://chatlab.fun'
|
||||
export function registerProxyRoutes(server: FastifyInstance): void {
|
||||
server.get<{ Params: { '*': string } }>('/_proxy/chatlab.fun/*', async (request, reply) => {
|
||||
const subPath = request.params['*'] ?? ''
|
||||
const queryString = new URLSearchParams(
|
||||
request.query as Record<string, string>,
|
||||
).toString()
|
||||
const targetUrl = queryString
|
||||
? `${PROXY_TARGET}/${subPath}?${queryString}`
|
||||
: `${PROXY_TARGET}/${subPath}`
|
||||
const queryString = new URLSearchParams(request.query as Record<string, string>).toString()
|
||||
const targetUrl = queryString ? `${PROXY_TARGET}/${subPath}?${queryString}` : `${PROXY_TARGET}/${subPath}`
|
||||
|
||||
try {
|
||||
const upstream = await fetch(targetUrl, {
|
||||
|
||||
Reference in new issue
Block a user