100 lines
2.6 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 { resolve } from 'path'
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 fs from 'node:fs'
import path from 'node:path'
// 读取 apps.txt 确定应用优先级(靠后优先)
function loadAppsOrder(appsDir: string) {
const appsTxt = path.join(appsDir, 'apps.txt')
try {
const content = fs.readFileSync(appsTxt, 'utf-8')
return content
.split(/\r?\n/)
.map((s) => s.trim())
.filter(Boolean)
} catch {
return ['jingrow']
}
}
// 计算本工程中的 apps 目录(当前文件位于 apps/jingrow/frontend/vite.config.ts
const currentDir = fileURLToPath(new URL('.', import.meta.url))
const appsDir = path.resolve(currentDir, '..', '..')
const APPS_ORDER = loadAppsOrder(appsDir)
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
const JINGROW_SERVER_URL = env.VITE_JINGROW_SERVER_URL
const BACKEND_URL = env.VITE_BACKEND_SERVER_URL
const FRONTEND_HOST = env.VITE_FRONTEND_HOST || '0.0.0.0'
const FRONTEND_PORT = Number(env.VITE_FRONTEND_PORT) || 3100
return {
plugins: [
vue(),
Icons({
autoInstall: true,
compiler: 'vue3'
}),
Components({
resolvers: [
IconsResolver({
prefix: 'i',
enabledCollections: ['tabler']
}),
],
}),
],
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,
fs: {
// 放行 monorepo apps 目录,便于 import.meta.glob 跨应用扫描
allow: [appsDir]
},
proxy: {
'/api/action': {
target: JINGROW_SERVER_URL,
changeOrigin: true,
secure: false
},
'/api/data': {
target: BACKEND_URL,
changeOrigin: true,
secure: false
},
'/jingrow': {
target: BACKEND_URL,
changeOrigin: true,
secure: false
}
}
},
build: {
outDir: 'dist',
assetsDir: 'assets'
},
define: {
// 确保环境变量在构建时可用
__APP_VERSION__: JSON.stringify(process.env.npm_package_version),
// 注入 apps.txt 的应用顺序到前端
__APPS_ORDER__: JSON.stringify(APPS_ORDER)
}
}
})