vite.config.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. const pathResolve = (dir: string) => {
  10. return resolve(__dirname, '.', dir);
  11. };
  12. const alias: Record<string, string> = {
  13. '/@': pathResolve('./src/'),
  14. '@views': pathResolve('./src/views'),
  15. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
  16. '@dvaformflow': pathResolve('./src/viwes/plugins/dvaadmin_form_flow/src/')
  17. };
  18. const viteConfig = defineConfig((mode: ConfigEnv) => {
  19. const env = loadEnv(mode.mode, process.cwd());
  20. return {
  21. plugins: [
  22. vue(),
  23. vueJsx(),
  24. // vueSetupExtend()
  25. compression({
  26. algorithm: 'gzip', // 使用 gzip 压缩
  27. ext: '.gz', // 输出的文件扩展名
  28. threshold: 10240, // 只有大小大于该值的资源会被压缩(默认 10KB)
  29. deleteOriginFile: false, // 是否删除原始未压缩的文件
  30. }),
  31. AutoImport({
  32. imports: [
  33. 'vue',
  34. 'vue-router',
  35. 'pinia'
  36. ],
  37. resolvers: [ElementPlusResolver()],
  38. dts: 'src/auto-imports.d.ts', // 生成 TypeScript 声明文件
  39. }),
  40. Components({
  41. resolvers: [ElementPlusResolver()],
  42. }),
  43. ],
  44. root: process.cwd(),
  45. resolve: { alias },
  46. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  47. optimizeDeps: {
  48. include: [ 'element-plus/es/locale/lang/zh-cn', 'element-plus/es/locale/lang/en', 'element-plus/es/locale/lang/zh-tw' ]
  49. },
  50. server: {
  51. host: '0.0.0.0',
  52. port: env.VITE_PORT as unknown as number,
  53. open: false,
  54. hmr: true,
  55. proxy: {
  56. '/gitee': {
  57. target: 'https://gitee.com',
  58. ws: true,
  59. changeOrigin: true,
  60. rewrite: (path) => path.replace(/^\/gitee/, '')
  61. }
  62. }
  63. },
  64. build: {
  65. outDir: env.VITE_DIST_PATH || 'dist',
  66. chunkSizeWarningLimit: 1500,
  67. rollupOptions: {
  68. output: {
  69. entryFileNames: `assets/[name].[hash].js`,
  70. chunkFileNames: `assets/[name].[hash].js`,
  71. assetFileNames: `assets/[name].[hash].[ext]`,
  72. compact: true,
  73. manualChunks: {
  74. vue: [ 'vue', 'vue-router', 'pinia' ],
  75. echarts: [ 'echarts' ],
  76. elementPlus: ['element-plus'],
  77. },
  78. // manualChunks(id) {
  79. // if (id.includes('node_modules')) {
  80. // return 'vendor';
  81. // }
  82. // }
  83. }
  84. }
  85. },
  86. css: { preprocessorOptions: { css: { charset: false } } },
  87. define: {
  88. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  89. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  90. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  91. __VERSION__: JSON.stringify(process.env.npm_package_version)
  92. }
  93. };
  94. });
  95. export default viteConfig;