From dd85c259975e09abded33aff7f432e7cd948677f Mon Sep 17 00:00:00 2001 From: zhanjunhao <642307404@qq.com> Date: Sat, 17 Oct 2026 16:10:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BB=A3=E7=90=86=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E3=80=81=E5=88=86=E5=8C=85=E8=A7=84=E5=88=99?= =?UTF-8?q?=E9=9B=86=E4=B8=AD=E9=85=8D=E7=BD=AE=E3=80=81=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=9B=B4=E8=A7=84=E8=8C=83=E3=80=81=E5=A4=8D?= =?UTF-8?q?=E7=94=A8=E4=BE=9D=E8=B5=96=E7=BC=93=E5=AD=98=E3=80=81=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E6=AD=A3=E5=B8=B8=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 4 + README.md | 14 +- vite.config.js | 366 ++++++++++++++++++++----------------------------- 3 files changed, 166 insertions(+), 218 deletions(-) diff --git a/.env.example b/.env.example index e745e919..28015e49 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,7 @@ # Frontend build-time feature flags. Rebuild the frontend after changing them. # Missing or false keeps the Cloud Brain suite hidden and blocks direct routes. VITE_CLOUD_BRAIN_UI_ENABLED=false + +# All Vite development proxy paths share this backend target. +# Override in .env.local and restart the development server. +DEV_PROXY_TARGET=http://134.175.227.134:13000 diff --git a/README.md b/README.md index 11c562c6..9a311856 100644 --- a/README.md +++ b/README.md @@ -71,13 +71,19 @@ window.__APP_CONFIG__ = { }; ``` -[vite.config.js](./vite.config.js) 将下列前缀转发到 `http://localhost:3000`: +[vite.config.js](./vite.config.js) 将下列前缀转发到 `DEV_PROXY_TARGET` 指定的后端,未配置时默认为 `http://localhost:3000`: ```text /api /v1 /v1beta /mj /pg /dashboard /kling /jimeng /suno ``` -其中 `/v1` 已启用 WebSocket 代理。连接远程测试后端时,修改所需代理项的 `target`,然后重启开发服务;代理目标目前是写在配置文件中的,**没有读取专用的代理环境变量**。`package.json` 中的 `proxy` 字段不是这里的生效入口。 +其中 `/v1` 已启用 WebSocket 代理。连接远程测试后端时,在 `.env.local` 中设置统一的后端根地址,然后重启开发服务: + +```dotenv +DEV_PROXY_TARGET=https://api.example.com +``` + +不需要逐项修改代理配置,接口路径会原样转发,不需要给地址额外加 `/api`。该变量用于 Vite 代理;浏览器直连独立后端仍使用下节的配置。`package.json` 中的 `proxy` 字段不是这里的生效入口。 ### 可选:浏览器直接访问独立后端域名 @@ -111,11 +117,11 @@ VITE_CLOUD_BRAIN_UI_ENABLED=false | 配置 | 作用 | 生效方式 | | --- | --- | --- | | `VITE_CLOUD_BRAIN_UI_ENABLED` | 云脑套件入口与路由开关,默认关闭 | 构建时;修改后重启开发服务或重新构建 | -| `VITE_UMAMI_WEBSITE_ID` | 启用 Umami 统计脚本注入 | Vite 配置读取进程环境变量 | +| `VITE_UMAMI_WEBSITE_ID` | 启用 Umami 统计脚本注入 | Vite 配置通过 `loadEnv` 读取当前模式的环境文件及进程环境变量 | | `VITE_UMAMI_SCRIPT_URL` | Umami 脚本地址 | 同上,默认 `https://analytics.umami.is/script.js` | | `VITE_GOOGLE_ANALYTICS_ID` | 启用 Google Analytics 脚本注入 | 同上 | -统计脚本插件使用 `process.env`,没有显式调用 Vite `loadEnv`;如果使用 Node/npm 运行 Vite,不要假定只写 `.env.local` 就能让配置文件读到这些统计变量,应由启动终端或 CI 环境注入。 +统计脚本变量支持写入 `.env.local` 或对应模式的环境文件,也支持由启动终端或 CI 注入;同名进程环境变量优先。修改后需重启开发服务或重新构建。 前端环境变量和 `config.js` 都会暴露给浏览器,仅放公开配置,不放上游模型密钥或后端机密。 diff --git a/vite.config.js b/vite.config.js index 4b2b51b3..801ca0a4 100644 --- a/vite.config.js +++ b/vite.config.js @@ -17,232 +17,170 @@ along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import react from '@vitejs/plugin-react'; -import { defineConfig, transformWithEsbuild } from 'vite'; +import { + defineConfig, + loadEnv, + normalizePath, + transformWithEsbuild, +} from 'vite'; import pkg from '@douyinfe/vite-plugin-semi'; -import path from 'path'; + const { vitePluginSemi } = pkg; +const projectRoot = path.dirname(fileURLToPath(import.meta.url)); +const sourceRoot = `${normalizePath(path.resolve(projectRoot, 'src'))}/`; -// https://vitejs.dev/config/ -export default defineConfig({ - 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: [ - { - name: 'treat-js-files-as-jsx', - async transform(code, id) { - if (!/src\/.*\.js$/.test(id)) { - return null; - } +const DEFAULT_PROXY_TARGET = 'http://localhost:3000'; +const PROXY_PREFIXES = [ + '/api', + '/v1', + '/v1beta', + '/mj', + '/pg', + '/dashboard', + '/kling', + '/jimeng', + '/suno', +]; - // Use the exposed transform from vite, instead of directly - // transforming with esbuild - return transformWithEsbuild(code, id, { - loader: 'jsx', - jsx: 'automatic', - }); +function createProxy(target) { + return Object.fromEntries( + PROXY_PREFIXES.map((prefix) => [ + prefix, + { + target, + changeOrigin: true, + ...(prefix === '/v1' ? { ws: true } : {}), }, - }, - react(), - vitePluginSemi({ - cssLayer: true, - }), - { - name: 'inject-analytics', - transformIndexHtml(html) { - let result = html; - const umamiId = process.env.VITE_UMAMI_WEBSITE_ID; - const umamiUrl = - process.env.VITE_UMAMI_SCRIPT_URL || - 'https://analytics.umami.is/script.js'; - if (umamiId) { - result = result.replace( - '', - ``, - ); - } - const gaId = process.env.VITE_GOOGLE_ANALYTICS_ID; - if (gaId) { - result = result.replace( - '', - `` + - ``, - ); - } - return result; - }, - }, + ]), + ); +} + +// Keep matching order: the first matching group owns the module. +const VENDOR_CHUNKS = [ + ['react-core', ['react/', 'react-dom/', 'react-router-dom/', 'scheduler/']], + ['semi-icons', ['@douyinfe/semi-icons']], + ['semi-ui', ['@douyinfe/semi-ui', '@douyinfe/semi-foundation']], + ['vchart', ['@visactor/']], + ['mermaid', ['mermaid/', 'dagre-d3', 'cytoscape', 'elkjs']], + ['markdown', ['react-markdown/', 'remark-', 'rehype-', 'unified/', 'unist-']], + ['katex', ['katex/']], + ['highlight', ['highlight.js/']], + ['lobe-icons', ['@lobehub/icons']], + ['lucide-icons', ['lucide-react/']], + ['i18n', ['i18next', 'react-i18next/']], + ['utils', ['axios/', 'dayjs/', 'clsx/']], + [ + 'react-components', + [ + 'react-dropzone/', + 'react-toastify/', + 'react-telegram-login/', + 'react-turnstile/', + ], ], - optimizeDeps: { - force: true, - esbuildOptions: { - loader: { - '.js': 'jsx', - '.json': 'json', + ['qrcode', ['qrcode']], +]; + +function manualChunks(id) { + const normalizedId = normalizePath(id); + return VENDOR_CHUNKS.find(([, packages]) => + packages.some((name) => normalizedId.includes(`node_modules/${name}`)), + )?.[0]; +} + +function legacyJsxPlugin() { + return { + name: 'treat-js-files-as-jsx', + async transform(code, id) { + const file = normalizePath(id).split('?')[0]; + if (!file.startsWith(sourceRoot) || !file.endsWith('.js')) return null; + + return transformWithEsbuild(code, id, { + loader: 'jsx', + jsx: 'automatic', + }); + }, + }; +} + +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; + }, + }; +} + +export default defineConfig(({ mode }) => { + // Load only the settings used here; DEV_PROXY_TARGET stays server-side. + const env = loadEnv(mode, projectRoot, ['VITE_', 'DEV_PROXY_TARGET']); + const proxyTarget = env.DEV_PROXY_TARGET?.trim() || DEFAULT_PROXY_TARGET; + + return { + resolve: { + alias: { + '@': path.resolve(projectRoot, 'src'), + '~': path.resolve(projectRoot, 'node_modules'), + '@douyinfe/semi-theme-default': path.resolve( + projectRoot, + 'node_modules/@douyinfe/semi-theme-default', + ), }, }, - }, - build: { - // 使用 esbuild 压缩,速度更快且体积更小 - minify: 'esbuild', - // 启用 CSS 代码分割 - cssCodeSplit: true, - // 设置 chunk 大小警告阈值 (KB) - chunkSizeWarningLimit: 1000, - // Target 现代浏览器以获得更小的打包体积 - target: 'es2015', - // 生产环境不需要 sourcemap - sourcemap: false, - // esbuild 压缩选项 - esbuild: { - drop: ['console', 'debugger'], - legalComments: 'none', - }, - rollupOptions: { - output: { - // 优化 chunk 命名以支持更好的缓存 - chunkFileNames: 'assets/[name]-[hash].js', - entryFileNames: 'assets/[name]-[hash].js', - assetFileNames: 'assets/[name]-[hash].[ext]', - manualChunks: (id) => { - // React 核心库 - if ( - id.includes('node_modules/react/') || - id.includes('node_modules/react-dom/') || - id.includes('node_modules/react-router-dom/') || - id.includes('node_modules/scheduler/') - ) { - return 'react-core'; - } - // Semi UI - 拆分为图标和组件 - if (id.includes('node_modules/@douyinfe/semi-icons')) { - return 'semi-icons'; - } - if ( - id.includes('node_modules/@douyinfe/semi-ui') || - id.includes('node_modules/@douyinfe/semi-foundation') - ) { - return 'semi-ui'; - } - // 图表库单独分离(较大) - if (id.includes('node_modules/@visactor/')) { - return 'vchart'; - } - // Mermaid 图表库(非常大,单独分离) - if ( - id.includes('node_modules/mermaid/') || - id.includes('node_modules/dagre-d3') || - id.includes('node_modules/cytoscape') || - id.includes('node_modules/elkjs') - ) { - return 'mermaid'; - } - // Markdown 相关 - if ( - id.includes('node_modules/react-markdown/') || - id.includes('node_modules/remark-') || - id.includes('node_modules/rehype-') || - id.includes('node_modules/unified/') || - id.includes('node_modules/unist-') - ) { - return 'markdown'; - } - // KaTeX 数学公式渲染 - if (id.includes('node_modules/katex/')) { - return 'katex'; - } - // 代码高亮 - if (id.includes('node_modules/highlight.js/')) { - return 'highlight'; - } - // LobeHub 图标库 - if (id.includes('node_modules/@lobehub/icons')) { - return 'lobe-icons'; - } - // Lucide 图标库 - if (id.includes('node_modules/lucide-react/')) { - return 'lucide-icons'; - } - // i18n 国际化 - if ( - id.includes('node_modules/i18next') || - id.includes('node_modules/react-i18next/') - ) { - return 'i18n'; - } - // 工具库 - if ( - id.includes('node_modules/axios/') || - id.includes('node_modules/dayjs/') || - id.includes('node_modules/clsx/') - ) { - return 'utils'; - } - // React 第三方组件 - if ( - id.includes('node_modules/react-dropzone/') || - id.includes('node_modules/react-toastify/') || - id.includes('node_modules/react-telegram-login/') || - id.includes('node_modules/react-turnstile/') - ) { - return 'react-components'; - } - // QRCode 库 - if (id.includes('node_modules/qrcode')) { - return 'qrcode'; - } + plugins: [ + legacyJsxPlugin(), + react(), + vitePluginSemi({ cssLayer: true }), + analyticsPlugin(env), + ], + optimizeDeps: { + esbuildOptions: { + loader: { + '.js': 'jsx', + '.json': 'json', }, }, }, - }, - server: { - host: '0.0.0.0', - proxy: { - '/api': { - target: 'http://localhost:3000', - changeOrigin: true, - }, - '/v1': { - target: 'http://localhost:3000', - changeOrigin: true, - ws: true, - }, - '/v1beta': { - target: 'http://localhost:3000', - changeOrigin: true, - }, - '/mj': { - target: 'http://localhost:3000', - changeOrigin: true, - }, - '/pg': { - target: 'http://localhost:3000', - changeOrigin: true, - }, - '/dashboard': { - target: 'http://localhost:3000', - changeOrigin: true, - }, - '/kling': { - target: 'http://localhost:3000', - changeOrigin: true, - }, - '/jimeng': { - target: 'http://localhost:3000', - changeOrigin: true, - }, - '/suno': { - target: 'http://localhost:3000', - changeOrigin: true, + build: { + minify: 'esbuild', + cssCodeSplit: true, + chunkSizeWarningLimit: 1000, + target: 'es2015', + sourcemap: false, + rollupOptions: { + output: { + chunkFileNames: 'assets/[name]-[hash].js', + entryFileNames: 'assets/[name]-[hash].js', + assetFileNames: 'assets/[name]-[hash].[ext]', + manualChunks, + }, }, }, - }, + server: { + host: '0.0.0.0', + proxy: createProxy(proxyTarget), + }, + }; });