vite.config.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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: [
  75. 'element-plus/es/locale/lang/zh-cn',
  76. 'element-plus/es/locale/lang/en',
  77. 'element-plus/es/locale/lang/zh-tw',
  78. '@fast-crud/fast-crud'
  79. ]
  80. },
  81. server: {
  82. host: '0.0.0.0',
  83. port: env.VITE_PORT as unknown as number,
  84. open: false,
  85. hmr: true,
  86. proxy: {
  87. '/gitee': {
  88. target: 'https://gitee.com',
  89. ws: true,
  90. changeOrigin: true,
  91. rewrite: (path) => path.replace(/^\/gitee/, '')
  92. }
  93. }
  94. },
  95. build: {
  96. outDir: env.VITE_DIST_PATH || 'dist',
  97. chunkSizeWarningLimit: 1500,
  98. rollupOptions: {
  99. output: {
  100. entryFileNames: `assets/[name].[hash].js`,
  101. chunkFileNames: `assets/[name].[hash].js`,
  102. assetFileNames: `assets/[name].[hash].[ext]`,
  103. compact: true,
  104. manualChunks: {
  105. vue: [ 'vue', 'vue-router', 'pinia' ],
  106. echarts: [ 'echarts' ],
  107. elementPlus: [ 'element-plus' ]
  108. }
  109. // manualChunks(id) {
  110. // if (id.includes('node_modules')) {
  111. // return 'vendor';
  112. // }
  113. // }
  114. }
  115. }
  116. },
  117. css: {
  118. preprocessorOptions: {
  119. css: { charset: false },
  120. scss: { api: 'modern-compiler' }
  121. }
  122. },
  123. define: {
  124. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  125. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  126. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  127. __VERSION__: JSON.stringify(process.env.npm_package_version)
  128. }
  129. };
  130. });
  131. export default viteConfig;