-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-server.js
More file actions
72 lines (62 loc) · 2.17 KB
/
dev-server.js
File metadata and controls
72 lines (62 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const { execPath } = require('process');
// Start TypeScript compilation for each project
const projects = [
{ name: 'audio', path: 'audio-visualizer-from-microphone' },
{ name: 'network', path: 'network-tools' },
{ name: 'camera', path: 'camera-utils' }
];
projects.forEach(({ name, path }) => {
Bun.spawn(['bun', 'build', './src/script.ts', '--outdir', './dist', '--watch'], {
stdio: ['inherit', 'inherit', 'inherit'],
cwd: path,
env: process.env
});
console.log(`Started TypeScript compilation for ${name}`);
});
// Start the dev server
const server = Bun.serve({
port: 3000,
development: true,
async fetch(req) {
const url = new URL(req.url);
let filePath = url.pathname;
// Handle directory requests
if (filePath.endsWith('/')) {
filePath += 'index.html';
} else {
// Check if path exists, if not, try adding index.html
const directFile = Bun.file('.' + filePath);
const directExists = await directFile.exists();
if (!directExists && !filePath.endsWith('.html')) {
const withIndex = Bun.file('.' + filePath + '/index.html');
if (await withIndex.exists()) {
filePath += '/index.html';
}
}
}
try {
const file = Bun.file('.' + filePath);
const exists = await file.exists();
if (!exists) {
console.log('Not found:', filePath);
return new Response('Not Found', { status: 404 });
}
// Set appropriate Content-Type
const headers = new Headers();
if (filePath.endsWith('.js')) headers.set('Content-Type', 'application/javascript');
else if (filePath.endsWith('.css')) headers.set('Content-Type', 'text/css');
else if (filePath.endsWith('.html')) headers.set('Content-Type', 'text/html');
return new Response(file, { headers });
} catch (e) {
console.error('Error serving:', filePath, e);
return new Response('Server Error', { status: 500 });
}
},
});
console.log(`
🚀 Development server running at: http://localhost:${server.port}
📦 TypeScript compilation active for all projects
🔥 Hot reload enabled
⌨️ Press Ctrl+C to stop
`);
// No need for explicit cleanup as Bun handles it