-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
60 lines (53 loc) · 1.74 KB
/
vite.config.js
File metadata and controls
60 lines (53 loc) · 1.74 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 { defineConfig } from 'vite'
import { readFileSync, readdirSync, statSync } from 'fs'
import { join, extname } from 'path'
function getScripts() {
const scriptsDir = join(process.cwd(), 'scripts')
const files = readdirSync(scriptsDir)
return files
.filter((file) => extname(file) === '.js')
.map((file) => ({
name: file.replace('.js', ''),
path: join(scriptsDir, file),
}))
}
export default defineConfig({
server: {
port: 3000,
host: true,
cors: true,
},
configureServer(server) {
// Serve individual scripts
server.middlewares.use('/scripts', (req, res, next) => {
const url = req.url.substring(1) // Remove leading slash
const scriptPath = join(process.cwd(), 'scripts', url)
try {
if (statSync(scriptPath).isFile() && extname(scriptPath) === '.js') {
const content = readFileSync(scriptPath, 'utf-8')
res.setHeader('Content-Type', 'application/javascript')
res.setHeader('Access-Control-Allow-Origin', '*')
res.end(content)
return
}
} catch (err) {
// File doesn't exist, continue
}
next()
})
// Serve scripts list
server.middlewares.use('/api/scripts', (_, res) => {
const scripts = getScripts()
const baseUrl = `http://localhost:3000`
const scriptList = scripts.map((script) => ({
name: script.name,
url: `${baseUrl}/scripts/${script.name}.js`,
downloadUrl: `${baseUrl}/scripts/${script.name}.js`,
updateUrl: `${baseUrl}/scripts/${script.name}.js`,
}))
res.setHeader('Content-Type', 'application/json')
res.setHeader('Access-Control-Allow-Origin', '*')
res.end(JSON.stringify(scriptList, null, 2))
})
},
})