53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { defineConfig } from 'vite'
|
|
import vue from '@vitejs/plugin-vue'
|
|
import { resolve } from 'path'
|
|
import fs from 'fs'
|
|
|
|
function apiSavePlugin() {
|
|
return {
|
|
name: 'api-save',
|
|
configureServer(server: any) {
|
|
server.middlewares.use('/api/save', (req: any, res: any) => {
|
|
if (req.method !== 'POST') { res.writeHead(405); res.end(); return }
|
|
let body = ''
|
|
req.on('data', (c: string) => body += c)
|
|
req.on('end', () => {
|
|
try {
|
|
const { path, data } = JSON.parse(body)
|
|
if (!path || typeof path !== 'string' || !path.startsWith('/scenes/')) {
|
|
res.writeHead(400)
|
|
res.end(JSON.stringify({ error: 'invalid path' }))
|
|
return
|
|
}
|
|
const safePath = resolve(__dirname, 'public', '.' + path)
|
|
fs.writeFileSync(safePath, JSON.stringify(data, null, 2))
|
|
res.writeHead(200, { 'Content-Type': 'application/json' })
|
|
res.end(JSON.stringify({ ok: true }))
|
|
} catch (e: any) {
|
|
res.writeHead(400)
|
|
res.end(JSON.stringify({ error: e.message }))
|
|
}
|
|
})
|
|
})
|
|
},
|
|
}
|
|
}
|
|
|
|
export default defineConfig({
|
|
plugins: [vue(), apiSavePlugin()],
|
|
resolve: {
|
|
alias: {
|
|
'@': resolve(__dirname, 'src'),
|
|
'@engine': resolve(__dirname, 'engine'),
|
|
},
|
|
},
|
|
build: {
|
|
rollupOptions: {
|
|
input: {
|
|
main: resolve(__dirname, 'index.html'),
|
|
editor: resolve(__dirname, 'editor/index.html'),
|
|
},
|
|
},
|
|
},
|
|
})
|