vite.config.ts 5.6 KB

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