vite.config.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 '/@/rc-common/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('./public/rc-common/icons/'), // 将 SVG 文件转换成 Vue 组件
  24. vueSetupExtend(), // setup语法糖增强插件
  25. AutoImport({
  26. imports: ['vue', 'vue-router', 'pinia'], // 自动导入的依赖库数组
  27. dts: './config/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/es/locale/lang/zh-cn', 'element-plus/es/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. target: "https://cmc.fmzgo.com/api", // 目标服务器地址
  61. ws: true, // 是否启用 WebSocket
  62. changeOrigin: true, // 是否修改请求头中的 Origin 字段
  63. rewrite: (path, req) =>{
  64. return path.replace(/^\/api/, '')
  65. },
  66. },
  67. '^/ws/info/.*': {
  68. target: env.VITE_ADMIN_PROXY_PATH, // 目标服务器地址
  69. ws: true, // 是否启用 WebSocket
  70. changeOrigin: true,
  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. echarts: ['echarts'],
  86. },
  87. },
  88. },
  89. },
  90. css: {
  91. preprocessorOptions: {
  92. css: { charset: false },
  93. scss: {
  94. // 使用Sass现代API
  95. api: 'modern-compiler'
  96. }
  97. }
  98. },
  99. define: {
  100. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  101. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  102. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  103. __VERSION__: JSON.stringify(process.env.npm_package_version),
  104. __NEXT_NAME__: JSON.stringify(process.env.npm_package_name),
  105. },
  106. };
  107. });
  108. export default viteConfig;