vite.config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. },
  57. },
  58. define: {
  59. // 为某些依赖(例如 browser-crypto)提供全局对象
  60. // globalThis 同时兼容主页面、Web Worker 和 SSR 构建;window 会让 module Worker
  61. // 在 Vite 注入 env.mjs 时直接抛出 ReferenceError。
  62. global: 'globalThis',
  63. },
  64. optimizeDeps: {
  65. include: [
  66. '@arco-design/color',
  67. '@codemirror/lang-java',
  68. '@codemirror/lang-javascript',
  69. 'codemirror',
  70. '@codemirror/lang-sql',
  71. '@codemirror/lang-xml',
  72. '@codemirror/theme-one-dark',
  73. '@form-create/designer',
  74. // 表单设计器通过懒加载页面引入该语言包;显式预构建,避免首次进入设计页时
  75. // Vite 二次优化依赖并使浏览器中的 element-plus 旧 hash 返回 504。
  76. '@form-create/designer/locale/zh-cn.es',
  77. '@form-create/element-ui',
  78. '@stomp/stompjs',
  79. '@vicons/ionicons5',
  80. '@vicons/utils',
  81. '@vueuse/core',
  82. 'axios',
  83. 'bpmn-js/lib/Modeler',
  84. 'bpmn-js/lib/NavigatedViewer',
  85. 'crypto-js',
  86. 'dagre',
  87. 'dayjs',
  88. 'diagram-js/lib/draw/BaseRenderer',
  89. 'echarts',
  90. 'element-plus',
  91. 'highlight.js',
  92. 'inherits-browser',
  93. 'jsencrypt-ext',
  94. 'lodash-es',
  95. 'marked',
  96. 'md5',
  97. 'naive-ui',
  98. 'pinia',
  99. 'pinia-plugin-persistedstate',
  100. 'sm-crypto',
  101. 'sockjs-client',
  102. 'tiny-svg',
  103. 'vuedraggable',
  104. 'vue3-intro-step',
  105. 'vue3-slide-verify',
  106. ],
  107. },
  108. css: {
  109. postcss: {
  110. plugins: [],
  111. },
  112. preprocessorOptions: {
  113. scss: {
  114. additionalData: `@use "@/styles/variables.scss";`,
  115. },
  116. },
  117. },
  118. server: {
  119. port: VITE_HTTP_PORT,
  120. open: false,
  121. proxy: {
  122. // 流程服务代理 - 必须在主代理之前,匹配更具体的路径
  123. [`${VITE_REQUEST_PREFIX}/api/flow`]: {
  124. target: VITE_FLOW_PROXY_TARGET || 'http://localhost:8081',
  125. changeOrigin: true,
  126. secure: false,
  127. rewrite: path => path.replace(/^\/[^/]+/, ''),
  128. },
  129. // 工作台接口由流程服务提供,需在主代理之前匹配
  130. [`${VITE_REQUEST_PREFIX}/api/workspace`]: {
  131. target: VITE_FLOW_PROXY_TARGET || 'http://localhost:8081',
  132. changeOrigin: true,
  133. secure: false,
  134. rewrite: path => path.replace(/^\/[^/]+/, ''),
  135. },
  136. // 主代理 - 匹配所有其他请求
  137. [VITE_REQUEST_PREFIX]: {
  138. secure: false,
  139. target: VITE_HTTP_PROXY_TARGET,
  140. changeOrigin: true,
  141. rewrite: path => path.replace(new RegExp(`^${VITE_REQUEST_PREFIX}`), ''),
  142. configure: (proxy, options) => {
  143. // 配置此项可在响应头中看到请求的真实地址
  144. proxy.on('proxyRes', (proxyRes, req) => {
  145. proxyRes.headers['x-real-url'] = new URL(req.url || '', options.target)?.href || ''
  146. })
  147. },
  148. },
  149. // WebSocket 代理到后端同一服务
  150. '/ws': {
  151. target: VITE_HTTP_PROXY_TARGET,
  152. changeOrigin: true,
  153. ws: true,
  154. secure: false,
  155. },
  156. },
  157. },
  158. build: {
  159. // 关闭源码映射(大幅降低内存)
  160. sourcemap: false,
  161. // 分包优化
  162. chunkSizeWarningLimit: 2000, // chunk 大小警告的限制(单位kb)
  163. },
  164. }
  165. })