vite.config.ts 3.0 KB

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