vite.config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // import vueDevTools from 'vite-plugin-vue-devtools'
  11. const envDir = './env'
  12. // 自定义插件:重命名入口文件
  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. // 检查文件是否存在
  21. if (fs.existsSync(oldFile)) {
  22. // 删除已存在的 index.html
  23. if (fs.existsSync(newFile)) {
  24. fs.unlinkSync(newFile)
  25. }
  26. // 重命名文件
  27. fs.renameSync(oldFile, newFile)
  28. }
  29. },
  30. }
  31. }
  32. // https://vite.dev/config/
  33. export default defineConfig((conf: any) => {
  34. const mode = conf.mode
  35. const ENV = loadEnv(mode, envDir)
  36. const proxyConf: Record<string, string | ProxyOptions> = {}
  37. proxyConf['/admin/api'] = {
  38. target: 'http://127.0.0.1:8080',
  39. changeOrigin: true,
  40. }
  41. proxyConf['/chat/api'] = {
  42. target: 'http://127.0.0.1:8080',
  43. changeOrigin: true,
  44. }
  45. proxyConf['/doc'] = {
  46. target: 'http://127.0.0.1:8080',
  47. changeOrigin: true,
  48. rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
  49. }
  50. proxyConf['/schema'] = {
  51. target: 'http://127.0.0.1:8080',
  52. changeOrigin: true,
  53. rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
  54. }
  55. proxyConf['/static'] = {
  56. target: 'http://127.0.0.1:8080',
  57. changeOrigin: true,
  58. rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
  59. }
  60. // 前端静态资源转发到本身
  61. proxyConf[`^${ENV.VITE_BASE_PATH}.+\/oss\/file\/.*$`] = {
  62. target: `http://127.0.0.1:8080`,
  63. changeOrigin: true,
  64. }
  65. // 前端静态资源转发到本身
  66. proxyConf[`^${ENV.VITE_BASE_PATH}oss\/file\/.*$`] = {
  67. target: `http://127.0.0.1:8080`,
  68. changeOrigin: true,
  69. }
  70. proxyConf[`^${ENV.VITE_BASE_PATH}oss\/get_url\/.*$`] = {
  71. target: `http://127.0.0.1:8080`,
  72. changeOrigin: true,
  73. }
  74. // 前端静态资源转发到本身
  75. proxyConf[ENV.VITE_BASE_PATH] = {
  76. target: `http://127.0.0.1:${ENV.VITE_APP_PORT}`,
  77. changeOrigin: true,
  78. rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
  79. }
  80. // 代理 /chat 到 3001 端口
  81. if (mode !== 'chat') {
  82. proxyConf['/chat'] = {
  83. target: 'http://127.0.0.1:3001',
  84. changeOrigin: true,
  85. // 避免代理后端的 API
  86. bypass: (req: any) => {
  87. if (req.url && req.url.startsWith('/chat/api')) {
  88. return req.url
  89. }
  90. }
  91. }
  92. }
  93. return {
  94. preflight: false,
  95. lintOnSave: false,
  96. base: './',
  97. envDir: envDir,
  98. plugins: [
  99. vue(),
  100. vueJsx(),
  101. DefineOptions(),
  102. createHtmlPlugin({template: ENV.VITE_ENTRY}),
  103. renameHtmlPlugin(`dist${ENV.VITE_BASE_PATH}`, ENV.VITE_ENTRY),
  104. ],
  105. server: {
  106. cors: true,
  107. host: '0.0.0.0',
  108. port: Number(ENV.VITE_APP_PORT),
  109. strictPort: true,
  110. proxy: proxyConf,
  111. },
  112. build: {
  113. outDir: `dist${ENV.VITE_BASE_PATH}`,
  114. target: 'es2022',
  115. rollupOptions: {
  116. input: ENV.VITE_ENTRY,
  117. },
  118. },
  119. resolve: {
  120. alias: {
  121. '@': fileURLToPath(new URL('./src', import.meta.url)),
  122. },
  123. },
  124. }
  125. })