-
Notifications
You must be signed in to change notification settings - Fork 2
/
dev-server.js
52 lines (47 loc) · 1.56 KB
/
dev-server.js
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
// @ts-check
import { relative } from 'path';
import WebSocket from 'ws';
import chokidar from 'chokidar';
import app_server from './server.js';
import ws_server from './src/ws.js';
import { info } from './src/log.js';
export default function (config) {
let { live_reload, port, ssr, save } = config;
config.port = port = port || process.env.PORT || 8080;
const app = app_server(config);
const { server, wss } = ws_server(app);
const send = message => {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
}
const debounce = (func, delay) => {
let timeout;
return (...args) => {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => func(...args), delay);
};
};
const onChange = ((path) => {
path = '/' + relative(config.output, path);
send(JSON.stringify({ path }));
});
const debouncedOnChange = debounce(onChange, 300);
server.listen(port, function () {
if (live_reload) {
const watcher = chokidar.watch(`${config.output}/**/*.{js,css,html}`, {
ignored: /(^|[\\/])\../, // ignore dotfiles
persistent: true
});
watcher.on('change', debouncedOnChange);
watcher.on('add', debouncedOnChange);
}
info('Your app is listening on:', `http://localhost:${port}`);
info('Serving from:', `${config.output}`);
info('SSR:', `${!ssr ? 'disabled' : 'enabled'}.`);
info('Save:', `${!save ? 'disabled' : 'enabled'}.`);
info('Live reload:', `${!live_reload ? 'disabled' : 'enabled'}.`);
});
}