/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import path from 'node:path'; import react from '@vitejs/plugin-react'; import pkg from '@douyinfe/vite-plugin-semi'; import { defineConfig, loadEnv, transformWithEsbuild } from 'vite'; // @douyinfe/vite-plugin-semi 是 CommonJS 包,具名导出挂在 default 上。 const { vitePluginSemi } = pkg; /** * 未配置代理变量时保留原有联调地址,兼容自定义 mode。 */ const DEV_PROXY_TARGET = 'http://134.175.227.134:13000'; /** * 允许在 .js 文件里直接书写 JSX,无需为纯 JSX 文件单独改名为 .jsx。 * * 该正则按路径子串匹配,因此 node_modules 下同样形如 src/*.js 的文件也会命中。 * 这是既有行为,保持不变;若要收紧为「只匹配项目 src 目录」,属于独立变更。 */ const JSX_IN_JS_RE = /src\/.*\.js$/; /** * 手动分包规则,数组顺序即匹配优先级:首个命中的分组拥有该模块。 * * 每条规则只负责把 node_modules 下的第三方库归入固定 chunk; * 未命中的模块返回 undefined,仍交回 Rollup 的默认分包策略。 */ const VENDOR_CHUNK_RULES = [ // React 运行时核心 ['react-core', ['react/', 'react-dom/', 'react-router-dom/', 'scheduler/']], // Semi UI:图标与组件分开,避免图标变动连带组件 chunk 失效 ['semi-icons', ['@douyinfe/semi-icons']], ['semi-ui', ['@douyinfe/semi-ui', '@douyinfe/semi-foundation']], // 图表库,体积较大 ['vchart', ['@visactor/']], // Mermaid 及其布局引擎,体积最大 ['mermaid', ['mermaid/', 'dagre-d3', 'cytoscape', 'elkjs']], // Markdown 渲染链路 ['markdown', ['react-markdown/', 'remark-', 'rehype-', 'unified/', 'unist-']], // KaTeX 数学公式 ['katex', ['katex/']], // 代码高亮 ['highlight', ['highlight.js/']], // 图标库 ['lobe-icons', ['@lobehub/icons']], ['lucide-icons', ['lucide-react/']], // 国际化 ['i18n', ['i18next', 'react-i18next/']], // 基础工具库 ['utils', ['axios/', 'dayjs/', 'clsx/']], // 独立的 React 生态第三方组件 [ 'react-components', [ 'react-dropzone/', 'react-toastify/', 'react-telegram-login/', 'react-turnstile/', ], ], // 二维码生成 ['qrcode', ['qrcode']], ]; /** * 解析模块应归属的第三方 chunk。 * * @param {string} id 模块的绝对路径 * @returns {string | undefined} chunk 名;无匹配时返回 undefined */ function resolveVendorChunk(id) { return VENDOR_CHUNK_RULES.find(([, packages]) => packages.some((name) => id.includes(`node_modules/${name}`)), )?.[0]; } /** * 让 JSX_IN_JS_RE 命中的 .js 按 JSX 语法解析。 * * 复用 Vite 暴露的 esbuild 转换能力,而不是自行直接调用 esbuild。 */ function jsxInJsPlugin() { return { name: 'treat-js-files-as-jsx', async transform(code, id) { if (!JSX_IN_JS_RE.test(id)) { return null; } return transformWithEsbuild(code, id, { loader: 'jsx', jsx: 'automatic', }); }, }; } /** * 向 index.html 注入统计脚本。 * * 使用当前 mode 的环境变量;启动终端或 CI 的同名变量优先。 * 未提供对应 ID 时不注入脚本。 */ function analyticsPlugin(env) { return { name: 'inject-analytics', transformIndexHtml(html) { let result = html; const umamiId = env.VITE_UMAMI_WEBSITE_ID; const umamiUrl = env.VITE_UMAMI_SCRIPT_URL || 'https://analytics.umami.is/script.js'; if (umamiId) { result = result.replace( '', ``, ); } const gaId = env.VITE_GOOGLE_ANALYTICS_ID; if (gaId) { result = result.replace( '', `` + ``, ); } return result; }, }; } // https://vitejs.dev/config/ export default defineConfig(({ mode }) => { const env = loadEnv(mode, __dirname, ['VITE_', 'DEV_PROXY_']); const proxyTarget = env.DEV_PROXY_TARGET || DEV_PROXY_TARGET; return { resolve: { alias: { '@': path.resolve(__dirname, './src'), '~': path.resolve(__dirname, 'node_modules'), '@douyinfe/semi-theme-default': path.resolve( __dirname, 'node_modules/@douyinfe/semi-theme-default', ), }, }, plugins: [ jsxInJsPlugin(), react(), vitePluginSemi({ cssLayer: true, }), analyticsPlugin(env), ], optimizeDeps: { // 强制重新预构建,避免依赖变更后命中陈旧的依赖缓存 force: true, esbuildOptions: { loader: { '.js': 'jsx', '.json': 'json', }, }, }, build: { // 使用 esbuild 压缩:速度远快于 terser,压缩率略低 minify: 'esbuild', // 启用 CSS 代码分割 cssCodeSplit: true, // chunk 体积告警阈值 (KB) chunkSizeWarningLimit: 1000, // 构建目标为 es2015,以换取更广的浏览器兼容性 target: 'es2015', // 生产环境不需要 sourcemap sourcemap: false, // esbuild 压缩选项:移除调试语句与版权注释 esbuild: { drop: ['console', 'debugger'], legalComments: 'none', }, rollupOptions: { output: { // 文件名带内容 hash,便于浏览器长期缓存与增量更新 chunkFileNames: 'assets/[name]-[hash].js', entryFileNames: 'assets/[name]-[hash].js', assetFileNames: 'assets/[name]-[hash].[ext]', manualChunks: resolveVendorChunk, }, }, }, server: { // 监听所有网卡,便于局域网与容器内访问 host: '0.0.0.0', proxy: { // 以下前缀统一转发到同一个后端,其中 /v1 额外开启 WebSocket '/api': { target: proxyTarget, changeOrigin: true }, '/v1': { target: proxyTarget, changeOrigin: true, ws: true }, '/v1beta': { target: proxyTarget, changeOrigin: true }, '/mj': { target: proxyTarget, changeOrigin: true }, '/pg': { target: proxyTarget, changeOrigin: true }, '/dashboard': { target: proxyTarget, changeOrigin: true }, '/kling': { target: proxyTarget, changeOrigin: true }, '/jimeng': { target: proxyTarget, changeOrigin: true }, '/suno': { target: proxyTarget, changeOrigin: true }, }, }, }; });