| import { defineConfig, loadEnv, Plugin } from 'vite' |
| import vue from '@vitejs/plugin-vue' |
| import checker from 'vite-plugin-checker' |
| import { resolve } from 'path' |
|
|
| |
| |
| |
| |
| function injectPublicSettings(backendUrl: string): Plugin { |
| return { |
| name: 'inject-public-settings', |
| apply: 'serve', |
| transformIndexHtml: { |
| order: 'pre', |
| async handler(html) { |
| try { |
| const response = await fetch(`${backendUrl}/api/v1/settings/public`, { |
| signal: AbortSignal.timeout(2000) |
| }) |
| if (response.ok) { |
| const data = await response.json() |
| if (data.code === 0 && data.data) { |
| const script = `<script>window.__APP_CONFIG__=${JSON.stringify(data.data)};</script>` |
| return html.replace('</head>', `${script}\n</head>`) |
| } |
| } |
| } catch (e) { |
| console.warn('[vite] 无法获取公开配置,将回退到 API 调用:', (e as Error).message) |
| } |
| return html |
| } |
| } |
| } |
| } |
|
|
| export default defineConfig(({ mode }) => { |
| |
| const env = loadEnv(mode, process.cwd(), '') |
| const backendUrl = env.VITE_DEV_PROXY_TARGET || 'http://localhost:8080' |
| const devPort = Number(env.VITE_DEV_PORT || 3000) |
|
|
| return { |
| plugins: [ |
| vue(), |
| checker({ |
| typescript: true, |
| vueTsc: true |
| }), |
| injectPublicSettings(backendUrl) |
| ], |
| resolve: { |
| alias: { |
| '@': resolve(__dirname, 'src'), |
| |
| 'vue-i18n': 'vue-i18n/dist/vue-i18n.runtime.esm-bundler.js' |
| } |
| }, |
| define: { |
| |
| |
| __INTLIFY_JIT_COMPILATION__: true |
| }, |
| build: { |
| outDir: '../backend/internal/web/dist', |
| emptyOutDir: true, |
| rollupOptions: { |
| output: { |
| |
| |
| |
| |
| manualChunks(id: string) { |
| if (id.includes('node_modules')) { |
| |
| if ( |
| id.includes('/vue/') || |
| id.includes('/vue-router/') || |
| id.includes('/pinia/') || |
| id.includes('/@vue/') |
| ) { |
| return 'vendor-vue' |
| } |
|
|
| |
| if (id.includes('/@vueuse/') || id.includes('/xlsx/')) { |
| return 'vendor-ui' |
| } |
|
|
| |
| if (id.includes('/chart.js/') || id.includes('/vue-chartjs/')) { |
| return 'vendor-chart' |
| } |
|
|
| |
| if (id.includes('/vue-i18n/') || id.includes('/@intlify/')) { |
| return 'vendor-i18n' |
| } |
|
|
| |
| return 'vendor-misc' |
| } |
|
|
| |
| |
| } |
| } |
| } |
| }, |
| server: { |
| host: '0.0.0.0', |
| port: devPort, |
| proxy: { |
| '/api': { |
| target: backendUrl, |
| changeOrigin: true |
| }, |
| '/v1': { |
| target: backendUrl, |
| changeOrigin: true |
| }, |
| '/setup': { |
| target: backendUrl, |
| changeOrigin: true |
| } |
| } |
| } |
| } |
| }) |
|
|