vite.config.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. /* eslint-disable no-restricted-imports */
  3. /// <reference types="vitest" />
  4. import { defineConfig } from 'vite'
  5. import RubyPlugin from 'vite-plugin-ruby'
  6. import VuePlugin from '@vitejs/plugin-vue'
  7. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  8. import type { OptimizeOptions } from 'svgo'
  9. import * as path from 'path'
  10. import tsconfig from './tsconfig.base.json'
  11. import TransformTestId from './app/frontend/build/transforms/transformTestId'
  12. import ManualChunks from './app/frontend/build/manual-chunks.mjs'
  13. export default defineConfig(({ mode, command }) => {
  14. const isTesting = ['test', 'storybook', 'cypress'].includes(mode)
  15. const isBuild = command === 'build'
  16. return {
  17. esbuild: {
  18. target: tsconfig.compilerOptions.target,
  19. },
  20. resolve: {
  21. alias: {
  22. '@mobile': path.resolve(__dirname, 'app/frontend/apps/mobile'),
  23. '@shared': path.resolve(__dirname, 'app/frontend/shared'),
  24. '@tests': path.resolve(__dirname, 'app/frontend/tests'),
  25. '@stories': path.resolve(__dirname, 'app/frontend/stories'),
  26. '@cy': path.resolve(__dirname, '.cypress'),
  27. '@': path.resolve(__dirname, 'app/frontend'),
  28. },
  29. },
  30. define: {
  31. VITE_TEST_MODE: !!process.env.VITEST || !!process.env.VITE_TEST_MODE,
  32. },
  33. test: {
  34. globals: true,
  35. setupFiles: ['app/frontend/tests/vitest.setup.ts'],
  36. environment: 'jsdom',
  37. },
  38. plugins: [
  39. // Ruby plugin is not needed inside of the vitest context and has some side effects.
  40. isTesting && !isBuild ? [] : [...RubyPlugin(), ManualChunks()],
  41. VuePlugin({
  42. template: {
  43. compilerOptions: {
  44. nodeTransforms:
  45. isTesting || !!process.env.VITE_TEST_MODE
  46. ? []
  47. : [TransformTestId],
  48. },
  49. },
  50. }),
  51. createSvgIconsPlugin({
  52. // Specify the icon folder to be cached
  53. iconDirs: [
  54. path.resolve(
  55. process.cwd(),
  56. `${
  57. mode === 'storybook' ? '../public' : 'public'
  58. }/assets/images/icons`,
  59. ),
  60. ],
  61. // Specify symbolId format
  62. symbolId: 'icon-[dir]-[name]',
  63. svgoOptions: {
  64. plugins: [
  65. { name: 'preset-default' },
  66. {
  67. name: 'removeAttributesBySelector',
  68. params: {
  69. selectors: [
  70. {
  71. selector: "[fill='#50E3C2']",
  72. attributes: 'fill',
  73. },
  74. // TODO: we need to add a own plugin or add some identifier to the svg files, to add the same functionality
  75. // like we have in the old gulp script (fill='#50E3C2'] + parent fill='none' should be removed).
  76. ],
  77. },
  78. },
  79. {
  80. name: 'convertColors',
  81. params: {
  82. currentColor: /(#BD0FE1|#BD10E0)/,
  83. },
  84. },
  85. ],
  86. } as OptimizeOptions,
  87. }),
  88. ],
  89. }
  90. })