66 lines
2.7 KiB
JavaScript
66 lines
2.7 KiB
JavaScript
import { fileURLToPath, URL } from 'node:url'
|
|
import { defineConfig, mergeConfig } from 'vite'
|
|
import sharedConfig from '../shared/vite.config.js'
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import glsl from 'vite-plugin-glsl'
|
|
|
|
export default defineConfig(mergeConfig(sharedConfig, {
|
|
...sharedConfig,
|
|
resolve: {
|
|
alias: {
|
|
'@': fileURLToPath(new URL('./src', import.meta.url)),
|
|
'@shared': fileURLToPath(new URL('../shared', import.meta.url)),
|
|
},
|
|
},
|
|
plugins: [
|
|
glsl(),
|
|
{
|
|
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()
|
|
})
|
|
},
|
|
},
|
|
],
|
|
}))
|