feat: 代理统一管理、分包规则集中配置、路径处理更规范、复用依赖缓存、环境变量正常加载
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
# Frontend build-time feature flags. Rebuild the frontend after changing them.
|
# Frontend build-time feature flags. Rebuild the frontend after changing them.
|
||||||
# Missing or false keeps the Cloud Brain suite hidden and blocks direct routes.
|
# Missing or false keeps the Cloud Brain suite hidden and blocks direct routes.
|
||||||
VITE_CLOUD_BRAIN_UI_ENABLED=false
|
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,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
|
```text
|
||||||
/api /v1 /v1beta /mj /pg /dashboard /kling /jimeng /suno
|
/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_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_UMAMI_SCRIPT_URL` | Umami 脚本地址 | 同上,默认 `https://analytics.umami.is/script.js` |
|
||||||
| `VITE_GOOGLE_ANALYTICS_ID` | 启用 Google Analytics 脚本注入 | 同上 |
|
| `VITE_GOOGLE_ANALYTICS_ID` | 启用 Google Analytics 脚本注入 | 同上 |
|
||||||
|
|
||||||
统计脚本插件使用 `process.env`,没有显式调用 Vite `loadEnv`;如果使用 Node/npm 运行 Vite,不要假定只写 `.env.local` 就能让配置文件读到这些统计变量,应由启动终端或 CI 环境注入。
|
统计脚本变量支持写入 `.env.local` 或对应模式的环境文件,也支持由启动终端或 CI 注入;同名进程环境变量优先。修改后需重启开发服务或重新构建。
|
||||||
|
|
||||||
前端环境变量和 `config.js` 都会暴露给浏览器,仅放公开配置,不放上游模型密钥或后端机密。
|
前端环境变量和 `config.js` 都会暴露给浏览器,仅放公开配置,不放上游模型密钥或后端机密。
|
||||||
|
|
||||||
|
|||||||
+109
-171
@@ -17,59 +17,110 @@ 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 { fileURLToPath } from 'node:url';
|
||||||
import react from '@vitejs/plugin-react';
|
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 pkg from '@douyinfe/vite-plugin-semi';
|
||||||
import path from 'path';
|
|
||||||
const { vitePluginSemi } = pkg;
|
|
||||||
|
|
||||||
// https://vitejs.dev/config/
|
const { vitePluginSemi } = pkg;
|
||||||
export default defineConfig({
|
const projectRoot = path.dirname(fileURLToPath(import.meta.url));
|
||||||
resolve: {
|
const sourceRoot = `${normalizePath(path.resolve(projectRoot, 'src'))}/`;
|
||||||
alias: {
|
|
||||||
'@': path.resolve(__dirname, './src'),
|
const DEFAULT_PROXY_TARGET = 'http://localhost:3000';
|
||||||
'~': path.resolve(__dirname, 'node_modules'),
|
const PROXY_PREFIXES = [
|
||||||
'@douyinfe/semi-theme-default': path.resolve(
|
'/api',
|
||||||
__dirname,
|
'/v1',
|
||||||
'node_modules/@douyinfe/semi-theme-default',
|
'/v1beta',
|
||||||
),
|
'/mj',
|
||||||
},
|
'/pg',
|
||||||
},
|
'/dashboard',
|
||||||
plugins: [
|
'/kling',
|
||||||
|
'/jimeng',
|
||||||
|
'/suno',
|
||||||
|
];
|
||||||
|
|
||||||
|
function createProxy(target) {
|
||||||
|
return Object.fromEntries(
|
||||||
|
PROXY_PREFIXES.map((prefix) => [
|
||||||
|
prefix,
|
||||||
{
|
{
|
||||||
name: 'treat-js-files-as-jsx',
|
target,
|
||||||
async transform(code, id) {
|
changeOrigin: true,
|
||||||
if (!/src\/.*\.js$/.test(id)) {
|
...(prefix === '/v1' ? { ws: true } : {}),
|
||||||
return null;
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the exposed transform from vite, instead of directly
|
// Keep matching order: the first matching group owns the module.
|
||||||
// transforming with esbuild
|
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/',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
['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, {
|
return transformWithEsbuild(code, id, {
|
||||||
loader: 'jsx',
|
loader: 'jsx',
|
||||||
jsx: 'automatic',
|
jsx: 'automatic',
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
},
|
};
|
||||||
react(),
|
}
|
||||||
vitePluginSemi({
|
|
||||||
cssLayer: true,
|
function analyticsPlugin(env) {
|
||||||
}),
|
return {
|
||||||
{
|
|
||||||
name: 'inject-analytics',
|
name: 'inject-analytics',
|
||||||
transformIndexHtml(html) {
|
transformIndexHtml(html) {
|
||||||
let result = html;
|
let result = html;
|
||||||
const umamiId = process.env.VITE_UMAMI_WEBSITE_ID;
|
const umamiId = env.VITE_UMAMI_WEBSITE_ID;
|
||||||
const umamiUrl =
|
const umamiUrl =
|
||||||
process.env.VITE_UMAMI_SCRIPT_URL ||
|
env.VITE_UMAMI_SCRIPT_URL || 'https://analytics.umami.is/script.js';
|
||||||
'https://analytics.umami.is/script.js';
|
|
||||||
if (umamiId) {
|
if (umamiId) {
|
||||||
result = result.replace(
|
result = result.replace(
|
||||||
'<!--umami-->',
|
'<!--umami-->',
|
||||||
`<script defer src="${umamiUrl}" data-website-id="${umamiId}"></script>`,
|
`<script defer src="${umamiUrl}" data-website-id="${umamiId}"></script>`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const gaId = process.env.VITE_GOOGLE_ANALYTICS_ID;
|
const gaId = env.VITE_GOOGLE_ANALYTICS_ID;
|
||||||
if (gaId) {
|
if (gaId) {
|
||||||
result = result.replace(
|
result = result.replace(
|
||||||
'<!--Google Analytics-->',
|
'<!--Google Analytics-->',
|
||||||
@@ -79,10 +130,32 @@ export default defineConfig({
|
|||||||
}
|
}
|
||||||
return result;
|
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',
|
||||||
|
),
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
legacyJsxPlugin(),
|
||||||
|
react(),
|
||||||
|
vitePluginSemi({ cssLayer: true }),
|
||||||
|
analyticsPlugin(env),
|
||||||
],
|
],
|
||||||
optimizeDeps: {
|
optimizeDeps: {
|
||||||
force: true,
|
|
||||||
esbuildOptions: {
|
esbuildOptions: {
|
||||||
loader: {
|
loader: {
|
||||||
'.js': 'jsx',
|
'.js': 'jsx',
|
||||||
@@ -91,158 +164,23 @@ export default defineConfig({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
build: {
|
build: {
|
||||||
// 使用 esbuild 压缩,速度更快且体积更小
|
|
||||||
minify: 'esbuild',
|
minify: 'esbuild',
|
||||||
// 启用 CSS 代码分割
|
|
||||||
cssCodeSplit: true,
|
cssCodeSplit: true,
|
||||||
// 设置 chunk 大小警告阈值 (KB)
|
|
||||||
chunkSizeWarningLimit: 1000,
|
chunkSizeWarningLimit: 1000,
|
||||||
// Target 现代浏览器以获得更小的打包体积
|
|
||||||
target: 'es2015',
|
target: 'es2015',
|
||||||
// 生产环境不需要 sourcemap
|
|
||||||
sourcemap: false,
|
sourcemap: false,
|
||||||
// esbuild 压缩选项
|
|
||||||
esbuild: {
|
|
||||||
drop: ['console', 'debugger'],
|
|
||||||
legalComments: 'none',
|
|
||||||
},
|
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
output: {
|
output: {
|
||||||
// 优化 chunk 命名以支持更好的缓存
|
|
||||||
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,
|
||||||
// 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: createProxy(proxyTarget),
|
||||||
'/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,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user