vite.config.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { fileURLToPath, URL } from 'node:url'
  2. import { defineConfig, loadEnv } from 'vite'
  3. import Unocss from 'unocss/vite'
  4. import vue from '@vitejs/plugin-vue'
  5. import vueJsx from '@vitejs/plugin-vue-jsx'
  6. import VueDevTools from 'vite-plugin-vue-devtools'
  7. import vueSetupExtend from 'vite-plugin-vue-setup-extend'
  8. import viteCompression from 'vite-plugin-compression'
  9. import AutoImport from 'unplugin-auto-import/vite'
  10. import Components from 'unplugin-vue-components/vite'
  11. import { VantResolver } from 'unplugin-vue-components/resolvers'
  12. import { resolve } from 'path'
  13. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  14. const env = process.env.NODE_ENV;
  15. export default defineConfig({
  16. plugins: [
  17. vue(),
  18. Unocss(),
  19. vueJsx(),
  20. VueDevTools(),
  21. vueSetupExtend(),
  22. Components({
  23. extensions: ['vue', 'tsx', 'md'],
  24. globs: ['src/components/*/*.vue', 'src/components/*/*.tsx'],
  25. include: [/\.vue$/, /\.vue\?vue/, /\.md$/, /\.[tj]sx?$/],
  26. resolvers: [VantResolver()],
  27. dts: 'src/typings/components.d.ts'
  28. }),
  29. AutoImport({
  30. include: [/\.[tj]sx?$/, /\.vue$/, /\.vue\?vue/],
  31. imports: ['vue', 'vue-router'],
  32. dts: 'src/typings/auto-imports.d.ts',
  33. resolvers: [VantResolver()],
  34. eslintrc: {
  35. enabled: true,
  36. filepath: './.eslintrc-auto-import.json'
  37. }
  38. }),
  39. viteCompression({
  40. algorithm: 'gzip',
  41. ext: '.gz',
  42. deleteOriginFile: false,
  43. threshold: 1024
  44. }),
  45. viteCompression({
  46. algorithm: 'brotliCompress',
  47. ext: '.br',
  48. deleteOriginFile: false,
  49. threshold: 1024
  50. }),
  51. // svg 图标
  52. createSvgIconsPlugin({
  53. iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
  54. svgoOptions: true,
  55. symbolId: 'icon-[dir]-[name]'
  56. })
  57. ],
  58. resolve: {
  59. alias: {
  60. '@': fileURLToPath(new URL('./src', import.meta.url))
  61. }
  62. },
  63. base:env === 'production' ? '/mobile' : '/',
  64. server: {
  65. host: '0.0.0.0',
  66. port: 3200,
  67. open: true,
  68. proxy: {
  69. '/api': {
  70. target: 'http://localhost:3000',
  71. // target: 'https://demo.lowflow.vip/api',
  72. changeOrigin: true,
  73. ws: true,
  74. rewrite: (path) => path.replace(/^\/api/, ''),
  75. secure: false
  76. }
  77. }
  78. },
  79. build: {
  80. rollupOptions: {
  81. output: {
  82. // 打包分类
  83. chunkFileNames: 'assets/js/[name]-[hash].js',
  84. entryFileNames: 'assets/js/[name]-[hash].js',
  85. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
  86. // 分包策略
  87. manualChunks: {
  88. vue: ['vue'],
  89. 'vue-router': ['vue-router']
  90. }
  91. }
  92. }
  93. }
  94. })