vite.config.js 3.8 KB

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