import { fileURLToPath, URL } from 'node:url'; import { defineConfig, loadEnv } from 'vite'; import vue from '@vitejs/plugin-vue'; import vueJsx from '@vitejs/plugin-vue-jsx'; import DefineOptions from 'unplugin-vue-define-options/vite'; import path from 'path'; import { createHtmlPlugin } from 'vite-plugin-html'; import fs from 'fs'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const envDir = './env'; const renameHtmlPlugin = (outDir, entry) => { return { name: 'rename-html', closeBundle: () => { const buildDir = path.resolve(__dirname, outDir); const oldFile = path.join(buildDir, entry); const newFile = path.join(buildDir, 'index.html'); if (fs.existsSync(oldFile)) { if (fs.existsSync(newFile)) { fs.unlinkSync(newFile); } fs.renameSync(oldFile, newFile); } }, }; }; export default defineConfig((conf) => { const mode = conf.mode; const ENV = loadEnv(mode, envDir); const basePath = ENV.VITE_BASE_PATH || '/'; const entryFile = ENV.VITE_ENTRY || 'index.html'; const proxyConf = {}; proxyConf['/admin/api'] = { target: 'http://127.0.0.1:8080', changeOrigin: true, }; proxyConf['/builder/api'] = { target: 'http://127.0.0.1:8080', changeOrigin: true, }; proxyConf['/chat/api'] = { target: 'http://127.0.0.1:8080', changeOrigin: true, }; proxyConf['/doc'] = { target: 'http://127.0.0.1:8080', changeOrigin: true, rewrite: (path) => path.replace(basePath, '/'), }; proxyConf['/schema'] = { target: 'http://127.0.0.1:8080', changeOrigin: true, rewrite: (path) => path.replace(basePath, '/'), }; proxyConf['/static'] = { target: 'http://127.0.0.1:8080', changeOrigin: true, rewrite: (path) => path.replace(basePath, '/'), }; proxyConf[`^${basePath}.+\/oss\/file\/.*$`] = { target: 'http://127.0.0.1:8080', changeOrigin: true, }; proxyConf[`^${basePath}oss\/file\/.*$`] = { target: 'http://127.0.0.1:8080', changeOrigin: true, }; proxyConf[`^${basePath}oss\/get_url\/.*$`] = { target: 'http://127.0.0.1:8080', changeOrigin: true, }; proxyConf[basePath] = { target: `http://127.0.0.1:${ENV.VITE_APP_PORT || 8080}`, changeOrigin: true, rewrite: (path) => path.replace(basePath, '/'), }; if (mode !== 'chat') { proxyConf['/chat'] = { target: 'http://127.0.0.1:3001', changeOrigin: true, bypass: (req) => { if (req.url && req.url.startsWith('/chat/api')) { return req.url; } } }; } if (mode === 'builder') { proxyConf['/admin'] = { target: 'http://127.0.0.1:8080', changeOrigin: true, }; } return { preflight: false, lintOnSave: false, base: '/admin/', envDir: envDir, plugins: [ vue(), vueJsx(), DefineOptions(), createHtmlPlugin({ template: entryFile }), renameHtmlPlugin(`dist${basePath}`, entryFile), ], server: { cors: true, host: '0.0.0.0', port: Number(ENV.VITE_APP_PORT) || 8080, strictPort: true, proxy: proxyConf, }, build: { outDir: `dist${basePath}`, target: 'es2022', rollupOptions: { input: entryFile, }, }, resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)), }, }, }; });