MyCrypto/webpack_config/server.js

88 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-07-04 03:28:56 +00:00
'use strict';
const path = require('path');
const express = require('express');
const webpack = require('webpack');
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');
const LogPlugin = require('./log-plugin');
2017-07-04 03:28:56 +00:00
const app = express();
2017-07-04 03:28:56 +00:00
const port = config.port;
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-07-04 03:28:56 +00:00
webpackConfig.plugins.push(new LogPlugin(port));
2017-07-04 03:28:56 +00:00
let compiler;
try {
2017-07-04 03:28:56 +00:00
compiler = webpack(webpackConfig);
} catch (err) {
2017-07-04 03:28:56 +00:00
console.log(err.message);
process.exit(1);
}
const devMiddleWare = require('webpack-dev-middleware')(compiler, {
2017-07-04 03:28:56 +00:00
publicPath: webpackConfig.output.publicPath,
quiet: true,
inline: true,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*'
},
watchOptions: {
aggregateTimeout: 300,
poll: true
2017-07-04 03:28:56 +00:00
}
});
app.use(devMiddleWare);
app.use(
require('webpack-hot-middleware')(compiler, {
log: console.log
2017-07-04 03:28:56 +00:00
})
);
2017-07-04 03:28:56 +00:00
const mfs = devMiddleWare.fileSystem;
const file = path.join(webpackConfig.output.path, 'index.html');
2017-07-04 03:28:56 +00:00
devMiddleWare.waitUntilValid();
app.get('*', (req, res) => {
2017-07-04 03:28:56 +00:00
devMiddleWare.waitUntilValid(() => {
const html = mfs.readFileSync(file);
res.end(html);
});
});
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);
}