f9c5321ac1
This reverts commit dd85c25997.
249 lines
7.3 KiB
JavaScript
249 lines
7.3 KiB
JavaScript
/*
|
|
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 <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import react from '@vitejs/plugin-react';
|
|
import { defineConfig, transformWithEsbuild } from 'vite';
|
|
import pkg from '@douyinfe/vite-plugin-semi';
|
|
import path from 'path';
|
|
const { vitePluginSemi } = pkg;
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Use the exposed transform from vite, instead of directly
|
|
// transforming with esbuild
|
|
return transformWithEsbuild(code, id, {
|
|
loader: 'jsx',
|
|
jsx: 'automatic',
|
|
});
|
|
},
|
|
},
|
|
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(
|
|
'<!--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: {
|
|
force: true,
|
|
esbuildOptions: {
|
|
loader: {
|
|
'.js': 'jsx',
|
|
'.json': 'json',
|
|
},
|
|
},
|
|
},
|
|
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';
|
|
}
|
|
},
|
|
},
|
|
},
|
|
},
|
|
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,
|
|
},
|
|
},
|
|
},
|
|
});
|