-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathserver.js
106 lines (97 loc) · 2.34 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import path from 'path';
import fs from 'fs';
import webpack from 'webpack';
import express from 'express';
import webpackMiddleware from 'webpack-dev-middleware';
import webpackHot from 'webpack-hot-middleware';
import config from './webpack.config';
const hotEntries = [
'react-hot-loader/patch',
'webpack-hot-middleware/client'
];
// Update config entry and output
config[1].entry = {
bundle: [
...hotEntries,
'./src/main.js'
],
demos: [
...hotEntries,
'./demos/index.js'
],
demo1: [
...hotEntries,
'./demos/demo1/index.js'
],
demo2: [
...hotEntries,
'./demos/demo2/index.js'
],
demo3: [
...hotEntries,
'./demos/demo3/index.js'
],
demo4: [
...hotEntries,
'./demos/demo4/index.js'
],
demo5: [
...hotEntries,
'./demos/demo5/index.js'
]
};
config[1].output.filename = '[name].js';
config[1].output.path = path.join(__dirname, 'demos');
config[1].output.publicPath = '/dist/';
delete config[1].externals;
delete config[1].module.rules[1].loader;
// Add plugins
config[1].plugins = [
new webpack.HotModuleReplacementPlugin()
];
// Create express application and attach middleware
const app = express();
const compiler = webpack(config[1]);
app.use(webpackMiddleware(compiler, {
publicPath: config[1].output.publicPath,
stats: {
colors: true,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
modules: false
}
}));
app.use(webpackHot(compiler));
// Setup main route
app.get('/', (req, res) => {
const body = fs
.readFileSync(path.join(__dirname, 'demos/index.html'), 'utf8')
.replace(
'${SCRIPTS}',
'<script src="/dist/bundle.js"></script><script src="/dist/demos.js"></script>'
);
res.set('content-type', 'text/html');
res.send(body);
});
// Setup demo routes
app.get('/demos/:name', (req, res) => {
const body = fs
.readFileSync(path.join(__dirname, 'demos/index.html'), 'utf8')
.replace(
'${SCRIPTS}',
'<script src="/dist/bundle.js"></script><script src="/dist/' + req.params.name + '.js"></script>'
);
res.set('content-type', 'text/html');
res.send(body);
});
// Redirect unknown routes
app.get('*', (req, res) => res.redirect('/'));
// Start the server
app.listen(3000, (err) => {
if (err) {
return console.error(err);
}
console.log('Listening at http://localhost:3000/');
});