vite.config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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['/builder/api'] = {
  40. target: 'http://127.0.0.1:8080',
  41. changeOrigin: true,
  42. }
  43. proxyConf['/chat/api'] = {
  44. target: 'http://127.0.0.1:8080',
  45. changeOrigin: true,
  46. }
  47. proxyConf['/doc'] = {
  48. target: 'http://127.0.0.1:8080',
  49. changeOrigin: true,
  50. rewrite: (path: string) => path.replace(basePath, '/'),
  51. }
  52. proxyConf['/schema'] = {
  53. target: 'http://127.0.0.1:8080',
  54. changeOrigin: true,
  55. rewrite: (path: string) => path.replace(basePath, '/'),
  56. }
  57. proxyConf['/static'] = {
  58. target: 'http://127.0.0.1:8080',
  59. changeOrigin: true,
  60. rewrite: (path: string) => path.replace(basePath, '/'),
  61. }
  62. proxyConf[`^${basePath}.+\/oss\/file\/.*$`] = {
  63. target: 'http://127.0.0.1:8080',
  64. changeOrigin: true,
  65. }
  66. proxyConf[`^${basePath}oss\/file\/.*$`] = {
  67. target: 'http://127.0.0.1:8080',
  68. changeOrigin: true,
  69. }
  70. proxyConf[`^${basePath}oss\/get_url\/.*$`] = {
  71. target: 'http://127.0.0.1:8080',
  72. changeOrigin: true,
  73. }
  74. proxyConf[basePath] = {
  75. target: `http://127.0.0.1:${ENV.VITE_APP_PORT || 8080}`,
  76. changeOrigin: true,
  77. rewrite: (path: string) => path.replace(basePath, '/'),
  78. }
  79. if (mode !== 'chat') {
  80. proxyConf['/chat'] = {
  81. target: 'http://127.0.0.1:3001',
  82. changeOrigin: true,
  83. bypass: (req: any) => {
  84. if (req.url && req.url.startsWith('/chat/api')) {
  85. return req.url
  86. }
  87. }
  88. }
  89. }
  90. if (mode === 'builder') {
  91. proxyConf['/admin'] = {
  92. target: 'http://127.0.0.1:8080',
  93. changeOrigin: true,
  94. }
  95. }
  96. return {
  97. preflight: false,
  98. lintOnSave: false,
  99. base: '/admin/',
  100. envDir: envDir,
  101. plugins: [
  102. vue(),
  103. vueJsx(),
  104. DefineOptions(),
  105. createHtmlPlugin({template: entryFile}),
  106. renameHtmlPlugin(`dist${basePath}`, entryFile),
  107. ],
  108. server: {
  109. cors: true,
  110. host: '0.0.0.0',
  111. port: Number(ENV.VITE_APP_PORT) || 8080,
  112. strictPort: true,
  113. proxy: proxyConf,
  114. },
  115. build: {
  116. outDir: `dist${basePath}`,
  117. target: 'es2022',
  118. rollupOptions: {
  119. input: entryFile,
  120. },
  121. },
  122. resolve: {
  123. alias: {
  124. '@': fileURLToPath(new URL('./src', import.meta.url)),
  125. },
  126. },
  127. }
  128. })