vite.config.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import vue from '@vitejs/plugin-vue';
  2. import { resolve } from 'path';
  3. import { ConfigEnv, defineConfig, loadEnv } from 'vite';
  4. import vueJsx from '@vitejs/plugin-vue-jsx';
  5. import compression from 'vite-plugin-compression';
  6. import AutoImport from 'unplugin-auto-import/vite'
  7. import Components from 'unplugin-vue-components/vite'
  8. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  9. import { lazyImport, VxeResolver } from 'vite-plugin-lazy-import'
  10. const pathResolve = (dir: string) => {
  11. return resolve(__dirname, '.', dir);
  12. };
  13. const alias: Record<string, string> = {
  14. '/@': pathResolve('./src/'),
  15. '@views': pathResolve('./src/views'),
  16. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
  17. '@dvaformflow': pathResolve('./src/viwes/plugins/dvaadmin_form_flow/src/')
  18. };
  19. const viteConfig = defineConfig((mode: ConfigEnv) => {
  20. const env = loadEnv(mode.mode, process.cwd());
  21. return {
  22. plugins: [
  23. vue(),
  24. vueJsx(),
  25. // vueSetupExtend()
  26. AutoImport({
  27. imports: [
  28. 'vue',
  29. 'vue-router',
  30. 'pinia',
  31. {
  32. 'dayjs': [
  33. // 默认导入
  34. ['default', 'dayjs']
  35. ],
  36. // '/@/utils/useTableData': [
  37. // 'useTableData'
  38. // ],
  39. // '/@/utils/usePagination': [
  40. // 'usePagination'
  41. // ],
  42. // '/@/utils/useResponse': [
  43. // 'useResponse'
  44. // ]
  45. },
  46. ],
  47. resolvers: [ElementPlusResolver()],
  48. dts: 'src/auto-imports.d.ts', // 生成 TypeScript 声明文件
  49. }),
  50. Components({
  51. // resolvers: [ElementPlusResolver()],
  52. }),
  53. compression({
  54. algorithm: 'gzip', // 使用 gzip 压缩
  55. ext: '.gz', // 输出的文件扩展名
  56. threshold: 10240, // 只有大小大于该值的资源会被压缩(默认 10KB)
  57. deleteOriginFile: false, // 是否删除原始未压缩的文件
  58. }),
  59. lazyImport({
  60. resolvers: [
  61. VxeResolver({
  62. libraryName: 'vxe-table'
  63. }),
  64. VxeResolver({
  65. libraryName: 'vxe-pc-ui'
  66. })
  67. ]
  68. })
  69. ],
  70. root: process.cwd(),
  71. resolve: { alias },
  72. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  73. optimizeDeps: {
  74. include: [ 'element-plus/es/locale/lang/zh-cn', 'element-plus/es/locale/lang/en', 'element-plus/es/locale/lang/zh-tw' ]
  75. },
  76. server: {
  77. host: '0.0.0.0',
  78. port: env.VITE_PORT as unknown as number,
  79. open: false,
  80. hmr: true,
  81. proxy: {
  82. '/gitee': {
  83. target: 'https://gitee.com',
  84. ws: true,
  85. changeOrigin: true,
  86. rewrite: (path) => path.replace(/^\/gitee/, '')
  87. }
  88. }
  89. },
  90. build: {
  91. outDir: env.VITE_DIST_PATH || 'dist',
  92. chunkSizeWarningLimit: 1500,
  93. rollupOptions: {
  94. output: {
  95. entryFileNames: `assets/[name].[hash].js`,
  96. chunkFileNames: `assets/[name].[hash].js`,
  97. assetFileNames: `assets/[name].[hash].[ext]`,
  98. compact: true,
  99. manualChunks: {
  100. vue: [ 'vue', 'vue-router', 'pinia' ],
  101. echarts: [ 'echarts' ],
  102. elementPlus: ['element-plus'],
  103. }
  104. // manualChunks(id) {
  105. // if (id.includes('node_modules')) {
  106. // return 'vendor';
  107. // }
  108. // }
  109. }
  110. }
  111. },
  112. css: {
  113. preprocessorOptions: {
  114. css: { charset: false },
  115. scss: { api: 'modern-compiler' },
  116. }
  117. },
  118. define: {
  119. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  120. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  121. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  122. __VERSION__: JSON.stringify(process.env.npm_package_version)
  123. }
  124. };
  125. });
  126. export default viteConfig;