vite.config.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import {fileURLToPath, URL} from 'node:url'
  2. import type {ProxyOptions} from 'vite'
  3. import {defineConfig, loadEnv} from 'vite'
  4. import vue from '@vitejs/plugin-vue'
  5. import vueJsx from '@vitejs/plugin-vue-jsx'
  6. import DefineOptions from 'unplugin-vue-define-options/vite'
  7. import path from 'path'
  8. import {createHtmlPlugin} from 'vite-plugin-html'
  9. import fs from 'fs'
  10. const __filename = fileURLToPath(import.meta.url)
  11. const __dirname = path.dirname(__filename)
  12. const envDir = './env'
  13. const renameHtmlPlugin = (outDir: string, entry: string) => {
  14. return {
  15. name: 'rename-html',
  16. closeBundle: () => {
  17. const buildDir = path.resolve(__dirname, outDir)
  18. const oldFile = path.join(buildDir, entry)
  19. const newFile = path.join(buildDir, 'index.html')
  20. if (fs.existsSync(oldFile)) {
  21. if (fs.existsSync(newFile)) {
  22. fs.unlinkSync(newFile)
  23. }
  24. fs.renameSync(oldFile, newFile)
  25. }
  26. },
  27. }
  28. }
  29. export default defineConfig((conf: any) => {
  30. const mode = conf.mode
  31. const ENV = loadEnv(mode, envDir)
  32. const basePath = ENV.VITE_BASE_PATH || '/'
  33. const entryFile = ENV.VITE_ENTRY || 'index.html'
  34. const proxyConf: Record<string, string | ProxyOptions> = {}
  35. proxyConf['/admin/api'] = {
  36. target: 'http://127.0.0.1:8080',
  37. changeOrigin: true,
  38. }
  39. proxyConf['/chat/api'] = {
  40. target: 'http://127.0.0.1:8080',
  41. changeOrigin: true,
  42. }
  43. proxyConf['/doc'] = {
  44. target: 'http://127.0.0.1:8080',
  45. changeOrigin: true,
  46. rewrite: (path: string) => path.replace(basePath, '/'),
  47. }
  48. proxyConf['/schema'] = {
  49. target: 'http://127.0.0.1:8080',
  50. changeOrigin: true,
  51. rewrite: (path: string) => path.replace(basePath, '/'),
  52. }
  53. proxyConf['/static'] = {
  54. target: 'http://127.0.0.1:8080',
  55. changeOrigin: true,
  56. rewrite: (path: string) => path.replace(basePath, '/'),
  57. }
  58. proxyConf[`^${basePath}.+\/oss\/file\/.*$`] = {
  59. target: 'http://127.0.0.1:8080',
  60. changeOrigin: true,
  61. }
  62. proxyConf[`^${basePath}oss\/file\/.*$`] = {
  63. target: 'http://127.0.0.1:8080',
  64. changeOrigin: true,
  65. }
  66. proxyConf[`^${basePath}oss\/get_url\/.*$`] = {
  67. target: 'http://127.0.0.1:8080',
  68. changeOrigin: true,
  69. }
  70. proxyConf[basePath] = {
  71. target: `http://127.0.0.1:${ENV.VITE_APP_PORT || 8080}`,
  72. changeOrigin: true,
  73. rewrite: (path: string) => path.replace(basePath, '/'),
  74. }
  75. if (mode !== 'chat') {
  76. proxyConf['/chat'] = {
  77. target: 'http://127.0.0.1:3001',
  78. changeOrigin: true,
  79. bypass: (req: any) => {
  80. if (req.url && req.url.startsWith('/chat/api')) {
  81. return req.url
  82. }
  83. }
  84. }
  85. }
  86. return {
  87. preflight: false,
  88. lintOnSave: false,
  89. base: './',
  90. envDir: envDir,
  91. plugins: [
  92. vue(),
  93. vueJsx(),
  94. DefineOptions(),
  95. createHtmlPlugin({template: entryFile}),
  96. renameHtmlPlugin(`dist${basePath}`, entryFile),
  97. ],
  98. server: {
  99. cors: true,
  100. host: '0.0.0.0',
  101. port: Number(ENV.VITE_APP_PORT) || 8080,
  102. strictPort: true,
  103. proxy: proxyConf,
  104. },
  105. build: {
  106. outDir: `dist${basePath}`,
  107. target: 'es2022',
  108. rollupOptions: {
  109. input: entryFile,
  110. },
  111. },
  112. resolve: {
  113. alias: {
  114. '@': fileURLToPath(new URL('./src', import.meta.url)),
  115. },
  116. },
  117. }
  118. })