vite.config.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. /* eslint-disable security/detect-non-literal-fs-filename */
  3. import { createRequire } from 'module'
  4. import { defineConfig } from 'vite'
  5. import VuePlugin from '@vitejs/plugin-vue'
  6. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  7. import { VitePWA } from 'vite-plugin-pwa'
  8. import { resolve, dirname } from 'node:path'
  9. import { readFileSync } from 'node:fs'
  10. import { fileURLToPath } from 'node:url'
  11. import tsconfig from './tsconfig.base.json' assert { type: 'json' }
  12. const dir = dirname(fileURLToPath(import.meta.url))
  13. const SSL_PATH = resolve(dir, 'config', 'ssl')
  14. export default defineConfig(({ mode, command }) => {
  15. const isStory = Boolean(process.env.HISTOIRE)
  16. const isTesting = ['test', 'cypress'].includes(mode) || isStory
  17. const isBuild = command === 'build' && !isStory
  18. const require = createRequire(import.meta.url)
  19. const svgPlugin = createSvgIconsPlugin({
  20. // Specify the directory containing all icon assets assorted by sets.
  21. iconDirs: [
  22. resolve(dir, 'app/frontend/shared/components/CommonIcon/assets'),
  23. ],
  24. // Specify symbolId format to include directory as icon set and filename as icon name.
  25. symbolId: 'icon-[dir]-[name]',
  26. svgoOptions: {
  27. plugins: [{ name: 'preset-default' }],
  28. },
  29. })
  30. if (isStory) {
  31. // Patch svg plugin for stories, because it's not working with SSR.
  32. const svgConfigResolved = svgPlugin.configResolved
  33. svgConfigResolved({ command: 'build' })
  34. delete svgPlugin.configResolved
  35. const { load } = svgPlugin
  36. svgPlugin.load = function fakeLoad(id) {
  37. // @ts-expect-error the plugin is not updated
  38. return load?.call(this, id, true)
  39. }
  40. }
  41. const plugins = [
  42. VuePlugin({
  43. template: {
  44. compilerOptions: {
  45. nodeTransforms:
  46. isTesting || !!process.env.VITE_TEST_MODE
  47. ? []
  48. : [require('./app/frontend/build/transforms/transformTestId')],
  49. },
  50. },
  51. }),
  52. svgPlugin,
  53. ]
  54. // Ruby plugin is not needed inside of the vitest context and has some side effects.
  55. if (!isTesting || isBuild) {
  56. const { default: RubyPlugin } = require('vite-plugin-ruby')
  57. // const ManualChunks = require('./app/frontend/build/manualChunks')
  58. plugins.push(RubyPlugin())
  59. plugins.push(
  60. ...VitePWA({
  61. // should be generated on ruby side
  62. manifest: false,
  63. registerType: 'prompt',
  64. srcDir: 'apps/mobile/sw',
  65. filename: 'sw.ts',
  66. includeManifestIcons: false,
  67. injectRegister: null,
  68. strategies: 'injectManifest',
  69. }),
  70. )
  71. // TODO: Disable manual chunks for now, check if it's still neded with Vite 3.0.
  72. // plugins.push(ManualChunks())
  73. }
  74. let https = false
  75. // vite-ruby controlls this variable, it's either "true" or "false"
  76. if (process.env.VITE_RUBY_HTTPS === 'true') {
  77. const SSL_CERT = readFileSync(resolve(SSL_PATH, 'localhost.crt'))
  78. const SSL_KEY = readFileSync(resolve(SSL_PATH, 'localhost.key'))
  79. https = {
  80. cert: SSL_CERT,
  81. key: SSL_KEY,
  82. }
  83. }
  84. return {
  85. esbuild: {
  86. target: tsconfig.compilerOptions.target,
  87. },
  88. resolve: {
  89. alias: {
  90. '@mobile': resolve(dir, 'app/frontend/apps/mobile'),
  91. '@shared': resolve(dir, 'app/frontend/shared'),
  92. '@tests': resolve(dir, 'app/frontend/tests'),
  93. '@stories': resolve(dir, 'app/frontend/stories'),
  94. '@cy': resolve(dir, '.cypress'),
  95. '@': resolve(dir, 'app/frontend'),
  96. '^vue-easy-lightbox$':
  97. 'vue-easy-lightbox/dist/external-css/vue-easy-lightbox.esm.min.js',
  98. },
  99. },
  100. server: {
  101. https,
  102. watch: {
  103. ignored: isTesting
  104. ? []
  105. : ['**/*.spec.*', '**/__tests__/**/*', 'app/frontend/tests/**/*'],
  106. },
  107. },
  108. define: {
  109. VITE_TEST_MODE: !!process.env.VITEST || !!process.env.VITE_TEST_MODE,
  110. },
  111. test: {
  112. globals: true,
  113. // narrowing down test folder speeds up fast-glob in Vitest
  114. dir: 'app/frontend',
  115. setupFiles: ['app/frontend/tests/vitest.setup.ts'],
  116. environment: 'jsdom',
  117. clearMocks: true,
  118. css: false,
  119. testTimeout: 30_000,
  120. deps: {
  121. // TODO remove after https://github.com/ueberdosis/tiptap/pull/3521 is merged
  122. inline: ['@tiptap/extension-mention'],
  123. },
  124. },
  125. plugins,
  126. }
  127. })