vite.config.ts 3.3 KB

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