vite.config.ts 3.3 KB

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