vite.config.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. 'dayjs': [
  38. // 默认导入
  39. ['default', 'dayjs']
  40. ]
  41. },
  42. ],
  43. resolvers: [ElementPlusResolver()],
  44. dts: 'src/auto-imports.d.ts', // 生成 TypeScript 声明文件
  45. }),
  46. Components({
  47. // resolvers: [ElementPlusResolver()],
  48. }),
  49. ],
  50. root: process.cwd(),
  51. resolve: { alias },
  52. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  53. optimizeDeps: {
  54. include: [ 'element-plus/es/locale/lang/zh-cn', 'element-plus/es/locale/lang/en', 'element-plus/es/locale/lang/zh-tw' ]
  55. },
  56. server: {
  57. host: '0.0.0.0',
  58. port: env.VITE_PORT as unknown as number,
  59. open: false,
  60. hmr: true,
  61. proxy: {
  62. '/gitee': {
  63. target: 'https://gitee.com',
  64. ws: true,
  65. changeOrigin: true,
  66. rewrite: (path) => path.replace(/^\/gitee/, '')
  67. }
  68. }
  69. },
  70. build: {
  71. outDir: env.VITE_DIST_PATH || 'dist',
  72. chunkSizeWarningLimit: 1500,
  73. rollupOptions: {
  74. output: {
  75. entryFileNames: `assets/[name].[hash].js`,
  76. chunkFileNames: `assets/[name].[hash].js`,
  77. assetFileNames: `assets/[name].[hash].[ext]`,
  78. compact: true,
  79. manualChunks: {
  80. vue: [ 'vue', 'vue-router', 'pinia' ],
  81. echarts: [ 'echarts' ],
  82. elementPlus: ['element-plus'],
  83. },
  84. // manualChunks(id) {
  85. // if (id.includes('node_modules')) {
  86. // return 'vendor';
  87. // }
  88. // }
  89. }
  90. }
  91. },
  92. css: { preprocessorOptions: { css: { charset: false } } },
  93. define: {
  94. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  95. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  96. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  97. __VERSION__: JSON.stringify(process.env.npm_package_version)
  98. }
  99. };
  100. });
  101. export default viteConfig;