vite.config.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { defineConfig, loadEnv } from "vite"
  2. import { resolve } from "path"
  3. import createVitePlugins from "./vite/plugins"
  4. //import { visualizer } from "rollup-plugin-visualizer"
  5. // https://vitejs.dev/config/
  6. export default defineConfig(({ mode, command }) => {
  7. const env = loadEnv(mode, process.cwd())
  8. return {
  9. // 部署生产环境和开发环境下的URL。
  10. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  11. // 例如 https://www.vber.net/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.vber.net/admin/,则设置 baseUrl 为 /admin/。
  12. base: env.VITE_APP_CONTEXT_PATH,
  13. plugins: [
  14. ...createVitePlugins(env, command === "build")
  15. //,visualizer()
  16. ],
  17. build: {
  18. minify: "terser", // 启用 terser 压缩
  19. terserOptions: {
  20. compress: {
  21. pure_funcs: ["console.log"], // 只删除 console.log
  22. //drop_console: true, // 删除所有 console
  23. drop_debugger: true // 删除 debugger
  24. }
  25. },
  26. rollupOptions: {
  27. output: {
  28. // 最小化拆分包
  29. manualChunks(id) {
  30. if (id.includes("node_modules")) {
  31. if (id.includes("node_modules/element-plus")) {
  32. return "vb_plugin_element-plus"
  33. }
  34. if (id.includes("node_modules/nprogress")) {
  35. return "vb_plugin_nprogress" //单独打包
  36. }
  37. if (id.includes("node_modules/quill")) {
  38. return "vb_plugin_quill"
  39. }
  40. if (id.includes("node_modules/echarts")) {
  41. return "vb_plugin_echarts"
  42. }
  43. if (id.includes("node_modules/lodash")) {
  44. return "vb_plugin_lodash"
  45. }
  46. // 通过拆分包的方式将所有来自node_modules的模块打包到单独的chunk中
  47. return "vb_plugin_vendor"
  48. }
  49. // 将pinia的全局库单独打包,避免和页面一起打包造成资源重复引入
  50. if (id.includes(resolve(__dirname, "/src/stores/index.ts"))) {
  51. return "vb_store"
  52. }
  53. if (id.includes("/src/components/") && id.includes("/Vb")) {
  54. return "vb_components"
  55. }
  56. if (id.includes("/src/core/utils/") && id.includes("/Vb")) {
  57. return "vb_utils"
  58. }
  59. },
  60. // 设置chunk的文件名格式
  61. chunkFileNames: (chunkInfo) => {
  62. if (chunkInfo.isEntry) {
  63. return "index.[hash].js"
  64. }
  65. if (chunkInfo.name === "index") {
  66. const facadeModuleId = chunkInfo.facadeModuleId
  67. ? chunkInfo.facadeModuleId.split("/")
  68. : []
  69. const fileName = facadeModuleId[facadeModuleId.length - 2] || "[name]"
  70. // 根据chunk的facadeModuleId(入口模块的相对路径)生成chunk的文件名
  71. return `js/vb_view_${fileName}.[hash].js`
  72. }
  73. return `js/[name].[hash].js`
  74. },
  75. // 设置入口文件的文件名格式
  76. entryFileNames: "js/[name].[hash].js",
  77. // 设置静态资源文件的文件名格式
  78. assetFileNames(assetInfo) {
  79. if (assetInfo.name?.endsWith(".css")) {
  80. return "css/[name].[hash].[ext]"
  81. }
  82. const imgArray = [".png", "jpg", ".jpeg", "webp ", ".svg", ".gif"]
  83. if (imgArray.some((v) => assetInfo.name?.endsWith(v))) {
  84. return "img/[name].[hash].[ext]"
  85. }
  86. return "media/asset/[name].[hash:4].[ext]"
  87. }
  88. }
  89. }
  90. },
  91. resolve: {
  92. // https://cn.vitejs.dev/config/#resolve-alias
  93. alias: {
  94. "@a": resolve(__dirname, "./src/api"),
  95. "@s": resolve(__dirname, "./src/stores"),
  96. "@r": resolve(__dirname, "./src/router"),
  97. "@v": resolve(__dirname, "./src/views"),
  98. "@@": resolve(__dirname, "./src/core"),
  99. "@@@": resolve(__dirname, "./src/components"),
  100. "@com": resolve(__dirname, "./src/components"),
  101. "@": resolve(__dirname, "./src"),
  102. "~": resolve(__dirname, "./")
  103. },
  104. // https://cn.vitejs.dev/config/#resolve-extensions
  105. extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"]
  106. },
  107. // vite 相关配置
  108. server: {
  109. port: Number(env.VITE_APP_PORT),
  110. // host: true,
  111. host: "0.0.0.0",
  112. // 热更新
  113. hmr: true,
  114. // 设为 true 时若端口已被占用则会直接退出,而不是尝试下一个可用端口
  115. strictPort: true,
  116. open: true,
  117. proxy: {
  118. [env.VITE_APP_BASE_API]: {
  119. //target: "http://localhost:8080",
  120. target: env.VITE_APP_PROXY_URL,
  121. changeOrigin: true,
  122. rewrite: (path) => path.replace(new RegExp("^" + env.VITE_APP_BASE_API), "")
  123. }
  124. }
  125. },
  126. preview: {
  127. port: 6666,
  128. // host: true,
  129. host: "0.0.0.0",
  130. // 热更新
  131. hmr: true,
  132. // 设为 true 时若端口已被占用则会直接退出,而不是尝试下一个可用端口
  133. strictPort: true,
  134. open: true,
  135. cors: false,
  136. proxy: {
  137. // "/prod-api": {
  138. // target: "http://localhost:6060",
  139. // changeOrigin: true,
  140. // rewrite: (p) => p.replace(/^\/prod-api/, ""),
  141. // }, 1
  142. "/prod-api": {
  143. target: "http://192.168.0.82:6066",
  144. changeOrigin: true,
  145. rewrite: (p) => p.replace(/^\/prod-api/, "/prod-api")
  146. }
  147. }
  148. },
  149. // 预编译
  150. optimizeDeps: {
  151. include: [
  152. "vue",
  153. "vue-router",
  154. "pinia",
  155. "axios",
  156. "@vueuse/core",
  157. "path-to-regexp",
  158. "echarts",
  159. "vue-i18n",
  160. "@vueup/vue-quill",
  161. "bpmn-js/lib/Viewer",
  162. "bpmn-js/lib/Modeler.js",
  163. "bpmn-js-properties-panel",
  164. "min-dash",
  165. "diagram-js/lib/navigation/movecanvas",
  166. "diagram-js/lib/navigation/zoomscroll",
  167. "bpmn-js/lib/features/palette/PaletteProvider",
  168. "bpmn-js/lib/features/context-pad/ContextPadProvider",
  169. "diagram-js/lib/draw/BaseRenderer",
  170. "tiny-svg",
  171. "image-conversion",
  172. "element-plus/es/components/**/css"
  173. ]
  174. }
  175. }
  176. })