jingrowtools/vite.config.ts
jingrow 6b638ac143 feat: 简化vite配置并修复去背景接口
- 简化vite.config.ts,删除冗余的apps.txt读取和文件服务插件
- 更新去背景接口路径为 /tools/rmbg/file/free
- 修复流式响应处理:使用fetch API正确处理NDJSON格式
- 添加/tools代理配置以支持新的接口路径
- 优化错误处理,适配fetch API的错误格式
2026-01-02 20:00:53 +08:00

98 lines
2.9 KiB
TypeScript
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.

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from 'node:url'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import Components from 'unplugin-vue-components/vite'
import path from 'node:path'
import { vitePluginPrerender } from './scripts/vite-plugin-prerender.js'
// 计算本工程中的 apps 目录(当前文件位于 apps/jingrow/frontend/vite.config.ts
const currentDir = fileURLToPath(new URL('.', import.meta.url))
const appsDir = path.resolve(currentDir, '..', '..')
export default defineConfig(({ mode, command }) => {
const env = loadEnv(mode, process.cwd(), '')
const BACKEND_URL = env.VITE_BACKEND_SERVER_URL || 'https://api.jingrow.com'
const FRONTEND_HOST = env.VITE_FRONTEND_HOST || '0.0.0.0'
const FRONTEND_PORT = Number(env.VITE_FRONTEND_PORT) || 3100
const ALLOWED_HOSTS = (env.VITE_ALLOWED_HOSTS || '').split(',').map((s) => s.trim()).filter(Boolean)
return {
plugins: [
vue(),
Icons({
autoInstall: true,
compiler: 'vue3'
}),
Components({
resolvers: [
IconsResolver({
prefix: 'i',
enabledCollections: ['tabler']
}),
],
}),
// 预渲染插件(仅在构建时启用)
command === 'build' && vitePluginPrerender({
// 路由列表会自动从工具 store 生成
// 也可以手动指定routes: ['/', '/tools', '/tools/remove-background']
rendererOptions: {
maxConcurrentRoutes: 4,
timeout: 30000,
waitUntil: 'networkidle0'
}
})
].filter(Boolean),
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
// 跨 app 访问源码(相对计算得到的 apps 目录)
'@apps': appsDir
}
},
server: {
host: FRONTEND_HOST,
port: FRONTEND_PORT,
strictPort: true,
open: false,
cors: true,
// 允许通过指定域名访问开发服务器(可通过 VITE_ALLOWED_HOSTS 配置,逗号分隔)
...(ALLOWED_HOSTS.length ? { allowedHosts: ALLOWED_HOSTS } : {}),
fs: {
// 放行 monorepo apps 目录,便于 import.meta.glob 跨应用扫描
allow: [appsDir]
},
proxy: {
'/api': {
target: BACKEND_URL,
changeOrigin: true,
secure: true,
},
'/jingrow': {
target: BACKEND_URL,
changeOrigin: true,
secure: true,
},
'/tools': {
target: BACKEND_URL,
changeOrigin: true,
secure: true,
}
}
},
build: {
outDir: 'dist',
assetsDir: 'assets',
sourcemap: false,
chunkSizeWarningLimit: 2000
},
define: {
// 确保环境变量在构建时可用
__APP_VERSION__: JSON.stringify(process.env.npm_package_version),
// 应用顺序(已移除 apps.txt 支持,使用默认值)
__APPS_ORDER__: JSON.stringify(['jingrow'])
}
}
})