-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
73 lines (65 loc) · 2.31 KB
/
vite.config.ts
File metadata and controls
73 lines (65 loc) · 2.31 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
import { existsSync, writeFileSync, mkdirSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, resolve, join } from 'node:path'
import { defineConfig, loadEnv, type Plugin } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import svgr from 'vite-plugin-svgr'
const __dirname = dirname(fileURLToPath(import.meta.url))
function devVaultWriter(): Plugin {
return {
name: 'dev-vault-writer',
configureServer(server) {
server.middlewares.use('/__dev/save', async (req, res) => {
if (req.method !== 'POST') {
res.statusCode = 405
res.end('Method not allowed')
return
}
let body = ''
req.on('data', chunk => { body += chunk })
req.on('end', () => {
try {
const { filename, content } = JSON.parse(body)
if (!filename || typeof content !== 'string') {
res.statusCode = 400
res.end(JSON.stringify({ error: 'filename and content required' }))
return
}
const targetPath = join(__dirname, 'src/content/vault', filename)
mkdirSync(dirname(targetPath), { recursive: true })
writeFileSync(targetPath, content, 'utf8')
res.statusCode = 200
res.end(JSON.stringify({ ok: true, path: targetPath }))
} catch (e) {
res.statusCode = 500
res.end(JSON.stringify({ error: String(e) }))
}
})
})
},
}
}
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
// App entry switch — public default points at App.tsx; private builds set
// VITE_USE_DEVTOOLS=true and (when App.dev.tsx exists locally) get the
// version with the /dev/* route mounted.
const wantsDev = env.VITE_USE_DEVTOOLS === 'true'
const devEntry = resolve(__dirname, 'src/App.dev.tsx')
const publicEntry = resolve(__dirname, 'src/App.tsx')
const appEntry = wantsDev && existsSync(devEntry) ? devEntry : publicEntry
return {
plugins: [
react(),
tailwindcss(),
svgr({ include: '**/*.svg?react' }),
...(mode === 'development' ? [devVaultWriter()] : []),
],
resolve: {
alias: {
'~app': appEntry,
},
},
}
})