vite.config.ts 3.3 KB

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