vite.config.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (C) 2012-2024 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 { homedir } from 'os'
  11. import svgIconsPlugin from './app/frontend/build/iconsPlugin.mjs'
  12. import ManualChunksPlugin from './app/frontend/build/manualChunks.mjs'
  13. import tsconfig from './tsconfig.base.json' assert { type: 'json' }
  14. const dir = dirname(fileURLToPath(import.meta.url))
  15. const SSL_PATH = resolve(homedir(), '.local/state/localhost.rb')
  16. // eslint-disable-next-line sonarjs/cognitive-complexity
  17. export default defineConfig(({ mode, command }) => {
  18. const isTesting = ['test', 'cypress'].includes(mode)
  19. const isBuild = command === 'build'
  20. const require = createRequire(import.meta.url)
  21. const plugins = [
  22. VuePlugin({
  23. template: {
  24. compilerOptions: {
  25. nodeTransforms:
  26. isTesting || !!process.env.VITE_TEST_MODE
  27. ? []
  28. : [require('./app/frontend/build/transforms/transformTestId.js')],
  29. },
  30. },
  31. }),
  32. svgIconsPlugin(),
  33. ]
  34. if (!isTesting || isBuild) {
  35. // Ruby plugin is not needed inside of the vitest context and has some side effects.
  36. const { default: RubyPlugin } = require('vite-plugin-ruby')
  37. plugins.push(RubyPlugin())
  38. plugins.push(
  39. ...VitePWA({
  40. disable: isTesting || !!process.env.VITE_TEST_MODE,
  41. // should be generated on ruby side
  42. manifest: false,
  43. registerType: 'prompt',
  44. srcDir: 'apps/mobile/sw',
  45. filename: 'sw.ts',
  46. includeManifestIcons: false,
  47. injectRegister: null,
  48. strategies: 'injectManifest',
  49. }),
  50. )
  51. plugins.push(ManualChunksPlugin())
  52. }
  53. let https = false
  54. // vite-ruby controlls this variable, it's either "true" or "false"
  55. if (process.env.VITE_RUBY_HTTPS === 'true') {
  56. const SSL_CERT = readFileSync(resolve(SSL_PATH, 'localhost.crt'))
  57. const SSL_KEY = readFileSync(resolve(SSL_PATH, 'localhost.key'))
  58. https = {
  59. cert: SSL_CERT,
  60. key: SSL_KEY,
  61. }
  62. }
  63. let publicDir
  64. if (!isBuild) {
  65. publicDir = resolve(dir, 'public')
  66. }
  67. return {
  68. publicDir,
  69. esbuild: {
  70. target: isTesting ? 'esnext' : tsconfig.compilerOptions.target,
  71. },
  72. resolve: {
  73. alias: {
  74. '^vue-easy-lightbox$':
  75. 'vue-easy-lightbox/dist/external-css/vue-easy-lightbox.esm.min.js',
  76. },
  77. },
  78. server: {
  79. https,
  80. watch: {
  81. ignored: isTesting
  82. ? []
  83. : [
  84. '**/*.spec.*',
  85. '**/__tests__/**/*',
  86. (path) =>
  87. !path.includes('app/frontend') ||
  88. path.includes('frontend/tests'),
  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. unstubGlobals: true,
  105. onConsoleLog(log) {
  106. if (log.includes('Not implemented: navigation')) return false
  107. },
  108. },
  109. plugins,
  110. }
  111. })