vite.config.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 { VitePWA } from 'vite-plugin-pwa'
  7. import { resolve, dirname } from 'node:path'
  8. import { readFileSync } from 'node:fs'
  9. import { fileURLToPath } from 'node:url'
  10. import svgIconsPlugin from './app/frontend/build/iconsPlugin.mjs'
  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 plugins = [
  21. VuePlugin({
  22. template: {
  23. compilerOptions: {
  24. nodeTransforms:
  25. isTesting || !!process.env.VITE_TEST_MODE
  26. ? []
  27. : [require('./app/frontend/build/transforms/transformTestId.js')],
  28. },
  29. },
  30. }),
  31. svgIconsPlugin(),
  32. ]
  33. // Ruby plugin is not needed inside of the vitest context and has some side effects.
  34. if (!isTesting || isBuild) {
  35. const { default: RubyPlugin } = require('vite-plugin-ruby')
  36. const ManualChunks = require('./app/frontend/build/manualChunks.js')
  37. plugins.push(RubyPlugin())
  38. plugins.push(
  39. ...VitePWA({
  40. // should be generated on ruby side
  41. manifest: false,
  42. registerType: 'prompt',
  43. srcDir: 'apps/mobile/sw',
  44. filename: 'sw.ts',
  45. includeManifestIcons: false,
  46. injectRegister: null,
  47. strategies: 'injectManifest',
  48. }),
  49. )
  50. plugins.push(ManualChunks())
  51. }
  52. let https = false
  53. // vite-ruby controlls this variable, it's either "true" or "false"
  54. if (process.env.VITE_RUBY_HTTPS === 'true') {
  55. const SSL_CERT = readFileSync(resolve(SSL_PATH, 'localhost.crt'))
  56. const SSL_KEY = readFileSync(resolve(SSL_PATH, 'localhost.key'))
  57. https = {
  58. cert: SSL_CERT,
  59. key: SSL_KEY,
  60. }
  61. }
  62. let publicDir
  63. if (!isBuild) {
  64. publicDir = resolve(dir, 'public')
  65. } else if (isStory) {
  66. publicDir = resolve(dir, 'app/frontend/public-build')
  67. }
  68. return {
  69. publicDir,
  70. esbuild: {
  71. target: isTesting ? 'esnext' : tsconfig.compilerOptions.target,
  72. },
  73. resolve: {
  74. alias: {
  75. '^vue-easy-lightbox$':
  76. 'vue-easy-lightbox/dist/external-css/vue-easy-lightbox.esm.min.js',
  77. },
  78. },
  79. server: {
  80. https,
  81. watch: {
  82. ignored: isTesting
  83. ? []
  84. : [
  85. '**/*.spec.*',
  86. '**/__tests__/**/*',
  87. 'app/frontend/tests/**/*',
  88. '!app/frontend/**',
  89. ],
  90. },
  91. },
  92. define: {
  93. VITE_TEST_MODE: !!process.env.VITEST || !!process.env.VITE_TEST_MODE,
  94. },
  95. test: {
  96. globals: true,
  97. // narrowing down test folder speeds up fast-glob in Vitest
  98. dir: 'app/frontend',
  99. setupFiles: ['app/frontend/tests/vitest.setup.ts'],
  100. environment: 'jsdom',
  101. clearMocks: true,
  102. css: false,
  103. testTimeout: process.env.CI ? 30_000 : 5_000,
  104. deps: {
  105. // TODO remove after https://github.com/ueberdosis/tiptap/pull/3521 is merged
  106. inline: ['@tiptap/extension-mention'],
  107. },
  108. onConsoleLog(log) {
  109. if (log.includes('Not implemented: navigation')) return false
  110. },
  111. },
  112. plugins,
  113. }
  114. })