-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
executable file
·114 lines (86 loc) · 2.96 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
107
108
109
110
111
112
113
114
#!/usr/bin/env node
const express = require('express');
const webpack = require('webpack');
const favicon = require('serve-favicon');
const program = require('commander');
const pack = require('./package');
const customFs = require('./lib/fs');
const getWebpackConfig = require('./lib/get_webpack_config');
const { CUSTOM_PREVIEW_HEADER, MIDDLEWARES_FILE, FAVICON_FILE, FALLBACK_FAVICON_FILE } = require('./config').paths;
const managerPageTemplate = require('./templates/manager_page.html');
const previewPageTemplate = require('./templates/preview_page.html');
const webpackMiddleware = require('webpack-dev-middleware');
const networkInterfaces = require('os').networkInterfaces();
const IP_ADDR = getIpAddr();
// read arguments
program
.version(pack.version)
.option('-p, --port <n>', 'Port')
.parse(process.argv);
// settings
const PORT = program.port || 3000;
// app
const app = express();
Promise.all([
customFs.readFile(CUSTOM_PREVIEW_HEADER),
getWebpackConfig(),
customFs.checkFile(MIDDLEWARES_FILE),
customFs.checkFile(FAVICON_FILE),
]).then(([header, webpackConfig, customMiddlewaresExist, faviconFileExists]) => {
const compiler = webpack(webpackConfig);
if(faviconFileExists){
console.log('using custon favicon');
}
app.use(favicon(faviconFileExists ? FAVICON_FILE : FALLBACK_FAVICON_FILE));
app.use(webpackMiddleware(compiler, {
serverSideRender: true,
stats: 'minimal',
}));
app.use((req, res, next) => {
res.header = header || '';
next();
});
app.get('/preview.html', previewMiddleware);
if (customMiddlewaresExist) {
try {
const customMiddlewares = [].concat(require(MIDDLEWARES_FILE));
customMiddlewares.forEach((middleware) => {
app.use(...[].concat(middleware));
});
console.log(`Added custom middlewares from ${MIDDLEWARES_FILE}`);
} catch (e) {
console.error(
`Couldn't load middlewares from file ${MIDDLEWARES_FILE}. Error: ${e}`,
);
}
}
app.use(appMiddleware);
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}\nOpen http://localhost:${PORT}/ on browser`);
console.log(`IP address ==> ${IP_ADDR}`);
});
}).catch(console.log);
function getIpAddr() {
let addr = null;
Object.keys(networkInterfaces).forEach((dev) => {
networkInterfaces[dev].filter((details) => {
if (details.family === 'IPv4' && details.internal === false) {
addr = details.address;
}
});
});
return addr;
}
function appMiddleware(req, res) {
const stats = assetsByChunkName = res.locals.webpackStats.toJson();
res.send(managerPageTemplate({
assets: [].concat(stats.assetsByChunkName.manager).map(asset => `${stats.publicPath}${asset}`),
}));
}
function previewMiddleware(req, res) {
const stats = assetsByChunkName = res.locals.webpackStats.toJson();
res.send(previewPageTemplate({
assets: [].concat(stats.assetsByChunkName.preview).map(asset => `${stats.publicPath}${asset}`),
header: res.header,
}));
}