vite.config.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. },
  37. ],
  38. resolvers: [ElementPlusResolver()],
  39. dts: 'src/auto-imports.d.ts', // 生成 TypeScript 声明文件
  40. }),
  41. Components({
  42. // resolvers: [ElementPlusResolver()],
  43. }),
  44. compression({
  45. algorithm: 'gzip', // 使用 gzip 压缩
  46. ext: '.gz', // 输出的文件扩展名
  47. threshold: 10240, // 只有大小大于该值的资源会被压缩(默认 10KB)
  48. deleteOriginFile: false, // 是否删除原始未压缩的文件
  49. }),
  50. lazyImport({
  51. resolvers: [
  52. VxeResolver({
  53. libraryName: 'vxe-table'
  54. }),
  55. VxeResolver({
  56. libraryName: 'vxe-pc-ui'
  57. })
  58. ]
  59. })
  60. ],
  61. root: process.cwd(),
  62. resolve: { alias },
  63. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  64. optimizeDeps: {
  65. include: [ 'element-plus/es/locale/lang/zh-cn', 'element-plus/es/locale/lang/en', 'element-plus/es/locale/lang/zh-tw' ]
  66. },
  67. server: {
  68. host: '0.0.0.0',
  69. port: env.VITE_PORT as unknown as number,
  70. open: false,
  71. hmr: true,
  72. proxy: {
  73. '/gitee': {
  74. target: 'https://gitee.com',
  75. ws: true,
  76. changeOrigin: true,
  77. rewrite: (path) => path.replace(/^\/gitee/, '')
  78. }
  79. }
  80. },
  81. build: {
  82. outDir: env.VITE_DIST_PATH || 'dist',
  83. chunkSizeWarningLimit: 1500,
  84. rollupOptions: {
  85. output: {
  86. entryFileNames: `assets/[name].[hash].js`,
  87. chunkFileNames: `assets/[name].[hash].js`,
  88. assetFileNames: `assets/[name].[hash].[ext]`,
  89. compact: true,
  90. manualChunks: {
  91. vue: [ 'vue', 'vue-router', 'pinia' ],
  92. echarts: [ 'echarts' ],
  93. elementPlus: ['element-plus'],
  94. }
  95. // manualChunks(id) {
  96. // if (id.includes('node_modules')) {
  97. // return 'vendor';
  98. // }
  99. // }
  100. }
  101. }
  102. },
  103. css: { preprocessorOptions: { css: { charset: false } } },
  104. define: {
  105. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  106. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  107. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  108. __VERSION__: JSON.stringify(process.env.npm_package_version)
  109. }
  110. };
  111. });
  112. export default viteConfig;