Files
terminal-lab/wetty/vite.config.ts
2026-03-03 16:07:18 +08:00

82 lines
2.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fs from 'node:fs';
import path from 'node:path';
import { defineConfig } from 'vite';
const DEV_PUBLIC_HOST = 'shell.biboer.cn';
const DEV_CERT_PATH =
'/Users/gavin/.acme.sh/shell.biboer.cn_ecc/fullchain.cer';
const DEV_KEY_PATH =
'/Users/gavin/.acme.sh/shell.biboer.cn_ecc/shell.biboer.cn.key';
const TERMINAL_CONFIG_FILE = path.resolve(process.cwd(), 'terminal.config.json');
function readTLSFile(path: string): Buffer {
if (!fs.existsSync(path)) {
throw new Error(`开发证书文件不存在: ${path}`);
}
return fs.readFileSync(path);
}
export default defineConfig({
plugins: [
{
name: 'serve-terminal-config',
configureServer(server) {
// 开发态从仓库根目录实时读取 terminal.config.json避免手动复制到 public。
server.middlewares.use((req, res, next) => {
const reqPath = (req.url || '').split('?')[0];
if (reqPath !== '/terminal.config.json') {
next();
return;
}
if (!fs.existsSync(TERMINAL_CONFIG_FILE)) {
res.statusCode = 404;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.end(
JSON.stringify({
error: 'terminal.config.json 不存在',
path: TERMINAL_CONFIG_FILE,
}),
);
return;
}
const content = fs.readFileSync(TERMINAL_CONFIG_FILE, 'utf8');
res.statusCode = 200;
res.setHeader('Cache-Control', 'no-store');
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.end(content);
});
},
},
],
server: {
host: '0.0.0.0',
port: 5173,
strictPort: true,
allowedHosts: [DEV_PUBLIC_HOST],
https: {
cert: readTLSFile(DEV_CERT_PATH),
key: readTLSFile(DEV_KEY_PATH),
},
hmr: {
protocol: 'wss',
host: DEV_PUBLIC_HOST,
port: 5173,
},
proxy: {
// 浏览器通过 https://shell.biboer.cn:5173 访问时,这里将 wss 升级请求转发到本地网关。
'/ws/terminal': {
target: 'ws://127.0.0.1:8787',
ws: true,
changeOrigin: true,
},
'/ws/asr': {
target: 'ws://127.0.0.1:8787',
ws: true,
changeOrigin: true,
},
},
},
});