vite.config.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import path from 'node:path'
  2. import Vue from '@vitejs/plugin-vue'
  3. import VueJsx from '@vitejs/plugin-vue-jsx'
  4. import Unocss from 'unocss/vite'
  5. import AutoImport from 'unplugin-auto-import/vite'
  6. import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
  7. import Components from 'unplugin-vue-components/vite'
  8. import VueRouter from 'unplugin-vue-router/vite'
  9. import { defineConfig, loadEnv } from 'vite'
  10. import removeNoMatch from 'vite-plugin-router-warn'
  11. import VueDevTools from 'vite-plugin-vue-devtools'
  12. import { pluginIcons, pluginPagePathes } from './build/plugin-isme'
  13. export default defineConfig(({ mode, command }) => {
  14. const viteEnv = loadEnv(mode, process.cwd())
  15. const { VITE_HTTP_PORT, VITE_REQUEST_PREFIX, VITE_PUBLIC_PATH, VITE_HTTP_PROXY_TARGET, VITE_FLOW_PROXY_TARGET } = viteEnv
  16. const isServe = command === 'serve'
  17. return {
  18. base: VITE_PUBLIC_PATH || '/',
  19. plugins: [
  20. // unplugin-vue-router 必须在 Vue 之前
  21. VueRouter({
  22. routesFolder: [
  23. {
  24. src: 'src/views',
  25. path: '',
  26. // 排除不需要生成路由的文件
  27. exclude: ['**/components/**', '**/api/**', '**/workspace/**'],
  28. },
  29. ],
  30. dts: false,
  31. watch: isServe,
  32. }),
  33. Vue(),
  34. VueJsx(),
  35. ...(isServe ? [VueDevTools()] : []),
  36. Unocss(),
  37. AutoImport({
  38. imports: ['vue', 'vue-router'],
  39. dts: false,
  40. }),
  41. Components({
  42. resolvers: [NaiveUiResolver()],
  43. dts: false,
  44. }),
  45. // 自定义插件,用于生成页面文件的path,并添加到虚拟模块
  46. pluginPagePathes(),
  47. // 自定义插件,用于生成自定义icon,并添加到虚拟模块
  48. pluginIcons(),
  49. // 移除非必要的vue-router动态路由警告: No match found for location with path
  50. removeNoMatch(),
  51. ],
  52. resolve: {
  53. alias: {
  54. '@': path.resolve(process.cwd(), 'src'),
  55. '~': path.resolve(process.cwd()),
  56. // vue3-barcode 的 exports 缺少 default 条件,Vite 7 无法解析,手动指向 UMD 文件
  57. 'vue3-barcode': path.resolve(process.cwd(), 'node_modules/vue3-barcode/dist/vue3-barcode.umd.js'),
  58. },
  59. },
  60. define: {
  61. // 为某些依赖(例如 browser-crypto)提供全局对象
  62. // globalThis 同时兼容主页面、Web Worker 和 SSR 构建;window 会让 module Worker
  63. // 在 Vite 注入 env.mjs 时直接抛出 ReferenceError。
  64. global: 'globalThis',
  65. },
  66. optimizeDeps: {
  67. include: [
  68. '@arco-design/color',
  69. '@codemirror/lang-java',
  70. '@codemirror/lang-javascript',
  71. 'codemirror',
  72. '@codemirror/lang-sql',
  73. '@codemirror/lang-xml',
  74. '@codemirror/theme-one-dark',
  75. '@form-create/designer',
  76. // 表单设计器通过懒加载页面引入该语言包;显式预构建,避免首次进入设计页时
  77. // Vite 二次优化依赖并使浏览器中的 element-plus 旧 hash 返回 504。
  78. '@form-create/designer/locale/zh-cn.es',
  79. '@form-create/element-ui',
  80. '@stomp/stompjs',
  81. '@vicons/ionicons5',
  82. '@vicons/utils',
  83. '@vueuse/core',
  84. 'axios',
  85. 'bpmn-js/lib/Modeler',
  86. 'bpmn-js/lib/NavigatedViewer',
  87. 'crypto-js',
  88. 'dagre',
  89. 'dayjs',
  90. 'diagram-js/lib/draw/BaseRenderer',
  91. 'echarts',
  92. 'element-plus',
  93. 'highlight.js',
  94. 'inherits-browser',
  95. 'jsencrypt-ext',
  96. 'lodash-es',
  97. 'marked',
  98. 'md5',
  99. 'naive-ui',
  100. 'pinia',
  101. 'pinia-plugin-persistedstate',
  102. 'sm-crypto',
  103. 'sockjs-client',
  104. 'tiny-svg',
  105. 'vuedraggable',
  106. 'vue3-intro-step',
  107. 'vue3-slide-verify',
  108. ],
  109. },
  110. css: {
  111. postcss: {
  112. plugins: [],
  113. },
  114. preprocessorOptions: {
  115. scss: {
  116. additionalData: `@use "@/styles/variables.scss";`,
  117. },
  118. },
  119. },
  120. server: {
  121. port: VITE_HTTP_PORT,
  122. open: false,
  123. proxy: {
  124. // 流程服务代理 - 必须在主代理之前,匹配更具体的路径
  125. [`${VITE_REQUEST_PREFIX}/api/flow`]: {
  126. target: VITE_FLOW_PROXY_TARGET || 'http://localhost:8081',
  127. changeOrigin: true,
  128. secure: false,
  129. rewrite: path => path.replace(/^\/[^/]+/, ''),
  130. },
  131. // 工作台接口由流程服务提供,需在主代理之前匹配
  132. [`${VITE_REQUEST_PREFIX}/api/workspace`]: {
  133. target: VITE_FLOW_PROXY_TARGET || 'http://localhost:8081',
  134. changeOrigin: true,
  135. secure: false,
  136. rewrite: path => path.replace(/^\/[^/]+/, ''),
  137. },
  138. // 主代理 - 匹配所有其他请求
  139. [VITE_REQUEST_PREFIX]: {
  140. secure: false,
  141. target: VITE_HTTP_PROXY_TARGET,
  142. changeOrigin: true,
  143. rewrite: path => path.replace(new RegExp(`^${VITE_REQUEST_PREFIX}`), ''),
  144. configure: (proxy, options) => {
  145. // 配置此项可在响应头中看到请求的真实地址
  146. proxy.on('proxyRes', (proxyRes, req) => {
  147. proxyRes.headers['x-real-url'] = new URL(req.url || '', options.target)?.href || ''
  148. })
  149. },
  150. },
  151. // WebSocket 代理到后端同一服务
  152. '/ws': {
  153. target: VITE_HTTP_PROXY_TARGET,
  154. changeOrigin: true,
  155. ws: true,
  156. secure: false,
  157. },
  158. },
  159. },
  160. build: {
  161. // 关闭源码映射(大幅降低内存)
  162. sourcemap: false,
  163. // 分包优化
  164. chunkSizeWarningLimit: 2000, // chunk 大小警告的限制(单位kb)
  165. },
  166. }
  167. })