Files
monkey-ai-web/vite.config.js
T
qiubaishen db8cdf3360 sync: 同步 monkey-ai@weiyuan(ddc5b9443) 的 web/ 到 ui-optimization
- 合入 weiyuan 新功能前端:分佣 V2 / 伙伴工作台、智能路由、企业资金与分析、官网设置与预约演示等,
  semi-ui 已全部迁移到 antd 6 / @/components/ui,不重新引入 Semi
- 分佣二级菜单与页头去重、伙伴入口探测(企业伙伴走企业中心「企业分佣」)、超管侧栏入口恢复
- 适配层 DatePicker / TimePicker 统一 onChange(Date|Date[]|null, 文本) 契约;生产构建剔除调试日志
- 图片链接刷新:同任务请求复用、后台刷新不弹全局错误、离页停止批量请求
- 恢复 bun.lock(与 package-lock.json 共存);docs 规范与 CHANGELOG 更新
- 停止跟踪 .env.playwright.local(本地登录凭据,已加入 .gitignore)
2026-11-18 10:22:02 +08:00

191 lines
6.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
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 path from 'node:path';
import react from '@vitejs/plugin-react';
import { defineConfig, loadEnv, transformWithEsbuild } from 'vite';
import legacyCssLayers from './vite-plugin-legacy-css-layers.js';
/**
* 未配置代理变量时保留原有联调地址,兼容自定义 mode。
*/
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$/;
/**
* 让 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 注入统计脚本。
*
* 使用当前 mode 的环境变量;启动终端或 CI 的同名变量优先。
* 未提供对应 ID 时不注入脚本。
*/
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;
},
};
}
// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
const env = loadEnv(mode, __dirname, ['VITE_', 'DEV_PROXY_']);
const proxyTarget = env.DEV_PROXY_TARGET || DEV_PROXY_TARGET;
return {
// 生产构建剔除调试日志:在 esbuild 压缩阶段把 console.log/info/debug/trace 标为纯调用并删除,
// 同时去掉 debugger;保留 console.warn/error 便于线上排障。作用于全部产物(含 jsxInJsPlugin
// 处理的 src/*.js),开发服务不受影响。
esbuild:
command === 'build'
? {
drop: ['debugger'],
pure: [
'console.log',
'console.info',
'console.debug',
'console.trace',
],
}
: undefined,
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'~': path.resolve(__dirname, 'node_modules'),
},
},
plugins: [
jsxInJsPlugin(),
react(),
analyticsPlugin(env),
// 旧内核(< Chromium 99)@layer 兜底,仅 build 生效(b7bac50b5)
legacyCssLayers(),
],
optimizeDeps: {
// Scan the app entry only; generated HTML reports are not app entries.
entries: ['index.html'],
esbuildOptions: {
// Dependency source maps caused large parallel allocations in esbuild.
// App source maps and React Fast Refresh remain enabled.
sourcemap: false,
loader: {
'.js': 'jsx',
'.json': 'json',
},
},
},
build: {
// 使用 esbuild 压缩:速度远快于 terser,压缩率略低
minify: 'esbuild',
// 启用 CSS 代码分割
cssCodeSplit: true,
// chunk 体积告警阈值 (KB)
chunkSizeWarningLimit: 1000,
// 构建目标为 es2015,以换取更广的浏览器兼容性
target: 'es2015',
// 生产环境不需要 sourcemap
sourcemap: false,
rollupOptions: {
output: {
// 文件名带内容 hash,便于浏览器长期缓存与增量更新
chunkFileNames: 'assets/[name]-[hash].js',
entryFileNames: 'assets/[name]-[hash].js',
assetFileNames: 'assets/[name]-[hash].[ext]',
},
},
},
server: {
// 监听所有网卡,便于局域网与容器内访问
host: '0.0.0.0',
port: '5174',
// Browser reports contain HTML; watching them would reload active acceptance pages.
watch: {
ignored: [
'**/logs/**',
'**/playwright-report/**',
'**/test-results/**',
'**/blob-report/**',
'**/e2e/*-snapshots/**',
],
},
proxy: {
// 以下前缀统一转发到同一个后端,其中 /v1 额外开启 WebSocket
'/api': { target: proxyTarget, changeOrigin: true },
'/v1': { target: proxyTarget, changeOrigin: true, ws: true },
'/v1beta': { target: proxyTarget, changeOrigin: true },
'/mj': { target: proxyTarget, changeOrigin: true },
'/pg': { target: proxyTarget, changeOrigin: true },
'/dashboard': { target: proxyTarget, changeOrigin: true },
'/kling': { target: proxyTarget, changeOrigin: true },
'/jimeng': { target: proxyTarget, changeOrigin: true },
'/suno': { target: proxyTarget, changeOrigin: true },
},
},
};
});