-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev-server.js
64 lines (59 loc) · 1.76 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
53
54
55
56
57
58
59
60
61
62
63
64
const webpack = require('webpack');
const middleware = require('webpack-dev-middleware');
const config = require('./webpack.config.js');
config.mode = 'development'
const compiler = webpack(config);
const express = require('express');
const app = express();
const fs = require('fs');
const WebSocket = require('ws');
app.use(middleware(compiler, {
}));
app.use(express.static('.',{setHeaders: function(req, path, stat){
req.set('Content-Type', "application/wasm");
}}))
app.listen(3000, () => console.log('Example app listening on port 3000!'))
function notifyClients() {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send('reload');
}
});
}
const wss = new WebSocket.Server({ port: 3001 });
(function() {
function exists(filename) {
try {
fs.accessSync(filename);
return true;
} catch (e) {
return false;
}
}
let backoff = 0;
function doWatch() {
try {
let watcher = fs.watch('spasm-tradingview', (event, filename)=>{
if (event == 'change')
notifyClients();
else if (event == 'rename') {
if (exists('spasm-material')) {
notifyClients();
} else {
watcher.close();
backoff = 100;
setTimeout(doWatch, backoff);
}
}
});
if (backoff != 0) {
notifyClients();
backoff = 0;
}
} catch (e) {
backoff = Math.min(backoff * 2, 60000);
setTimeout(doWatch, backoff);
}
}
doWatch();
})();