-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathvite.config.js
More file actions
303 lines (260 loc) · 8.7 KB
/
Copy pathvite.config.js
File metadata and controls
303 lines (260 loc) · 8.7 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import {defineConfig, transformWithOxc} from 'vite'
import react from '@vitejs/plugin-react'
import {readFileSync, writeFileSync, existsSync} from 'node:fs'
import {resolve, join} from 'node:path'
import {createRequire} from 'node:module'
import {globSync} from 'glob'
const require = createRequire(import.meta.url)
const ini = require('ini')
const matecatConfig = ini.parse(readFileSync('./inc/config.ini', 'utf-8'))
// Single source of truth: template name → Vite entry names
const entryGroups = JSON.parse(readFileSync('./public/vite-entries/groups.json', 'utf-8'))
const allEntryNames = [...new Set(Object.values(entryGroups).flat())]
const input = Object.fromEntries(
allEntryNames.map((name) => [name, `public/vite-entries/${name}.js`]),
)
// Load plugin-specific build configs (same pattern as webpack.config.js).
// Each plugin can provide a plugin.vite.config.js that assigns to pluginViteConfig.
const pluginConfigFiles = globSync('./plugins/*/plugin.vite.config.js', {
ignore: './plugins/**/node_modules/**',
})
let pluginConfig = {}
for (const file of pluginConfigFiles) {
const data = readFileSync(file, 'utf-8')
const config = (0, eval)(data)
pluginConfig = {...pluginConfig, ...config}
}
let sentryVitePlugin = null
if (pluginConfig.sentryVitePlugin) {
try {
sentryVitePlugin = require('@sentry/vite-plugin').sentryVitePlugin
} catch {
// @sentry/vite-plugin not installed — open-source repo without Sentry
}
}
// Vite 8 OXC fix: .js files default to lang:'js' (no JSX parsing).
// Pre-transform with lang:'jsx' so builtin:vite-transform doesn't choke.
function jsxInJsPlugin() {
return {
name: 'transform-js-as-jsx',
enforce: 'pre',
async transform(code, id) {
if (/\.js$/.test(id) && !id.includes('node_modules')) {
return transformWithOxc(code, id, {lang: 'jsx'})
}
},
}
}
/**
* Post-build plugin: reads source PHPTAL templates from lib/View/templates/,
* injects Vite asset tags (with TAL nonce attributes), and writes complete
* output templates to lib/View/ — the same location Webpack writes to.
*
* PHP serves the output templates directly. Zero manifest parsing at runtime.
* Tags use tal:attributes="nonce x_nonce_unique_id" so PHPTAL injects the
* per-request CSP nonce at render time.
*/
function htmlTemplatePlugin() {
const NONCE_ATTR = 'tal:attributes="nonce x_nonce_unique_id"'
const BASE = '/public/build/'
const TEMPLATES_DIR = resolve('lib/View/templates')
const OUTPUT_DIR = resolve('lib/View')
function collectDeps(manifest, chunkKey, seen, preloads, styles) {
if (seen.has(chunkKey)) return
seen.add(chunkKey)
const chunk = manifest[chunkKey]
if (!chunk) return
for (const importKey of chunk.imports ?? []) {
collectDeps(manifest, importKey, seen, preloads, styles)
}
if (!chunk.isEntry && chunk.file) {
preloads.push(chunk.file)
}
for (const css of chunk.css ?? []) {
styles.push(css)
}
}
return {
name: 'html-templates',
apply: 'build',
closeBundle() {
const outDir = resolve('public/build')
const manifestPath = join(outDir, '.vite', 'manifest.json')
if (!existsSync(manifestPath)) {
console.warn('[html-templates] No manifest found, skipping.')
return
}
const manifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))
for (const [templateName, entries] of Object.entries(entryGroups)) {
const sourcePath = join(TEMPLATES_DIR, `_${templateName}`)
if (!existsSync(sourcePath)) {
console.warn(
`[html-templates] Source template "_${templateName}" not found, skipping.`,
)
continue
}
let html = readFileSync(sourcePath, 'utf-8')
const outputPath = join(OUTPUT_DIR, templateName)
// Empty entries — just copy template without injection
if (entries.length === 0) {
writeFileSync(outputPath, html)
console.log(`[html-templates] _${templateName} → ${templateName} (copy)`)
continue
}
const scripts = []
const styles = []
const preloads = []
const seen = new Set()
for (const entryName of entries) {
const key = `public/vite-entries/${entryName}.js`
const entry = manifest[key]
if (!entry) {
console.warn(`[html-templates] Entry "${key}" not in manifest.`)
continue
}
for (const importKey of entry.imports ?? []) {
collectDeps(manifest, importKey, seen, preloads, styles)
}
if (entry.file) {
scripts.push(entry.file)
}
for (const css of entry.css ?? []) {
styles.push(css)
}
}
// .php templates use their own nonce mechanism — inject plain tags
const isPhp = templateName.endsWith('.php')
const nonce = isPhp ? '' : ` ${NONCE_ATTR}`
const tags = []
for (const file of [...new Set(preloads)]) {
tags.push(
` <link rel="modulepreload" href="${BASE}${file}"${nonce}/>`,
)
}
for (const file of [...new Set(styles)]) {
tags.push(
` <link rel="stylesheet" href="${BASE}${file}"${nonce}/>`,
)
}
for (const file of scripts) {
tags.push(
` <script type="module" src="${BASE}${file}"${nonce}></script>`,
)
}
const injection =
` <!-- Vite assets (auto-generated) -->\n${tags.join('\n')}\n`
html = html.replace('</body>', injection + '</body>')
writeFileSync(outputPath, html)
console.log(
`[html-templates] _${templateName} → ${templateName} (${tags.length} tags)`,
)
}
},
}
}
const cliHttpHost = matecatConfig.CLI_HTTP_HOST?.replace(/"/g, '') || 'https://dev.matecat.com'
const hostUrl = new URL(cliHttpHost)
export default defineConfig(({mode, command}) => {
const isProd = mode === 'production'
const hasSentry = isProd && sentryVitePlugin && pluginConfig.sentryVitePlugin
if (hasSentry && matecatConfig.BUILD_NUMBER) {
pluginConfig.sentryVitePlugin.release = {
name: matecatConfig.BUILD_NUMBER,
}
console.log('[sentry] release', pluginConfig.sentryVitePlugin.release)
}
return {
plugins: [
jsxInJsPlugin(),
react({
include: /\.(js|jsx)$/,
}),
htmlTemplatePlugin(),
hasSentry && sentryVitePlugin(pluginConfig.sentryVitePlugin),
],
define: {
'process.env._ENV': JSON.stringify(matecatConfig.ENV ?? 'development'),
'process.env.version': JSON.stringify(matecatConfig.BUILD_NUMBER ?? ''),
'process.env.MODE': JSON.stringify(mode),
'process.env.SENTRY_DSN': JSON.stringify(matecatConfig.SENTRY_DSN ?? ''),
global: 'globalThis',
},
resolve: {
extensions: ['.js', '.jsx', '.json'],
},
base: command === 'serve' ? '/' : '/public/build/',
publicDir: false,
build: {
manifest: true,
outDir: 'public/build',
emptyOutDir: true,
sourcemap: hasSentry ? 'hidden' : mode !== 'production',
rolldownOptions: {
input,
output: {
entryFileNames: '[name].[hash].js',
chunkFileNames: '[name].[hash].js',
assetFileNames: 'assets/[name].[hash].[ext]',
codeSplitting: {
groups: [
{
name: 'react-vendor',
test: /node_modules[\\/](react|react-dom|scheduler)/,
priority: 20,
},
{
name: 'editor-vendor',
test: /node_modules[\\/](draft-js|immutable)/,
priority: 15,
},
{
name: 'vendor',
test: /node_modules/,
priority: 10,
},
],
},
},
},
},
optimizeDeps: {
rolldownOptions: {
moduleTypes: {'.js': 'jsx'},
},
},
css: {
devSourcemap: true,
lightningcss: {
errorRecovery: true,
},
preprocessorOptions: {
scss: {
quietDeps: true,
},
},
},
server: {
host: '0.0.0.0',
port: 5173,
strictPort: true,
origin: cliHttpHost,
cors: true,
hmr: {
protocol: hostUrl.protocol === 'https:' ? 'wss' : 'ws',
host: hostUrl.hostname,
clientPort:
parseInt(hostUrl.port) || (hostUrl.protocol === 'https:' ? 443 : 80),
path: '__vite_hmr',
},
watch: {
usePolling: true,
interval: 500,
ignored: [
'**/storage/**',
'**/node_modules/**',
'**/vendor/**',
'**/public/build/**',
],
},
},
}})