vite.config.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. const mqttHostAlarm = env.VITE_MQTT_HOST_ALARM
  18. const mqttHostPoint = env.VITE_MQTT_HOST_POINT
  19. // 添加日志输出
  20. console.log('📘📘📘 Environment LOG 📘📘📘', {
  21. mode,
  22. apiHost,
  23. mqttHostAlarm,
  24. mqttHostPoint,
  25. })
  26. const pkg = JSON.parse(readFileSync('./package.json', 'utf-8'))
  27. return {
  28. plugins: [
  29. vue(),
  30. vueDevTools(),
  31. buildInfoPlugin(mode),
  32. Components({
  33. resolvers: [
  34. AntDesignVueResolver({
  35. importStyle: false, // css in js
  36. }),
  37. ],
  38. }),
  39. ],
  40. define: {
  41. __APP_VERSION__: JSON.stringify(pkg.version),
  42. __BUILD_TIME__: JSON.stringify(formatDateTime(new Date())),
  43. },
  44. resolve: {
  45. alias: {
  46. '@': fileURLToPath(new URL('./src', import.meta.url)),
  47. 'three/addons': path.join(__dirname, 'node_modules/three/addons'),
  48. },
  49. },
  50. server: {
  51. proxy: {
  52. '/portal-service-server': {
  53. target: apiHost,
  54. changeOrigin: true,
  55. secure: false,
  56. rewrite: (path) => path,
  57. },
  58. '/pub': {
  59. target: apiHost,
  60. changeOrigin: true,
  61. secure: false,
  62. rewrite: (path) => path,
  63. },
  64. },
  65. cors: true,
  66. },
  67. }
  68. })