-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
39 lines (36 loc) · 1.05 KB
/
Copy pathvite.config.ts
File metadata and controls
39 lines (36 loc) · 1.05 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
import fs from 'node:fs'
import path from 'node:path'
import { defineConfig, type Plugin } from 'vite'
import react from '@vitejs/plugin-react'
function boardPublishPlugin(): Plugin {
return {
name: 'board-publish',
configureServer(server) {
server.middlewares.use('/api/publish', (req, res) => {
if (req.method !== 'POST') {
res.statusCode = 405
res.end()
return
}
let body = ''
req.on('data', (chunk: Buffer) => { body += chunk.toString() })
req.on('end', () => {
try {
const dest = path.resolve(__dirname, 'public/board.json')
fs.writeFileSync(dest, body, 'utf-8')
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ ok: true }))
} catch {
res.statusCode = 500
res.end(JSON.stringify({ ok: false }))
}
})
})
},
}
}
// https://vite.dev/config/
export default defineConfig({
base: './',
plugins: [react(), boardPublishPlugin()],
})