vite.config.ts 1.8 KB

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