vite.config.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { fileURLToPath, URL } from 'node:url'
  2. import { defineConfig, loadEnv } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import vueDevTools from 'vite-plugin-vue-devtools'
  5. import Components from 'unplugin-vue-components/vite'
  6. import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
  7. import { readFileSync } from 'fs'
  8. import { formatDateTime } from './src/utils'
  9. // https://vite.dev/config/
  10. export default defineConfig(({ mode }) => {
  11. // 加载环境变量
  12. const env = loadEnv(mode, process.cwd(), '')
  13. // 获取当前环境的API主机
  14. const apiHost = env.VITE_APP_HOST
  15. // 添加日志输出
  16. console.log(`🚀CurrentMode: ${mode}`)
  17. console.log(`🚀HostAPI: ${apiHost}`)
  18. console.log(`🚀ProxyAPI: ${apiHost}`)
  19. const pkg = JSON.parse(readFileSync('./package.json', 'utf-8'))
  20. return {
  21. plugins: [
  22. vue(),
  23. vueDevTools(),
  24. Components({
  25. resolvers: [
  26. AntDesignVueResolver({
  27. importStyle: false, // css in js
  28. }),
  29. ],
  30. }),
  31. ],
  32. define: {
  33. __APP_VERSION__: JSON.stringify(pkg.version),
  34. __BUILD_TIME__: JSON.stringify(formatDateTime(new Date())),
  35. },
  36. resolve: {
  37. alias: {
  38. '@': fileURLToPath(new URL('./src', import.meta.url)),
  39. },
  40. },
  41. server: {
  42. proxy: {
  43. '/portal-service-server': {
  44. target: apiHost,
  45. changeOrigin: true,
  46. secure: false,
  47. rewrite: (path) => path,
  48. },
  49. '/pub': {
  50. target: apiHost,
  51. changeOrigin: true,
  52. secure: false,
  53. rewrite: (path) => path,
  54. },
  55. },
  56. cors: true,
  57. },
  58. }
  59. })