manualChunks.mjs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (C) 2012-2024 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: false,
  26. matcher: (id) => id.includes('frontend/shared/server'),
  27. chunk: 'apollo',
  28. },
  29. {
  30. vendor: true,
  31. matcher: (id) => id.includes('node_modules/lodash-es'),
  32. chunk: 'lodash',
  33. },
  34. {
  35. vendor: true,
  36. matcher: (id) => /node_modules\/@formkit/.test(id),
  37. chunk: 'formkit',
  38. },
  39. {
  40. vendor: true,
  41. matcher: (id) => /node_modules\/@?vue/.test(id),
  42. chunk: 'vue',
  43. },
  44. ]
  45. /**
  46. * @returns {import("vite").Plugin}
  47. */
  48. const ManualChunksPlugin = () => {
  49. const getChunk = splitVendorChunk()
  50. return {
  51. name: 'zammad:manual-chunks',
  52. // eslint-disable-next-line sonarjs/cognitive-complexity
  53. config() {
  54. return {
  55. build: {
  56. rollupOptions: {
  57. output: {
  58. manualChunks(id, api) {
  59. const chunk = getChunk(id, api)
  60. // FieldEditor is a special case, it's a dynamic import with a large dependency
  61. if (!chunk && id.includes('FieldEditor')) {
  62. return
  63. }
  64. if (!chunk) {
  65. for (const { vendor, matcher, chunk } of matchers) {
  66. if (vendor === false && matcher(id)) {
  67. return chunk
  68. }
  69. }
  70. }
  71. if (chunk !== 'vendor') return chunk
  72. for (const { vendor, matcher, chunk } of matchers) {
  73. if (vendor === true && matcher(id, api)) {
  74. return chunk
  75. }
  76. }
  77. return 'vendor'
  78. },
  79. },
  80. },
  81. },
  82. }
  83. },
  84. }
  85. }
  86. export default ManualChunksPlugin