-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
60 lines (58 loc) · 1.89 KB
/
vite.config.ts
File metadata and controls
60 lines (58 loc) · 1.89 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
import { resolve } from 'path'
import { createReadStream, existsSync } from 'fs'
import { exec } from 'child_process'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig(({ mode }) => ({
base: mode === 'production' ? '/' : '/',
plugins: [
vue(),
{
// Serve data/manufacturers/ files in dev mode and reload on changes
name: 'serve-data',
configureServer(server) {
server.middlewares.use('/data', (req, res, next) => {
const filePath = resolve(__dirname, 'data', req.url!.replace(/^\//, ''))
if (existsSync(filePath) && filePath.endsWith('.json')) {
res.setHeader('Content-Type', 'application/json')
createReadStream(filePath).pipe(res)
} else {
next()
}
})
// Watch data directory, run compile, then reload on JSON changes
const dataDir = resolve(__dirname, 'data')
let compiling = false
server.watcher.add(dataDir)
server.watcher.on('change', (path) => {
if (path.startsWith(dataDir) && path.endsWith('.json') && !compiling) {
compiling = true
console.log('\nData changed, recompiling...')
exec('npm run compile', { cwd: __dirname }, (err, _stdout, stderr) => {
compiling = false
if (err) {
console.error('Compile failed:', stderr || err.message)
} else {
console.log('Compile done, reloading.')
server.ws.send({ type: 'full-reload' })
}
})
}
})
},
},
],
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
},
},
build: {
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
...(mode !== 'production' && { admin: resolve(__dirname, 'admin.html') }),
},
},
},
}))