-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-dev.js
More file actions
75 lines (57 loc) · 1.68 KB
/
Copy pathserver-dev.js
File metadata and controls
75 lines (57 loc) · 1.68 KB
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
require("babel-polyfill");
const express = require("express");
const webpack = require("webpack");
const webpackDevMiddleware = require("webpack-dev-middleware");
const webpackHotMiddleware = require("webpack-hot-middleware");
const app = express();
const compilerConfigFactory = require("./webpack/webpack.config");
// ================
// Webpack client
const compilerConfig = compilerConfigFactory({
client: true,
env: "development"
});
const compiler = webpack(compilerConfig);
const compilerTask = new Promise(resolve => {
const context = { stats: null };
compiler.plugin("done", stats => {
const resolveContext = !context.stats;
context.stats = stats.toJson({ entrypoints: true });
if (resolveContext) {
resolve(context);
}
});
});
app.use(
webpackDevMiddleware(
compiler,
Object.assign({}, compilerConfig.devServer, {
publicPath: compilerConfig.output.publicPath
})
)
);
app.use(webpackHotMiddleware(compiler));
// ================
// Webpack server
const { runServerCompiler } = require("./server-dev-utils");
const serverCompiler = runServerCompiler({ server: true, env: "development" });
// ================
app.get("*", (req, res) => {
console.log("<<<<", req.path, req.query);
// todo: figure out how errors flow through promises...
compilerTask.then(({ stats }) =>
serverCompiler.task
.then(({ render }) => render(req, res, { stats }))
.catch(reason => {
if (reason.stack) {
res.send(reason.stack);
} else {
res.send(`${reason.constructor.name}: ${reason.message}`);
}
})
);
});
// Express...
app.listen(3000, () => {
console.log("listening on port 3000...");
});