-
Notifications
You must be signed in to change notification settings - Fork 121
/
server.mjs
80 lines (65 loc) · 2.33 KB
/
server.mjs
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
78
79
80
/* eslint-disable no-console */
/**
* Setup and run the development server for Hot-Module-Replacement
* https://webpack.github.io/docs/hot-module-replacement-with-webpack.html
* @flow
*/
import express from "express";
import webpack from "webpack";
import webpackDevMiddleware from "webpack-dev-middleware";
import webpackHotMiddleware from "webpack-hot-middleware";
import { spawn } from "child_process";
import fs from "fs";
import minimist from "minimist";
import config from "./webpack/ui.dev.js";
const argv = minimist(process.argv.slice(2));
const app = express();
const compiler = webpack(config);
const PORT = process.env.PORT || 3000;
const wdm = webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
stats: {
colors: true
}
});
app.use(wdm);
app.use(webpackHotMiddleware(compiler));
let preloadProc;
const sleep = (ms) => new Promise(ok => setTimeout(ok, ms));
const server = app.listen(PORT, "localhost", async serverError => {
if (serverError) {
return console.error(serverError);
}
// Start a webpack run to watch for changes to the preload script. We wait
// until the preload script is compiled to proceed with the server init.
const preloadPath = "./app/dist/wallet-preload.js";
if (fs.existsSync(preloadPath)) fs.unlinkSync(preloadPath);
preloadProc = spawn("npm", ["run", "start-preload"], { shell: true, env: process.env, stdio: "inherit" });
for (let i = 0; i < 60; i++) {
await sleep(1000);
if (fs.existsSync(preloadPath)) break;
}
if (!fs.existsSync(preloadPath)) {
throw "Preload script not created at " + preloadPath;
}
if (argv["start-hot"]) {
spawn("npm", [ "run", "start-hot" ], { shell: true, env: process.env, stdio: "inherit" })
.on("close", code => process.exit(code))
.on("error", spawnError => console.error(spawnError));
} else if (argv["start-hot-nosandbox"]) {
spawn("npm", [ "run", "start-hot-nosandbox" ], { shell: true, env: process.env, stdio: "inherit" })
.on("close", code => process.exit(code))
.on("error", spawnError => console.error(spawnError));
}
console.log(`Listening at http://localhost:${PORT}`);
});
process.on("exit", () => {
preloadProc && preloadProc.kill();
});
process.on("SIGTERM", () => {
console.log("Stopping dev server");
wdm.close();
server.close(() => {
process.exit(0);
});
});