vite.config.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 AutoImport from 'unplugin-auto-import/vite';
  6. import topLevelAwait from 'vite-plugin-top-level-await';
  7. import { createStyleImportPlugin, VxeTableResolve } from 'vite-plugin-style-import';
  8. import viteCompression from 'vite-plugin-compression';
  9. // @ts-ignore
  10. import { svgBuilder } from '/@/components/IconSelector/index';
  11. const pathResolve = (dir: string) => {
  12. return resolve(__dirname, '.', dir);
  13. };
  14. const alias: Record<string, string> = {
  15. '/@': pathResolve('./src/'),
  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(), // Vue 插件
  23. svgBuilder('./src/assets/icons/'), // 将 SVG 文件转换成 Vue 组件
  24. vueSetupExtend(), // setup语法糖增强插件
  25. AutoImport({
  26. imports: ['vue', 'vue-router', 'pinia'], // 自动导入的依赖库数组
  27. dts: './auto-imports.d.ts', // 自动导入类型定义文件路径
  28. }),
  29. createStyleImportPlugin({
  30. resolves: [VxeTableResolve()], // 配置vxetable 按需加载
  31. }),
  32. topLevelAwait({
  33. promiseExportName: '__tla', // TLA Promise 变量名
  34. promiseImportName: (i) => `__tla_${i}`, // TLA Promise 导入名
  35. }),
  36. viteCompression({
  37. deleteOriginFile: false, // 压缩后删除原来的文件
  38. }),
  39. ],
  40. root: process.cwd(), // 项目根目录
  41. resolve: { alias }, // 路径别名配置
  42. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  43. optimizeDeps: {
  44. include: ['element-plus/lib/locale/lang/zh-cn', 'element-plus/lib/locale/lang/en'],
  45. },
  46. server: {
  47. host: '0.0.0.0', // 服务器地址
  48. port: env.VITE_PORT as unknown as number, // 服务器端口号
  49. open: env.VITE_OPEN === 'true', // 是否自动打开浏览器
  50. hmr: true, // 启用热更新
  51. proxy: {
  52. '/api/gen': {
  53. //单体架构下特殊处理代码生成模块代理
  54. target: env.VITE_IS_MICRO === 'true' ? env.VITE_ADMIN_PROXY_PATH : env.VITE_GEN_PROXY_PATH,
  55. changeOrigin: true,
  56. rewrite: (path) => path.replace(/^\/api/, ''),
  57. },
  58. '/api': {
  59. target: env.VITE_ADMIN_PROXY_PATH, // 目标服务器地址
  60. ws: true, // 是否启用 WebSocket
  61. changeOrigin: true, // 是否修改请求头中的 Origin 字段
  62. rewrite: (path) => path.replace(/^\/api/, ''),
  63. },
  64. '/v1/kxs': {
  65. target: env.VITE_ADMIN_PROXY_PATH, // 目标服务器地址
  66. ws: true, // 是否启用 WebSocket
  67. changeOrigin: true
  68. },
  69. '^/ws/info/.*': {
  70. target: env.VITE_ADMIN_PROXY_PATH, // 目标服务器地址
  71. ws: true, // 是否启用 WebSocket
  72. changeOrigin: true,
  73. },
  74. },
  75. },
  76. build: {
  77. outDir: 'dist', // 打包输出目录
  78. chunkSizeWarningLimit: 1500, // 代码分包阈值
  79. rollupOptions: {
  80. output: {
  81. entryFileNames: `assets/[name].[hash].js`,
  82. chunkFileNames: `assets/[name].[hash].js`,
  83. assetFileNames: `assets/[name].[hash].[ext]`,
  84. compact: true,
  85. manualChunks: {
  86. vue: ['vue', 'vue-router', 'pinia'],
  87. echarts: ['echarts'],
  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. __NEXT_NAME__: JSON.stringify(process.env.npm_package_name),
  99. },
  100. };
  101. });
  102. export default viteConfig;