vite.config.ts 2.6 KB

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