vite.config.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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, VITE_HOST_IP } = 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. '/API/V1.0': {
  29. target: `http://${VITE_HOST_IP}:7000`,
  30. ws: true,
  31. /** 是否允许跨域 */
  32. changeOrigin: true
  33. }
  34. }
  35. : {},
  36. /** 预热常用文件,提高初始页面加载速度 */
  37. warmup: {
  38. clientFiles: ['./src/layouts/**/*.vue']
  39. }
  40. },
  41. plugins: [
  42. vue(),
  43. AutoImport({
  44. resolvers: [ElementPlusResolver()],
  45. include: [
  46. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  47. /\.vue$/,
  48. /\.vue\?vue/, // .vue
  49. /\.md$/ // .md
  50. ],
  51. imports: ['vue', 'vue-router'], // 自动导入
  52. //这个一定要配置,会多出一个auto-import.d.ts文件,
  53. dts: 'src/auto-import.d.ts'
  54. }),
  55. Components({
  56. resolvers: [ElementPlusResolver(
  57. {
  58. importStyle: 'sass',
  59. importIcons: true
  60. }
  61. )]
  62. })
  63. ],
  64. css: {
  65. preprocessorOptions: {
  66. scss: {
  67. api: 'modern-compiler'
  68. }
  69. }
  70. },
  71. resolve: {
  72. alias: {
  73. '@': fileURLToPath(new URL('./src', import.meta.url))
  74. }
  75. },
  76. define: {
  77. __BUILD_DATE__: JSON.stringify(format(new Date(), 'yyyy-MM-dd HH:mm:ss'))
  78. }
  79. }
  80. }