Files
WebDrop/web/vite.config.ts

42 lines
1.9 KiB
TypeScript

import { execFileSync } from 'node:child_process';
import http from 'node:http';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vitest/config';
const webRoot = fileURLToPath(new URL('.', import.meta.url));
const webPackage = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8')) as { version: string };
function gitValue(...args: string[]): string {
try {
return execFileSync('git', args, { cwd: webRoot, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim() || 'unknown';
} catch {
return 'unknown';
}
}
const buildInfo = {
frontendVersion: webPackage.version,
frontendCommit: process.env.WEBDROP_FRONTEND_COMMIT ?? gitValue('rev-parse', 'HEAD'),
frontendTag: process.env.WEBDROP_FRONTEND_TAG ?? gitValue('describe', '--tags', '--exact-match', 'HEAD'),
backendCommit: process.env.WEBDROP_BACKEND_COMMIT ?? gitValue('rev-parse', 'HEAD'),
backendTag: process.env.WEBDROP_BACKEND_TAG ?? gitValue('describe', '--tags', '--exact-match', 'HEAD'),
builtAt: process.env.WEBDROP_BUILD_TIME ?? new Date().toISOString(),
};
export default defineConfig({
plugins: [react()],
define: { __WEBDROP_BUILD_INFO__: JSON.stringify(buildInfo) },
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'@webdrop/core': fileURLToPath(new URL('../packages/core/src/index.ts', import.meta.url)),
},
},
build: { target: 'es2022', sourcemap: false },
// 开发代理复用到后端的连接:默认每个请求新开一条 TCP,上传 1 MiB 的帧会被连接建立和慢启动拖得很慢
server: { proxy: { '/api/v1': { target: process.env.WEBDROP_DEV_API_TARGET ?? 'http://127.0.0.1:5000', ws: true, agent: new http.Agent({ keepAlive: true, maxSockets: 64 }) } } },
test: { environment: 'node' },
});