vite.config.mjs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // eslint-disable-next-line sonarjs/cognitive-complexity
  15. export default defineConfig(({ mode, command }) => {
  16. const isStory = Boolean(process.env.HISTOIRE)
  17. const isTesting = ['test', 'cypress'].includes(mode) || isStory
  18. const isBuild = command === 'build' && !isStory
  19. const require = createRequire(import.meta.url)
  20. const svgPlugin = createSvgIconsPlugin({
  21. // Specify the directory containing all icon assets assorted by sets.
  22. iconDirs: [
  23. resolve(dir, 'app/frontend/shared/components/CommonIcon/assets'),
  24. ],
  25. // Specify symbolId format to include directory as icon set and filename as icon name.
  26. symbolId: 'icon-[dir]-[name]',
  27. svgoOptions: {
  28. plugins: [{ name: 'preset-default' }],
  29. },
  30. })
  31. if (isStory) {
  32. // Patch svg plugin for stories, because it's not working with SSR.
  33. const svgConfigResolved = svgPlugin.configResolved
  34. svgConfigResolved({ command: 'build' })
  35. delete svgPlugin.configResolved
  36. const { load } = svgPlugin
  37. svgPlugin.load = function fakeLoad(id) {
  38. // @ts-expect-error the plugin is not updated
  39. return load?.call(this, id, true)
  40. }
  41. }
  42. const plugins = [
  43. VuePlugin({
  44. template: {
  45. compilerOptions: {
  46. nodeTransforms:
  47. isTesting || !!process.env.VITE_TEST_MODE
  48. ? []
  49. : [require('./app/frontend/build/transforms/transformTestId.js')],
  50. },
  51. },
  52. }),
  53. svgPlugin,
  54. ]
  55. // Ruby plugin is not needed inside of the vitest context and has some side effects.
  56. if (!isTesting || isBuild) {
  57. const { default: RubyPlugin } = require('vite-plugin-ruby')
  58. const ManualChunks = require('./app/frontend/build/manualChunks.js')
  59. plugins.push(RubyPlugin())
  60. plugins.push(
  61. ...VitePWA({
  62. // should be generated on ruby side
  63. manifest: false,
  64. registerType: 'prompt',
  65. srcDir: 'apps/mobile/sw',
  66. filename: 'sw.ts',
  67. includeManifestIcons: false,
  68. injectRegister: null,
  69. strategies: 'injectManifest',
  70. }),
  71. )
  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. let publicDir
  85. if (!isBuild) {
  86. publicDir = resolve(dir, 'public')
  87. } else if (isStory) {
  88. publicDir = resolve(dir, 'app/frontend/public-build')
  89. }
  90. return {
  91. publicDir,
  92. esbuild: {
  93. target: isTesting ? 'esnext' : tsconfig.compilerOptions.target,
  94. },
  95. resolve: {
  96. alias: {
  97. '^vue-easy-lightbox$':
  98. 'vue-easy-lightbox/dist/external-css/vue-easy-lightbox.esm.min.js',
  99. },
  100. },
  101. server: {
  102. https,
  103. watch: {
  104. ignored: isTesting
  105. ? []
  106. : [
  107. '**/*.spec.*',
  108. '**/__tests__/**/*',
  109. 'app/frontend/tests/**/*',
  110. '!app/frontend/**',
  111. ],
  112. },
  113. },
  114. define: {
  115. VITE_TEST_MODE: !!process.env.VITEST || !!process.env.VITE_TEST_MODE,
  116. },
  117. test: {
  118. globals: true,
  119. // narrowing down test folder speeds up fast-glob in Vitest
  120. dir: 'app/frontend',
  121. setupFiles: ['app/frontend/tests/vitest.setup.ts'],
  122. environment: 'jsdom',
  123. clearMocks: true,
  124. css: false,
  125. testTimeout: process.env.CI ? 30_000 : 5_000,
  126. deps: {
  127. // TODO remove after https://github.com/ueberdosis/tiptap/pull/3521 is merged
  128. inline: ['@tiptap/extension-mention'],
  129. },
  130. onConsoleLog(log) {
  131. if (log.includes('Not implemented: navigation')) return false
  132. },
  133. },
  134. plugins,
  135. }
  136. })