-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
61 lines (49 loc) · 1.6 KB
/
index.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
// Development server startup
const webpack = require('webpack'),
serveStatic = require('serve-static'),
polka = require('polka'),
path = require('path');
const appConfig = require('./app-config'),
webpackConfig = require('./webpack.config'),
app = polka(),
port = process.env.PORT || appConfig.port;
let layout = 'Compiling... Refresh in a moment.';
if (appConfig.proxy) {
const hpm = require('http-proxy-middleware');
for (const prefix of appConfig.proxy.prefix)
app.use(hpm(prefix, appConfig.proxy));
}
// modify webpack config to work with a hot middleware
// noinspection JSValidateTypes
webpackConfig.entry = ['webpack-hot-middleware/client', webpackConfig.entry];
if (!webpackConfig.plugins) webpackConfig.plugins = [];
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
// dev middleware
const compiler = webpack(webpackConfig),
devMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
stats: {
colors: true,
chunks: false,
},
watchOptions: {
aggregateTimeout: 300,
poll: true,
},
});
// noinspection JSUnresolvedFunction
compiler.hooks.done.tap('done', () => {
const fs = devMiddleware.fileSystem,
filePath = path.join(webpackConfig.output.path, 'index.html');
if (fs.existsSync(filePath)) layout = fs.readFileSync(filePath, 'utf-8');
});
app.use(devMiddleware);
// hot middleware
app.use(require('webpack-hot-middleware')(compiler));
app.use('/dist', serveStatic('./dist'));
app.get('*', (req, res) => {
res.setHeader('Content-Type', 'text/html');
res.end(layout);
});
app.listen(port);
module.exports = app;