2017-07-04 03:28:56 +00:00
|
|
|
'use strict';
|
|
|
|
const path = require('path');
|
|
|
|
const express = require('express');
|
|
|
|
const webpack = require('webpack');
|
2017-09-20 00:47:46 +00:00
|
|
|
const https = require('https');
|
|
|
|
const fs = require('fs');
|
2017-07-04 03:28:56 +00:00
|
|
|
const webpackConfig = require('./webpack.dev');
|
|
|
|
const config = require('./config');
|
2018-01-22 23:38:06 +00:00
|
|
|
const LogPlugin = require('./plugins/serverLog');
|
2017-04-12 04:59:58 +00:00
|
|
|
|
2017-07-04 03:28:56 +00:00
|
|
|
const app = express();
|
2017-04-12 04:59:58 +00:00
|
|
|
|
2017-07-04 03:28:56 +00:00
|
|
|
const port = config.port;
|
2017-04-12 04:59:58 +00:00
|
|
|
webpackConfig.entry.client = [
|
2017-07-04 03:28:56 +00:00
|
|
|
'react-hot-loader/patch',
|
|
|
|
'webpack-hot-middleware/client?reload=true',
|
|
|
|
'webpack/hot/only-dev-server',
|
|
|
|
webpackConfig.entry.client
|
|
|
|
];
|
2017-04-12 04:59:58 +00:00
|
|
|
|
2017-07-04 03:28:56 +00:00
|
|
|
webpackConfig.plugins.push(new LogPlugin(port));
|
2017-04-12 04:59:58 +00:00
|
|
|
|
2017-07-04 03:28:56 +00:00
|
|
|
let compiler;
|
2017-04-12 04:59:58 +00:00
|
|
|
|
|
|
|
try {
|
2017-07-04 03:28:56 +00:00
|
|
|
compiler = webpack(webpackConfig);
|
2017-04-12 04:59:58 +00:00
|
|
|
} catch (err) {
|
2018-01-22 23:38:06 +00:00
|
|
|
console.error(err.message);
|
2017-07-04 03:28:56 +00:00
|
|
|
process.exit(1);
|
2017-04-12 04:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const devMiddleWare = require('webpack-dev-middleware')(compiler, {
|
2017-07-04 03:28:56 +00:00
|
|
|
publicPath: webpackConfig.output.publicPath,
|
2018-01-22 23:38:06 +00:00
|
|
|
logLevel: 'warn',
|
2017-07-04 03:28:56 +00:00
|
|
|
inline: true,
|
|
|
|
headers: {
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
'Access-Control-Allow-Methods': '*',
|
|
|
|
'Access-Control-Allow-Headers': '*'
|
2017-07-11 03:03:08 +00:00
|
|
|
},
|
|
|
|
watchOptions: {
|
2017-12-30 20:29:04 +00:00
|
|
|
aggregateTimeout: 100
|
2017-07-04 03:28:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
app.use(devMiddleWare);
|
|
|
|
app.use(
|
|
|
|
require('webpack-hot-middleware')(compiler, {
|
2018-03-27 04:04:08 +00:00
|
|
|
log: false
|
2017-07-04 03:28:56 +00:00
|
|
|
})
|
|
|
|
);
|
2017-04-12 04:59:58 +00:00
|
|
|
|
2017-07-04 03:28:56 +00:00
|
|
|
const mfs = devMiddleWare.fileSystem;
|
|
|
|
const file = path.join(webpackConfig.output.path, 'index.html');
|
2017-04-12 04:59:58 +00:00
|
|
|
|
2017-07-04 03:28:56 +00:00
|
|
|
devMiddleWare.waitUntilValid();
|
2017-04-12 04:59:58 +00:00
|
|
|
|
|
|
|
app.get('*', (req, res) => {
|
2017-07-04 03:28:56 +00:00
|
|
|
devMiddleWare.waitUntilValid(() => {
|
|
|
|
const html = mfs.readFileSync(file);
|
|
|
|
res.end(html);
|
|
|
|
});
|
|
|
|
});
|
2017-04-12 04:59:58 +00:00
|
|
|
|
2017-09-20 00:47:46 +00:00
|
|
|
if (process.env.HTTPS) {
|
|
|
|
let creds = {};
|
|
|
|
try {
|
|
|
|
creds.key = fs.readFileSync(path.resolve(__dirname, 'server.key'), 'utf8');
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Failed to get SSL private key at webpack_config/server.key');
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
creds.cert = fs.readFileSync(path.resolve(__dirname, 'server.crt'), 'utf8');
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Failed to get SSL certificate at webpack_config/server.crt');
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const httpsApp = https.createServer(creds, app);
|
|
|
|
httpsApp.listen(port);
|
|
|
|
} else {
|
|
|
|
app.listen(port);
|
|
|
|
}
|