vite.config.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { fileURLToPath, URL } from 'node:url'
  2. import { type ConfigEnv, type UserConfigExport, loadEnv } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import { format } from 'date-fns'
  5. import AutoImport from 'unplugin-auto-import/vite'
  6. import Components from 'unplugin-vue-components/vite'
  7. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  8. export default (configEnv: ConfigEnv): UserConfigExport => {
  9. const viteEnv = loadEnv(configEnv.mode, process.cwd()) as ImportMetaEnv
  10. const { VITE_PUBLIC_PATH } = viteEnv
  11. const isDevelopment = configEnv.mode === 'development'
  12. return {
  13. base: VITE_PUBLIC_PATH,
  14. server: {
  15. /** 设置 host: true 才可以使用 Network 的形式,以 IP 访问项目 */
  16. host: true,
  17. /** 端口号 */
  18. port: 3333,
  19. /** 是否自动打开浏览器 */
  20. open: false,
  21. /** 跨域设置允许 */
  22. cors: true,
  23. /** 端口被占用时,是否直接退出 */
  24. strictPort: false,
  25. /** 接口代理 */
  26. proxy: isDevelopment
  27. ? {
  28. '/sports/': {
  29. target: 'http://192.168.32.103/',
  30. ws: true,
  31. /** 是否允许跨域 */
  32. changeOrigin: true
  33. },
  34. '/API/V1.0': {
  35. target: 'http://192.168.32.103:7000',
  36. ws: true,
  37. /** 是否允许跨域 */
  38. changeOrigin: true
  39. },
  40. '/XWAPI/V1.0': {
  41. target: 'http://192.168.32.103:7000',
  42. ws: true,
  43. /** 是否允许跨域 */
  44. changeOrigin: true
  45. }
  46. }
  47. : {},
  48. /** 预热常用文件,提高初始页面加载速度 */
  49. warmup: {
  50. clientFiles: ['./src/layouts/**/*.vue']
  51. }
  52. },
  53. plugins: [
  54. vue(),
  55. AutoImport({
  56. resolvers: [ElementPlusResolver()],
  57. include: [
  58. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  59. /\.vue$/,
  60. /\.vue\?vue/, // .vue
  61. /\.md$/ // .md
  62. ],
  63. imports: ['vue', 'vue-router'], // 自动导入
  64. //这个一定要配置,会多出一个auto-import.d.ts文件,
  65. dts: 'src/auto-import.d.ts'
  66. }),
  67. Components({
  68. resolvers: [ElementPlusResolver(
  69. {
  70. importStyle: 'sass',
  71. importIcons: true
  72. }
  73. )]
  74. })
  75. ],
  76. css: {
  77. preprocessorOptions: {
  78. scss: {
  79. api: 'modern-compiler'
  80. }
  81. }
  82. },
  83. resolve: {
  84. alias: {
  85. '@': fileURLToPath(new URL('./src', import.meta.url))
  86. }
  87. },
  88. define: {
  89. __BUILD_DATE__: JSON.stringify(format(new Date(), 'yyyy-MM-dd HH:mm:ss'))
  90. }
  91. }
  92. }