refactor(vite): 集中管理分包规则并切换开发代理地址
提取 JSX 转换和统计脚本插件,保持原有分包匹配顺序。 统一开发代理目标为 http://134.175.227.134:13000,保留 /v1 WebSocket 支持。
This commit is contained in:
+151
-172
@@ -17,12 +17,141 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
For commercial licensing, please contact support@quantumnous.com
|
For commercial licensing, please contact support@quantumnous.com
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import path from 'node:path';
|
||||||
import react from '@vitejs/plugin-react';
|
import react from '@vitejs/plugin-react';
|
||||||
import { defineConfig, transformWithEsbuild } from 'vite';
|
|
||||||
import pkg from '@douyinfe/vite-plugin-semi';
|
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 { 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(
|
||||||
|
'<!--umami-->',
|
||||||
|
`<script defer src="${umamiUrl}" data-website-id="${umamiId}"></script>`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const gaId = process.env.VITE_GOOGLE_ANALYTICS_ID;
|
||||||
|
if (gaId) {
|
||||||
|
result = result.replace(
|
||||||
|
'<!--Google Analytics-->',
|
||||||
|
`<script async src="https://www.googletagmanager.com/gtag/js?id=${gaId}"></script>` +
|
||||||
|
`<script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag('js',new Date());gtag('config','${gaId}');</script>`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// https://vitejs.dev/config/
|
// https://vitejs.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
resolve: {
|
resolve: {
|
||||||
@@ -36,52 +165,15 @@ export default defineConfig({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
{
|
jsxInJsPlugin(),
|
||||||
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',
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
|
||||||
react(),
|
react(),
|
||||||
vitePluginSemi({
|
vitePluginSemi({
|
||||||
cssLayer: true,
|
cssLayer: true,
|
||||||
}),
|
}),
|
||||||
{
|
analyticsPlugin(),
|
||||||
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(
|
|
||||||
'<!--umami-->',
|
|
||||||
`<script defer src="${umamiUrl}" data-website-id="${umamiId}"></script>`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const gaId = process.env.VITE_GOOGLE_ANALYTICS_ID;
|
|
||||||
if (gaId) {
|
|
||||||
result = result.replace(
|
|
||||||
'<!--Google Analytics-->',
|
|
||||||
`<script async src="https://www.googletagmanager.com/gtag/js?id=${gaId}"></script>` +
|
|
||||||
`<script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag('js',new Date());gtag('config','${gaId}');</script>`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
optimizeDeps: {
|
optimizeDeps: {
|
||||||
|
// 强制重新预构建,避免依赖变更后命中陈旧的依赖缓存
|
||||||
force: true,
|
force: true,
|
||||||
esbuildOptions: {
|
esbuildOptions: {
|
||||||
loader: {
|
loader: {
|
||||||
@@ -91,158 +183,45 @@ export default defineConfig({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
build: {
|
build: {
|
||||||
// 使用 esbuild 压缩,速度更快且体积更小
|
// 使用 esbuild 压缩:速度远快于 terser,压缩率略低
|
||||||
minify: 'esbuild',
|
minify: 'esbuild',
|
||||||
// 启用 CSS 代码分割
|
// 启用 CSS 代码分割
|
||||||
cssCodeSplit: true,
|
cssCodeSplit: true,
|
||||||
// 设置 chunk 大小警告阈值 (KB)
|
// chunk 体积告警阈值 (KB)
|
||||||
chunkSizeWarningLimit: 1000,
|
chunkSizeWarningLimit: 1000,
|
||||||
// Target 现代浏览器以获得更小的打包体积
|
// 构建目标为 es2015,以换取更广的浏览器兼容性
|
||||||
target: 'es2015',
|
target: 'es2015',
|
||||||
// 生产环境不需要 sourcemap
|
// 生产环境不需要 sourcemap
|
||||||
sourcemap: false,
|
sourcemap: false,
|
||||||
// esbuild 压缩选项
|
// esbuild 压缩选项:移除调试语句与版权注释
|
||||||
esbuild: {
|
esbuild: {
|
||||||
drop: ['console', 'debugger'],
|
drop: ['console', 'debugger'],
|
||||||
legalComments: 'none',
|
legalComments: 'none',
|
||||||
},
|
},
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
output: {
|
output: {
|
||||||
// 优化 chunk 命名以支持更好的缓存
|
// 文件名带内容 hash,便于浏览器长期缓存与增量更新
|
||||||
chunkFileNames: 'assets/[name]-[hash].js',
|
chunkFileNames: 'assets/[name]-[hash].js',
|
||||||
entryFileNames: 'assets/[name]-[hash].js',
|
entryFileNames: 'assets/[name]-[hash].js',
|
||||||
assetFileNames: 'assets/[name]-[hash].[ext]',
|
assetFileNames: 'assets/[name]-[hash].[ext]',
|
||||||
manualChunks: (id) => {
|
manualChunks: resolveVendorChunk,
|
||||||
// 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';
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
server: {
|
server: {
|
||||||
|
// 监听所有网卡,便于局域网与容器内访问
|
||||||
host: '0.0.0.0',
|
host: '0.0.0.0',
|
||||||
proxy: {
|
proxy: {
|
||||||
'/api': {
|
// 以下前缀统一转发到同一个后端,其中 /v1 额外开启 WebSocket
|
||||||
target: 'http://localhost:3000',
|
'/api': { target: DEV_PROXY_TARGET, changeOrigin: true },
|
||||||
changeOrigin: true,
|
'/v1': { target: DEV_PROXY_TARGET, changeOrigin: true, ws: true },
|
||||||
},
|
'/v1beta': { target: DEV_PROXY_TARGET, changeOrigin: true },
|
||||||
'/v1': {
|
'/mj': { target: DEV_PROXY_TARGET, changeOrigin: true },
|
||||||
target: 'http://localhost:3000',
|
'/pg': { target: DEV_PROXY_TARGET, changeOrigin: true },
|
||||||
changeOrigin: true,
|
'/dashboard': { target: DEV_PROXY_TARGET, changeOrigin: true },
|
||||||
ws: true,
|
'/kling': { target: DEV_PROXY_TARGET, changeOrigin: true },
|
||||||
},
|
'/jimeng': { target: DEV_PROXY_TARGET, changeOrigin: true },
|
||||||
'/v1beta': {
|
'/suno': { target: DEV_PROXY_TARGET, changeOrigin: true },
|
||||||
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,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user