-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevServer.js
executable file
·54 lines (45 loc) · 1.43 KB
/
devServer.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
// Load environment variables
require('dotenv').config();
const express = require('express');
const chokidar = require('chokidar');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const emptyCache = require('require-reload').emptyCache;
// Initialize express app
let app = express();
// Import webpack configuration
const config = require('./webpack/webpack.config.js');
const PORT = process.env.PORT || config._port;
const compiler = webpack(config);
// Add webpack middleware
app.use(webpackDevMiddleware(compiler, {
stats: {
colors: true,
hash: false,
chunks: false,
}
}));
app.use(webpackHotMiddleware(compiler, {
log: console.log
}));
// Import server as hot-reload
let loadServer = () => require('./server/entry');
let server = loadServer()
// Wrap server in a function so that it can change
app.use((req, res, next) => {
server(req, res, next)
})
// Listen for change in server
const serverWatch = chokidar.watch('./server/**/*.*');
serverWatch.on('ready', () => {
serverWatch.on('all', () => {
console.log("Reloading server");
// Re-require server
emptyCache(server.context);
server = loadServer();
})
})
app.listen(PORT, function() {
console.log("Now listening on port " + PORT + " :)");
});