/
server.develop.js
executable file
·43 lines (36 loc) · 1.32 KB
/
server.develop.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
const express = require('express'),
app = express(),
port = process.env.PORT || 9000,
webpackConfig = require('./webpack.config.js'),
serverHtml = require('./src/server/utils/html.js'),
compiler = require('webpack')(webpackConfig),
compression = require('compression'),
webpackDevHandler = require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: webpackConfig.output.publicPath,
serverSideRender: true
}),
webpackHotHandler = require('webpack-hot-middleware')(compiler, {});
app.use(compression());
app.use(webpackDevHandler);
app.use(webpackHotHandler);
app.use((req, res) => {
const assetsByChunkName = res.locals.webpackStats.toJson().assetsByChunkName;
// TODO: create webpack lib file and function to filter assets
res.send(serverHtml.renderHtml('', '', {
vendor_js: {
js: webpackConfig.output.publicPath + assetsByChunkName.vendor_js.filter(function (path) { return path.endsWith('.js'); })[0]
},
app: {
js: webpackConfig.output.publicPath + assetsByChunkName.app.filter(function (path) { return path.endsWith('.js'); })[0]
}
},
{
title: '',
meta: '',
link: ''
}));
});
app.listen(port, function () {
console.log('app running on localhost:' + port);
});