80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
|
|
import { defineConfig, Plugin } from 'vite'
|
||
|
|
import react from '@vitejs/plugin-react'
|
||
|
|
import { readFileSync, existsSync } from 'fs'
|
||
|
|
import { resolve } from 'path'
|
||
|
|
|
||
|
|
const pkg = JSON.parse(readFileSync('./package.json', 'utf8'))
|
||
|
|
|
||
|
|
const textArtifacts: Record<string, string> = {
|
||
|
|
'/llms.txt': 'text/plain; charset=utf-8',
|
||
|
|
'/llms-full.txt': 'text/plain; charset=utf-8',
|
||
|
|
'/ARCHITECTURE.md': 'text/markdown; charset=utf-8',
|
||
|
|
}
|
||
|
|
|
||
|
|
function serveTextArtifacts(): Plugin {
|
||
|
|
return {
|
||
|
|
name: 'serve-text-artifacts',
|
||
|
|
configureServer(server) {
|
||
|
|
const publicDir = resolve('public')
|
||
|
|
|
||
|
|
server.middlewares.use((req, res, next) => {
|
||
|
|
const url = req.url?.split('?')[0]
|
||
|
|
const contentType = url ? textArtifacts[url] : undefined
|
||
|
|
if (!url || !contentType) return next()
|
||
|
|
|
||
|
|
const filePath = resolve(publicDir, url.slice(1))
|
||
|
|
if (!existsSync(filePath)) return next()
|
||
|
|
|
||
|
|
res.setHeader('Content-Type', contentType)
|
||
|
|
res.end(readFileSync(filePath))
|
||
|
|
})
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function serveDocs(): Plugin {
|
||
|
|
return {
|
||
|
|
name: 'serve-docs',
|
||
|
|
configureServer(server) {
|
||
|
|
const publicDir = resolve('public')
|
||
|
|
|
||
|
|
server.middlewares.use((req, _res, next) => {
|
||
|
|
const url = req.url?.split('?')[0]
|
||
|
|
if (!url?.startsWith('/docs')) return next()
|
||
|
|
|
||
|
|
const filePath = resolve(publicDir, url.slice(1))
|
||
|
|
|
||
|
|
if (url === '/docs') {
|
||
|
|
if (existsSync(resolve(filePath, 'index.html'))) {
|
||
|
|
req.url = '/docs/index.html'
|
||
|
|
return next()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (url.endsWith('/')) {
|
||
|
|
if (existsSync(resolve(filePath, 'index.html'))) {
|
||
|
|
req.url = url + 'index.html'
|
||
|
|
return next()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!url.split('/').pop()?.includes('.')) {
|
||
|
|
if (existsSync(filePath + '.html')) {
|
||
|
|
req.url = url + '.html'
|
||
|
|
return next()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
next()
|
||
|
|
})
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export default defineConfig({
|
||
|
|
plugins: [react(), serveTextArtifacts(), serveDocs()],
|
||
|
|
define: {
|
||
|
|
__BUILD_VERSION__: JSON.stringify(`v${pkg.version}`),
|
||
|
|
},
|
||
|
|
})
|