-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
85 lines (71 loc) · 2.26 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* eslint consistent-return: 0 */
const express = require('express');
const webpack = require('webpack');
const path = require('path');
const tls = require('tls');
const https = require('https');
const http = require('http');
const fs = require('fs-extra');
const createProxy = require('./lib');
const { getConfig } = require('./lib/utils');
const config = getConfig();
const { readKeys } = require('./lib/ssl-helper');
if (config.webpackConfig) {
try {
fs.accessSync(config.webpackConfig, fs.constants.F_OK);
} catch (err) {
console.error(`Cannot read webpack config at ${config.webpackConfig}!`);
config.webpackConfig = null;
}
}
let webpackConfig = null;
let compiler = null;
let webpackData = {};
if (config.webpackConfig) {
webpackConfig = require(config.webpackConfig);
compiler = webpack(webpackConfig);
const webpackData = {
compiler,
};
compiler.hooks.afterEmit.tap('DevProxyPlugin', compilation => {
const { publicPath } = compilation.outputOptions;
compilation.entrypoints.forEach(value => {
const { files } = value.runtimeChunk;
webpackData.assetsUrls = files
.map(asset => `${publicPath}${asset}`)
.map(asset => path.resolve(asset));
});
// return true to emit the output, otherwise false
return true;
});
}
const app = express();
// Accept all pre-flight requests
app.use((req, res, next) => {
if (req.method === 'OPTIONS') {
res.statusCode = 200;
res.end();
return;
}
next();
});
app.use(
require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: '/static/',
headers: {
'X-proxy-replaced': 'from-webpack',
},
}),
);
createProxy(app, webpackData);
const secureContext = readKeys(config.ssl);
const httpsOptions = {
SNICallback(domain, callback) {
callback(null, secureContext[domain]);
}, // SNICallback is passed the domain name, see NodeJS docs on TLS
// default ssl keys if browser does not support SNI
...config.ssl[config.ssl.defaultSSLkeys],
};
http.createServer(app).listen(config.port, config.bindAddress);
https.createServer(httpsOptions, app).listen(config.sslPort, config.bindAddress);