Files
monkey-ai-web/vite.config.js
T

164 lines
5.2 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 path from 'node:path';
import react from '@vitejs/plugin-react';
import { defineConfig, loadEnv, transformWithEsbuild } from 'vite';
/**
* 未配置代理变量时保留原有联调地址,兼容自定义 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 }) => {
const env = loadEnv(mode, __dirname, ['VITE_', 'DEV_PROXY_']);
const proxyTarget = env.DEV_PROXY_TARGET || DEV_PROXY_TARGET;
return {
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'~': path.resolve(__dirname, 'node_modules'),
},
},
plugins: [jsxInJsPlugin(), react(), analyticsPlugin(env)],
optimizeDeps: {
esbuildOptions: {
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 },
},
},
};
});