vite.config.mjs 3.8 KB

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