import path from 'path'; // Build the TypeScript bundle async function buildBundle() { const result = await Bun.build({ entrypoints: ['./src/main.ts'], outdir: './dist', target: 'browser', format: 'esm', sourcemap: 'inline', minify: false, }); if (!result.success) { console.error('Build failed:'); for (const log of result.logs) { console.error(log); } throw new Error('Build failed'); } console.log('Bundle built successfully'); return result; } // Initial build await buildBundle(); const server = Bun.serve({ port: 3000, hostname: '0.0.0.0', // Bind to all interfaces async fetch(req) { const url = new URL(req.url); let filePath = url.pathname; // Default to index.html if (filePath === '/') { filePath = '/index.html'; } // Redirect main.ts to bundled version if (filePath === '/src/main.ts') { filePath = '/dist/main.js'; } // Determine content type const ext = filePath.split('.').pop() || ''; const contentTypes: Record = { html: 'text/html', js: 'application/javascript', mjs: 'application/javascript', css: 'text/css', json: 'application/json', png: 'image/png', jpg: 'image/jpeg', gif: 'image/gif', svg: 'image/svg+xml', }; const contentType = contentTypes[ext] || 'application/octet-stream'; try { const file = Bun.file(`.${filePath}`); const exists = await file.exists(); if (!exists) { console.log(`404: ${filePath}`); return new Response('Not Found', { status: 404 }); } return new Response(file, { headers: { 'Content-Type': contentType, 'Cache-Control': 'no-cache', }, }); } catch (error) { console.error('Server error:', error); return new Response('Internal Server Error', { status: 500 }); } }, }); console.log(`Server running at http://0.0.0.0:${server.port}`); console.log(`Access locally: http://localhost:${server.port}`);