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