braindrops.nicwands.com/app/vite.config.js
2026-07-17 13:52:32 -04:00

65 lines
2.6 KiB
JavaScript

import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import sharedConfig from '../shared/vite.config.js'
import fs from 'node:fs'
import path from 'node:path'
export default defineConfig({
...sharedConfig,
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'@shared': fileURLToPath(new URL('../shared', import.meta.url)),
},
},
plugins: [
...sharedConfig.plugins,
{
name: 'serve-drops',
configureServer(server) {
server.middlewares.use((req, res, next) => {
// Check if the request is for a drop
if (req.url?.startsWith('/drops/')) {
let filePath = path.join(
fileURLToPath(new URL('./public', import.meta.url)),
req.url.split('?')[0] // Remove query params
)
// Check if the path exists
if (fs.existsSync(filePath)) {
// If it's a directory, append index.html
const stats = fs.statSync(filePath)
if (stats.isDirectory()) {
filePath = path.join(filePath, 'index.html')
if (!fs.existsSync(filePath)) {
return next()
}
}
// Determine content type
let contentType = 'text/html'
if (filePath.endsWith('.js')) {
contentType = 'application/javascript'
} else if (filePath.endsWith('.css')) {
contentType = 'text/css'
} else if (filePath.endsWith('.json')) {
contentType = 'application/json'
} else if (filePath.endsWith('.png')) {
contentType = 'image/png'
} else if (filePath.endsWith('.woff') || filePath.endsWith('.woff2')) {
contentType = 'font/woff' + (filePath.endsWith('.woff2') ? '2' : '')
}
// Read and serve the file
const content = fs.readFileSync(filePath)
res.setHeader('Content-Type', contentType)
res.end(content)
return
}
}
next()
})
},
},
],
})