-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
63 lines (52 loc) · 1.69 KB
/
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
'use strict';
const path = require('path');
const config = require('./webpack.config');
const webpack = require('webpack');
const express = require('express');
const app = express();
const devMiddleware = require('webpack-dev-middleware');
const hotMiddleware = require('webpack-hot-middleware');
const { PORT } = process.env;
const port = PORT ? Number(PORT) : 3000;
config.entry.app.unshift(
// activate HMR for React
// 'react-hot-loader/patch',
'webpack-hot-middleware/client'
// // bundle the client for webpack-dev-server
// // and connect to the provided endpoint
// 'webpack-dev-server/client?http://0.0.0.0:' + port,
// // bundle the client for hot reloading
// // only- means to only hot reload for successful updates
// 'webpack/hot/only-dev-server'
);
config.plugins.push(
// enable HMR globally
new webpack.HotModuleReplacementPlugin(),
// prints more readable module names in the browser console on HMR updates
new webpack.NamedModulesPlugin()
);
const compiler = webpack(config);
// webpack-dev-server config
const publicPath = config.output.publicPath;
const stats = {
colors: true,
cached: false,
cachedAssets: false,
chunks: false,
chunkModules: false
};
const options = { publicPath, stats };
app.use(devMiddleware(compiler, options));
app.use(hotMiddleware(compiler));
app.use('*', (req, res, next) => {
const filename = path.join(compiler.outputPath, 'index.html');
compiler.outputFileSystem.readFile(filename, (err, result) => {
if (err) return next(err);
res.set('content-type', 'text/html');
res.send(result);
res.end();
});
});
app.listen(port, '0.0.0.0', () => {
console.log('\n>> Listening at http://0.0.0.0:' + port + '\n');
});