27 lines
891 B
TypeScript
27 lines
891 B
TypeScript
import { defineConfig } from 'vite';
|
|
import react from '@vitejs/plugin-react';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
export default defineConfig({
|
|
plugins: [
|
|
react(),
|
|
{
|
|
name: 'serve-resources',
|
|
configureServer(server) {
|
|
server.middlewares.use('/resources', (req, res, next) => {
|
|
const filePath = path.join(process.cwd(), 'resources', decodeURIComponent(req.url ?? ''));
|
|
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
|
|
const ext = path.extname(filePath).toLowerCase();
|
|
const mime = ext === '.png' ? 'image/png' : ext === '.jpg' ? 'image/jpeg' : 'application/octet-stream';
|
|
res.setHeader('Content-Type', mime);
|
|
fs.createReadStream(filePath).pipe(res as import('stream').Writable);
|
|
} else {
|
|
next();
|
|
}
|
|
});
|
|
},
|
|
},
|
|
],
|
|
});
|