-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
225 lines (217 loc) · 6.56 KB
/
vite.config.ts
File metadata and controls
225 lines (217 loc) · 6.56 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import mdx from '@mdx-js/rollup'
import tailwindcss from '@tailwindcss/vite'
import react from '@vitejs/plugin-react'
// import basicSsl from '@vitejs/plugin-basic-ssl'
import { resolve } from 'node:path'
import { readFileSync } from 'node:fs'
import { execSync } from 'node:child_process'
import { globSync } from 'glob'
import { defineConfig, loadEnv, type Plugin } from 'vite'
import { createMpaPlugin, type Page } from 'vite-plugin-virtual-mpa'
import { PRODUCT } from './src/config/product'
import {
defaultLang,
type Lang,
langs,
languages,
meta,
} from './src/i18n/locales'
import { cacheVersionPlugin } from './src/lib/cache-version-plugin'
import { corsProxyPlugin } from './src/lib/cors-proxy-plugin'
import { oauthProxyPlugin } from './src/lib/oauth-proxy-plugin'
import { getProxyRoutes } from './vite-proxy-routes'
// Read version from package.json
const packageJson = JSON.parse(readFileSync('./package.json', 'utf-8'))
const APP_VERSION = packageJson.version
// Dynamically list all pages with their full paths
const pageFiles = globSync('*/index.{tsx,mdx}', { cwd: './src/pages' })
const pagesList = pageFiles
.map((file) => ({
page: file.replace(/\/index\.(tsx|mdx)$/, ''),
entry: `/src/pages/${file}` as `/${string}`,
}))
.map((e) => ({
...e,
index: e.page === 'Index',
notFound: e.page === 'NotFound',
}))
.map((e) => ({
...e,
name: (lang: Lang) => `${lang}__${e.page.replace(/\//g, '__')}`,
filename: (lang: Lang) =>
`${
e.notFound
? '404'
: `${lang ? `${lang}/` : ''}${e.index ? '' : `${e.page.toLowerCase()}/`}index`
}.html` as `${string}.html`,
title: (lang: Lang) =>
e.index
? PRODUCT.displayName
: `${PRODUCT.displayName} · ${meta[lang || defaultLang]?.[e.page]?.title}`,
}))
// Generate localized pages
const pages = langs.reduce((acc, lang = defaultLang) => {
pagesList.forEach(({ page, name, entry, filename, title }) => {
const isIndex = page === 'Index'
const _title = title(lang)
const _description =
meta[lang || defaultLang]?.[page]?.description ?? PRODUCT.description
console.log(
`Adding page: [${lang}] /${isIndex ? '' : page.toLowerCase()}/`,
{
title: _title,
description: _description,
},
)
acc.push({
name: name(lang),
filename: filename(lang),
entry,
data: {
lang,
language: languages[lang],
title: _title,
description: _description,
},
})
})
return acc
}, [] as Page[])
/**
* Vite plugin that watches YAML files in the public folder and runs
* prepare-config-files.js when they change during development.
*/
function yamlWatchPlugin(): Plugin {
return {
name: 'yaml-watch',
configureServer(server) {
const runPrepareScript = (file: string) => {
console.log(`\n[yaml-watch] Detected change in ${file}`)
console.log('[yaml-watch] Running prepare-config-files.js...')
try {
execSync('node src/scripts/prepare-config-files.js', {
cwd: process.cwd(),
stdio: 'inherit',
})
console.log('[yaml-watch] Config files prepared successfully')
// Trigger a full page reload after the config is updated
server.ws.send({ type: 'full-reload' })
console.log('[yaml-watch] Triggered browser reload\n')
} catch (error) {
console.error(
'[yaml-watch] Failed to run prepare-config-files.js:',
error,
)
}
}
server.watcher.add('public/**/*.yaml')
server.watcher.on('change', (file) => {
if (file.includes('public/') && file.endsWith('.yaml')) {
runPrepareScript(file)
}
})
server.watcher.on('add', (file) => {
if (file.includes('public/') && file.endsWith('.yaml')) {
runPrepareScript(file)
}
})
},
}
}
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
// Load env file based on `mode` in the current working directory.
const env = loadEnv(mode, process.cwd(), '')
return {
define: {
__APP_VERSION__: JSON.stringify(APP_VERSION),
__BUILD_TIME__: JSON.stringify(new Date().toISOString()),
},
plugins: [
react(),
tailwindcss(),
mdx(),
yamlWatchPlugin(),
// basicSsl(),
createMpaPlugin({
htmlMinify: true,
pages,
}) as any,
cacheVersionPlugin(),
corsProxyPlugin(),
// Unified proxy plugin for all OAuth/API routes
// Proxy configuration is defined in src/features/connectors/providers/apps/index.ts
oauthProxyPlugin(getProxyRoutes(env)),
],
optimizeDeps: {
include: ['quickjs-emscripten'],
exclude: ['@jitl/quickjs-wasmfile-release-sync'],
},
assetsInclude: ['**/*.wasm'],
resolve: {
alias: {
'@': resolve(__dirname, './src'),
},
dedupe: [
'react',
'react-dom',
'react-aria',
'react-aria-components',
'@react-aria/interactions',
'@react-aria/focus',
'@react-aria/overlays',
'@react-aria/utils',
'@react-aria/ssr',
'@react-aria/i18n',
'@react-aria/button',
'@react-aria/tooltip',
'@react-stately/tooltip',
'@react-stately/overlays',
'@react-stately/utils',
'@internationalized/string',
],
},
build: {
outDir: 'dist',
assetsDir: 'assets',
sourcemap: false,
rollupOptions: {
output: {
manualChunks: {
vendor: ['react', 'react-dom'],
ui: ['@heroui/react'],
i18n: ['@/i18n'],
// ...Object.fromEntries(
// langs.map((lang) => [`i18n-${lang}`, [`@/i18n/locales/${lang}`]]),
// ),
icons: ['@/components/Icon'],
editor: [
'@/components/MonacoEditor',
'@monaco-editor/react',
'monaco-mermaid',
'monaco-yaml',
],
// features
connectors: ['@/features/connectors'],
live: ['@/features/live'],
local: ['@/features/local-backup'],
meeting: ['@/features/meeting-bot'],
search: ['@/features/search'],
studio: ['@/features/studio'],
sync: ['@/features/sync'],
traces: ['@/features/traces'],
},
},
},
},
server: {
port: 3000,
host: true,
// All proxy routes are handled by the oauthProxyPlugin above
},
preview: {
port: 3000,
host: true,
},
}
})