-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
59 lines (49 loc) · 1.4 KB
/
server.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
import * as express from "express";
import {promises as fsp} from "fs";
import * as os from "os";
import * as path from "path";
import {exec} from "child_process";
import * as livereload from "livereload";
import * as compression from "compression";
import * as webpack from "webpack";
/* express */
const app = express();
const port = process.env.PORT || 8000;
// standard stuff
app.use(compression());
app.use("/", async (req, res, next) => {
if (req.path !== "/") {
return next();
// insert livereload
}
let file = await fsp.readFile("./index.html", "utf8");
const lrBlurb =
"<script>document.write(`<script src=\"" +
"${location.protocol}//${location.hostname}:35729/livereload.js\">" +
"<` + `/script>`);</script>";
file = file.replace("</body>", lrBlurb + "</body>");
res.send(file);
});
app.use("/", express.static("."));
app.listen(port);
/* livereload */
const livereload = require("livereload");
const lrHttpServer = livereload.createServer({
exts: ["html", "css", "png", "gif", "jpg", "svg"]
});
lrHttpServer.watch(__dirname);
/* webpack */
const webpackConfig = require("./webpack.config.js");
const compiler = webpack(webpackConfig);
// watch
let firstRun = true;
compiler.watch({}, (err, stats) => {
console.info(stats.toString({
colors: true
}));
// open in browser
if (firstRun) {
firstRun = false;
exec(`xdg-open http://localhost:${port}`);
}
});