From e395b6e4ed48767b467a7775f9914465bcae1f8f Mon Sep 17 00:00:00 2001
From: zhanjunhao <642307404@qq.com>
Date: Sat, 17 Oct 2026 19:11:47 +0800
Subject: [PATCH] =?UTF-8?q?refactor(vite):=20=E9=9B=86=E4=B8=AD=E7=AE=A1?=
=?UTF-8?q?=E7=90=86=E5=88=86=E5=8C=85=E8=A7=84=E5=88=99=E5=B9=B6=E5=88=87?=
=?UTF-8?q?=E6=8D=A2=E5=BC=80=E5=8F=91=E4=BB=A3=E7=90=86=E5=9C=B0=E5=9D=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
提取 JSX 转换和统计脚本插件,保持原有分包匹配顺序。
统一开发代理目标为 http://134.175.227.134:13000,保留 /v1 WebSocket 支持。
---
vite.config.js | 323 +++++++++++++++++++++++--------------------------
1 file changed, 151 insertions(+), 172 deletions(-)
diff --git a/vite.config.js b/vite.config.js
index 4b2b51b3..1ea814b8 100644
--- a/vite.config.js
+++ b/vite.config.js
@@ -17,12 +17,141 @@ 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 { defineConfig, transformWithEsbuild } from 'vite';
import pkg from '@douyinfe/vite-plugin-semi';
-import path from 'path';
+import { defineConfig, transformWithEsbuild } from 'vite';
+
+// @douyinfe/vite-plugin-semi 是 CommonJS 包,具名导出挂在 default 上。
const { vitePluginSemi } = pkg;
+/**
+ * 开发服务器代理统一指向的后端根地址。
+ * 切换联调环境时只需改这一处,不必逐个前缀修改。
+ */
+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 注入统计脚本。
+ *
+ * 变量在 transformIndexHtml 执行时才读取,以保持原有的生效时机:
+ * 由启动终端或 CI 注入进程环境变量,未提供对应 ID 时不做任何替换。
+ */
+function analyticsPlugin() {
+ return {
+ 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;
+ },
+ };
+}
+
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
@@ -36,52 +165,15 @@ export default defineConfig({
},
},
plugins: [
- {
- name: 'treat-js-files-as-jsx',
- async transform(code, id) {
- if (!/src\/.*\.js$/.test(id)) {
- return null;
- }
-
- // Use the exposed transform from vite, instead of directly
- // transforming with esbuild
- return transformWithEsbuild(code, id, {
- loader: 'jsx',
- jsx: 'automatic',
- });
- },
- },
+ jsxInJsPlugin(),
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;
- },
- },
+ analyticsPlugin(),
],
optimizeDeps: {
+ // 强制重新预构建,避免依赖变更后命中陈旧的依赖缓存
force: true,
esbuildOptions: {
loader: {
@@ -91,158 +183,45 @@ export default defineConfig({
},
},
build: {
- // 使用 esbuild 压缩,速度更快且体积更小
+ // 使用 esbuild 压缩:速度远快于 terser,压缩率略低
minify: 'esbuild',
// 启用 CSS 代码分割
cssCodeSplit: true,
- // 设置 chunk 大小警告阈值 (KB)
+ // chunk 体积告警阈值 (KB)
chunkSizeWarningLimit: 1000,
- // Target 现代浏览器以获得更小的打包体积
+ // 构建目标为 es2015,以换取更广的浏览器兼容性
target: 'es2015',
// 生产环境不需要 sourcemap
sourcemap: false,
- // esbuild 压缩选项
+ // esbuild 压缩选项:移除调试语句与版权注释
esbuild: {
drop: ['console', 'debugger'],
legalComments: 'none',
},
rollupOptions: {
output: {
- // 优化 chunk 命名以支持更好的缓存
+ // 文件名带内容 hash,便于浏览器长期缓存与增量更新
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';
- }
- },
+ manualChunks: resolveVendorChunk,
},
},
},
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,
- },
+ // 以下前缀统一转发到同一个后端,其中 /v1 额外开启 WebSocket
+ '/api': { target: DEV_PROXY_TARGET, changeOrigin: true },
+ '/v1': { target: DEV_PROXY_TARGET, changeOrigin: true, ws: true },
+ '/v1beta': { target: DEV_PROXY_TARGET, changeOrigin: true },
+ '/mj': { target: DEV_PROXY_TARGET, changeOrigin: true },
+ '/pg': { target: DEV_PROXY_TARGET, changeOrigin: true },
+ '/dashboard': { target: DEV_PROXY_TARGET, changeOrigin: true },
+ '/kling': { target: DEV_PROXY_TARGET, changeOrigin: true },
+ '/jimeng': { target: DEV_PROXY_TARGET, changeOrigin: true },
+ '/suno': { target: DEV_PROXY_TARGET, changeOrigin: true },
},
},
});