Revert "feat: 代理统一管理、分包规则集中配置、路径处理更规范、复用依赖缓存、环境变量正常加载"
This reverts commit dd85c25997.
This commit is contained in:
@@ -1,7 +1,3 @@
|
||||
# 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
|
||||
|
||||
@@ -71,19 +71,13 @@ window.__APP_CONFIG__ = {
|
||||
};
|
||||
```
|
||||
|
||||
[vite.config.js](./vite.config.js) 将下列前缀转发到 `DEV_PROXY_TARGET` 指定的后端,未配置时默认为 `http://localhost:3000`:
|
||||
[vite.config.js](./vite.config.js) 将下列前缀转发到 `http://localhost:3000`:
|
||||
|
||||
```text
|
||||
/api /v1 /v1beta /mj /pg /dashboard /kling /jimeng /suno
|
||||
```
|
||||
|
||||
其中 `/v1` 已启用 WebSocket 代理。连接远程测试后端时,在 `.env.local` 中设置统一的后端根地址,然后重启开发服务:
|
||||
|
||||
```dotenv
|
||||
DEV_PROXY_TARGET=https://api.example.com
|
||||
```
|
||||
|
||||
不需要逐项修改代理配置,接口路径会原样转发,不需要给地址额外加 `/api`。该变量用于 Vite 代理;浏览器直连独立后端仍使用下节的配置。`package.json` 中的 `proxy` 字段不是这里的生效入口。
|
||||
其中 `/v1` 已启用 WebSocket 代理。连接远程测试后端时,修改所需代理项的 `target`,然后重启开发服务;代理目标目前是写在配置文件中的,**没有读取专用的代理环境变量**。`package.json` 中的 `proxy` 字段不是这里的生效入口。
|
||||
|
||||
### 可选:浏览器直接访问独立后端域名
|
||||
|
||||
@@ -117,11 +111,11 @@ VITE_CLOUD_BRAIN_UI_ENABLED=false
|
||||
| 配置 | 作用 | 生效方式 |
|
||||
| --- | --- | --- |
|
||||
| `VITE_CLOUD_BRAIN_UI_ENABLED` | 云脑套件入口与路由开关,默认关闭 | 构建时;修改后重启开发服务或重新构建 |
|
||||
| `VITE_UMAMI_WEBSITE_ID` | 启用 Umami 统计脚本注入 | Vite 配置通过 `loadEnv` 读取当前模式的环境文件及进程环境变量 |
|
||||
| `VITE_UMAMI_WEBSITE_ID` | 启用 Umami 统计脚本注入 | Vite 配置读取进程环境变量 |
|
||||
| `VITE_UMAMI_SCRIPT_URL` | Umami 脚本地址 | 同上,默认 `https://analytics.umami.is/script.js` |
|
||||
| `VITE_GOOGLE_ANALYTICS_ID` | 启用 Google Analytics 脚本注入 | 同上 |
|
||||
|
||||
统计脚本变量支持写入 `.env.local` 或对应模式的环境文件,也支持由启动终端或 CI 注入;同名进程环境变量优先。修改后需重启开发服务或重新构建。
|
||||
统计脚本插件使用 `process.env`,没有显式调用 Vite `loadEnv`;如果使用 Node/npm 运行 Vite,不要假定只写 `.env.local` 就能让配置文件读到这些统计变量,应由启动终端或 CI 环境注入。
|
||||
|
||||
前端环境变量和 `config.js` 都会暴露给浏览器,仅放公开配置,不放上游模型密钥或后端机密。
|
||||
|
||||
|
||||
+214
-152
@@ -17,170 +17,232 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
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,
|
||||
loadEnv,
|
||||
normalizePath,
|
||||
transformWithEsbuild,
|
||||
} from 'vite';
|
||||
import { defineConfig, 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'))}/`;
|
||||
|
||||
const DEFAULT_PROXY_TARGET = 'http://localhost:3000';
|
||||
const PROXY_PREFIXES = [
|
||||
'/api',
|
||||
'/v1',
|
||||
'/v1beta',
|
||||
'/mj',
|
||||
'/pg',
|
||||
'/dashboard',
|
||||
'/kling',
|
||||
'/jimeng',
|
||||
'/suno',
|
||||
];
|
||||
// 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;
|
||||
}
|
||||
|
||||
function createProxy(target) {
|
||||
return Object.fromEntries(
|
||||
PROXY_PREFIXES.map((prefix) => [
|
||||
prefix,
|
||||
{
|
||||
target,
|
||||
changeOrigin: true,
|
||||
...(prefix === '/v1' ? { ws: true } : {}),
|
||||
// Use the exposed transform from vite, instead of directly
|
||||
// transforming with esbuild
|
||||
return transformWithEsbuild(code, id, {
|
||||
loader: 'jsx',
|
||||
jsx: 'automatic',
|
||||
});
|
||||
},
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
// 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/',
|
||||
],
|
||||
},
|
||||
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;
|
||||
},
|
||||
},
|
||||
],
|
||||
['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(
|
||||
'<!--umami-->',
|
||||
`<script defer src="${umamiUrl}" data-website-id="${umamiId}"></script>`,
|
||||
);
|
||||
}
|
||||
const gaId = 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;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
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',
|
||||
),
|
||||
optimizeDeps: {
|
||||
force: true,
|
||||
esbuildOptions: {
|
||||
loader: {
|
||||
'.js': 'jsx',
|
||||
'.json': 'json',
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
legacyJsxPlugin(),
|
||||
react(),
|
||||
vitePluginSemi({ cssLayer: true }),
|
||||
analyticsPlugin(env),
|
||||
],
|
||||
optimizeDeps: {
|
||||
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';
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
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: {
|
||||
'/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,
|
||||
},
|
||||
},
|
||||
server: {
|
||||
host: '0.0.0.0',
|
||||
proxy: createProxy(proxyTarget),
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user