manualChunks.mjs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { splitVendorChunk } from 'vite'
  3. const matchers = [
  4. {
  5. vendor: false,
  6. matcher: (id) => id.includes('commonjsHelpers.js'),
  7. chunk: 'commonjsHelpers',
  8. },
  9. {
  10. vendor: false,
  11. matcher: (id) => id.includes('vite/preload-helper'),
  12. chunk: 'vite',
  13. },
  14. {
  15. vendor: false,
  16. matcher: (id) => id.endsWith('/routes.ts'),
  17. chunk: 'routes',
  18. },
  19. {
  20. vendor: true,
  21. matcher: (id) => id.includes('@vue/apollo'),
  22. chunk: 'apollo',
  23. },
  24. {
  25. vendor: true,
  26. matcher: (id) => id.includes('@vuepic/vue-datepicker'),
  27. chunk: 'datepicker',
  28. },
  29. {
  30. vendor: false,
  31. matcher: (id) => id.includes('frontend/shared/server'),
  32. chunk: 'apollo',
  33. },
  34. {
  35. vendor: true,
  36. matcher: (id) => id.includes('node_modules/lodash-es'),
  37. chunk: 'lodash',
  38. },
  39. {
  40. vendor: true,
  41. matcher: (id) => id.includes('node_modules/date-fns'),
  42. chunk: 'date',
  43. },
  44. {
  45. vendor: true,
  46. matcher: (id) => /node_modules\/@formkit/.test(id),
  47. chunk: 'formkit',
  48. },
  49. {
  50. vendor: true,
  51. matcher: (id) => /node_modules\/@?vue/.test(id),
  52. chunk: 'vue',
  53. },
  54. ]
  55. /**
  56. * @returns {import("vite").Plugin}
  57. */
  58. const ManualChunksPlugin = () => {
  59. const getChunk = splitVendorChunk()
  60. return {
  61. name: 'zammad:manual-chunks',
  62. // eslint-disable-next-line sonarjs/cognitive-complexity
  63. config() {
  64. return {
  65. build: {
  66. rollupOptions: {
  67. output: {
  68. manualChunks(id, api) {
  69. const chunk = getChunk(id, api)
  70. // FieldEditor is a special case, it's a dynamic import with a large dependency
  71. if (!chunk && id.includes('FieldEditor')) {
  72. return
  73. }
  74. if (!chunk) {
  75. for (const { vendor, matcher, chunk } of matchers) {
  76. if (vendor === false && matcher(id)) {
  77. return chunk
  78. }
  79. }
  80. }
  81. if (chunk !== 'vendor') return chunk
  82. for (const { vendor, matcher, chunk } of matchers) {
  83. if (vendor === true && matcher(id, api)) {
  84. return chunk
  85. }
  86. }
  87. return 'vendor'
  88. },
  89. },
  90. },
  91. },
  92. }
  93. },
  94. }
  95. }
  96. export default ManualChunksPlugin