vite.config.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import vue from '@vitejs/plugin-vue';
  2. import { resolve } from 'path';
  3. import { defineConfig, loadEnv, ConfigEnv } from 'vite';
  4. import vueSetupExtend from 'vite-plugin-vue-setup-extend';
  5. import vueJsx from '@vitejs/plugin-vue-jsx';
  6. import AutoImport from 'unplugin-auto-import/vite';
  7. import compression from 'vite-plugin-compression';
  8. import Components from 'unplugin-vue-components/vite';
  9. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
  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. };
  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. AutoImport({
  26. imports: ['vue', 'vue-router', 'pinia'],
  27. // resolvers: [ElementPlusResolver()],
  28. }),
  29. compression({
  30. algorithm: 'gzip', // 使用 gzip 压缩
  31. ext: '.gz', // 输出的文件扩展名
  32. threshold: 10240, // 只有大小大于该值的资源会被压缩(默认 10KB)
  33. deleteOriginFile: false, // 是否删除原始未压缩的文件
  34. }),
  35. // Components({
  36. // resolvers: [ElementPlusResolver()],
  37. // }),
  38. ],
  39. root: process.cwd(),
  40. resolve: { alias },
  41. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  42. optimizeDeps: {
  43. include: ['element-plus/es/locale/lang/zh-cn', 'element-plus/es/locale/lang/en', 'element-plus/es/locale/lang/zh-tw'],
  44. },
  45. server: {
  46. host: '0.0.0.0',
  47. port: env.VITE_PORT as unknown as number,
  48. open: false,
  49. hmr: true,
  50. proxy: {
  51. '/gitee': {
  52. target: 'https://gitee.com',
  53. ws: true,
  54. changeOrigin: true,
  55. rewrite: (path) => path.replace(/^\/gitee/, ''),
  56. },
  57. },
  58. },
  59. build: {
  60. outDir: 'dist',
  61. chunkSizeWarningLimit: 1500,
  62. rollupOptions: {
  63. output: {
  64. entryFileNames: `assets/[name].[hash].js`,
  65. chunkFileNames: `assets/[name].[hash].js`,
  66. assetFileNames: `assets/[name].[hash].[ext]`,
  67. compact: true,
  68. manualChunks: {
  69. vue: ['vue', 'vue-router', 'pinia'],
  70. elementPlus: ['element-plus'],
  71. echarts: ['echarts'],
  72. },
  73. },
  74. },
  75. },
  76. css: { preprocessorOptions: { css: { charset: false } } },
  77. define: {
  78. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  79. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  80. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  81. __VERSION__: JSON.stringify(process.env.npm_package_version),
  82. },
  83. };
  84. });
  85. export default viteConfig;