vite.config.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. import { createRequire } from 'module'
  3. import { defineConfig, type ResolvedConfig } from 'vite'
  4. import VuePlugin from '@vitejs/plugin-vue'
  5. import {
  6. createSvgIconsPlugin,
  7. type ViteSvgIconsPlugin,
  8. } from 'vite-plugin-svg-icons'
  9. import path from 'path'
  10. import tsconfig from './tsconfig.base.json'
  11. export default defineConfig(({ mode, command }) => {
  12. const isStory = Boolean(process.env.HISTOIRE)
  13. const isTesting = ['test', 'cypress'].includes(mode) || isStory
  14. const isBuild = command === 'build' && !isStory
  15. const require = createRequire(import.meta.url)
  16. const svgPlugin = createSvgIconsPlugin({
  17. // Specify the icon folder to be cached
  18. iconDirs: [path.resolve(process.cwd(), `public/assets/images/icons`)],
  19. // Specify symbolId format
  20. symbolId: 'icon-[dir]-[name]',
  21. svgoOptions: {
  22. plugins: [
  23. { name: 'preset-default' },
  24. {
  25. name: 'removeAttributesBySelector',
  26. params: {
  27. selectors: [
  28. {
  29. selector: "[fill='#50E3C2']",
  30. attributes: 'fill',
  31. },
  32. // TODO: we need to add a own plugin or add some identifier to the svg files, to add the same functionality
  33. // like we have in the old gulp script (fill='#50E3C2'] + parent fill='none' should be removed).
  34. ],
  35. },
  36. },
  37. {
  38. name: 'convertColors',
  39. params: {
  40. currentColor: /(#BD0FE1|#BD10E0)/,
  41. },
  42. },
  43. ],
  44. } as ViteSvgIconsPlugin['svgoOptions'],
  45. })
  46. if (isStory) {
  47. svgPlugin.configResolved?.({ command: 'build' } as ResolvedConfig)
  48. delete svgPlugin.configResolved
  49. const { load } = svgPlugin
  50. svgPlugin.load = function fakeLoad(id) {
  51. // @ts-expect-error the plugin is not updated
  52. return load?.call(this, id, true)
  53. }
  54. }
  55. const plugins = [
  56. VuePlugin({
  57. template: {
  58. compilerOptions: {
  59. nodeTransforms:
  60. isTesting || !!process.env.VITE_TEST_MODE
  61. ? []
  62. : [require('./app/frontend/build/transforms/transformTestId')],
  63. },
  64. },
  65. }),
  66. svgPlugin,
  67. ]
  68. // Ruby plugin is not needed inside of the vitest context and has some side effects.
  69. if (!isTesting || isBuild) {
  70. const { default: RubyPlugin } = require('vite-plugin-ruby')
  71. const ManualChunks = require('./app/frontend/build/manualChunks')
  72. plugins.push(RubyPlugin())
  73. plugins.push(ManualChunks())
  74. }
  75. return {
  76. esbuild: {
  77. target: tsconfig.compilerOptions.target,
  78. },
  79. resolve: {
  80. alias: {
  81. '@mobile': path.resolve(__dirname, 'app/frontend/apps/mobile'),
  82. '@shared': path.resolve(__dirname, 'app/frontend/shared'),
  83. '@tests': path.resolve(__dirname, 'app/frontend/tests'),
  84. '@stories': path.resolve(__dirname, 'app/frontend/stories'),
  85. '@cy': path.resolve(__dirname, '.cypress'),
  86. '@': path.resolve(__dirname, 'app/frontend'),
  87. '^vue-easy-lightbox$':
  88. 'vue-easy-lightbox/dist/external-css/vue-easy-lightbox.esm.min.js',
  89. },
  90. },
  91. server: {
  92. watch: {
  93. ignored: isTesting
  94. ? []
  95. : ['**/*.spec.*', '**/__tests__/**/*', 'app/frontend/tests/**/*'],
  96. },
  97. },
  98. define: {
  99. VITE_TEST_MODE: !!process.env.VITEST || !!process.env.VITE_TEST_MODE,
  100. },
  101. test: {
  102. globals: true,
  103. // narrowing down test folder speeds up fast-glob in Vitest
  104. dir: 'app/frontend',
  105. setupFiles: ['app/frontend/tests/vitest.setup.ts'],
  106. environment: 'jsdom',
  107. css: false,
  108. testTimeout: 30_000,
  109. },
  110. plugins,
  111. }
  112. })