vite.config.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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: [
  27. 'vue',
  28. 'vue-router',
  29. 'pinia'
  30. ],
  31. resolvers: [ElementPlusResolver()],
  32. dts: 'src/auto-imports.d.ts', // 生成 TypeScript 声明文件,
  33. }),
  34. Components({
  35. // resolvers: [ElementPlusResolver()],
  36. }),
  37. compression({
  38. algorithm: 'gzip', // 使用 gzip 压缩
  39. ext: '.gz', // 输出的文件扩展名
  40. threshold: 10240, // 只有大小大于该值的资源会被压缩(默认 10KB)
  41. deleteOriginFile: false, // 是否删除原始未压缩的文件
  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: '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. elementPlus: ['element-plus'],
  76. echarts: ['echarts'],
  77. },
  78. },
  79. },
  80. },
  81. css: { preprocessorOptions: { css: { charset: false } } },
  82. define: {
  83. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  84. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  85. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  86. __VERSION__: JSON.stringify(process.env.npm_package_version),
  87. },
  88. };
  89. });
  90. export default viteConfig;