-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
77 lines (63 loc) · 1.88 KB
/
main.ts
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
73
74
75
76
77
import { serveDir } from 'https://deno.land/std/http/file_server.ts';
import * as esbuild from 'https://deno.land/x/[email protected]/mod.js';
import { vanillaExtractPlugin } from '@vanilla-extract/esbuild-plugin';
let bytes: Uint8Array;
const PORT = process.env.PORT || 8000;
const watcher = Deno.watchFs(['./index.html', './src'], { recursive: true });
await esbuild.initialize({});
async function bundleTS() {
bytes = await Deno.readFile('index.html');
try {
const result = await esbuild.build({
entryPoints: ['src/app.ts'],
bundle: true,
outfile: 'public/js/bundle.js',
format: 'esm',
plugins: [
//vanillaExtractPlugin({ outputCss: true, identifiers: 'debug' }),
],
platform: 'browser',
sourcemap: true,
sourceRoot: 'src',
target: 'es2020',
});
return await esbuild.build({
entryPoints: ['src/styles.css'],
bundle: true,
outfile: 'public/css/static_bundle.css',
});
console.log('Bundle successful:', new Date().toLocaleTimeString());
} catch (error) {
console.error('Bundle failed:', error);
}
}
await bundleTS();
const sockets = new Set<WebSocket>();
Deno.serve(async (req: Request) => {
const url = new URL(req.url);
if (url.pathname === '/') {
return new Response(bytes);
}
if (req.headers.get('upgrade') === 'websocket') {
const { socket, response } = Deno.upgradeWebSocket(req);
socket.onopen = () => {
sockets.add(socket);
};
socket.onclose = () => {
sockets.delete(socket);
};
return response;
}
return await serveDir(req, {
fsRoot: 'public',
urlRoot: '',
});
}, { port: PORT });
console.log(`Dev server running on http://localhost:${PORT}/`);
for await (const event of watcher) {
console.log('Watcher has seen some things...', event);
await bundleTS();
sockets.forEach((socket) => {
socket.send('reload');
});
}