MyCrypto/webpack_config/server.js

60 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-07-03 20:28:56 -07:00
'use strict';
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const webpackConfig = require('./webpack.dev');
const config = require('./config');
const LogPlugin = require('./log-plugin');
2017-07-03 20:28:56 -07:00
const app = express();
2017-07-03 20:28:56 -07:00
const port = config.port;
webpackConfig.entry.client = [
2017-07-03 20:28:56 -07:00
'react-hot-loader/patch',
'webpack-hot-middleware/client?reload=true',
'webpack/hot/only-dev-server',
webpackConfig.entry.client
];
2017-07-03 20:28:56 -07:00
webpackConfig.plugins.push(new LogPlugin(port));
2017-07-03 20:28:56 -07:00
let compiler;
try {
2017-07-03 20:28:56 -07:00
compiler = webpack(webpackConfig);
} catch (err) {
2017-07-03 20:28:56 -07:00
console.log(err.message);
process.exit(1);
}
const devMiddleWare = require('webpack-dev-middleware')(compiler, {
2017-07-03 20:28:56 -07:00
publicPath: webpackConfig.output.publicPath,
quiet: true,
inline: true,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*'
}
});
app.use(devMiddleWare);
app.use(
require('webpack-hot-middleware')(compiler, {
log: console.log
2017-07-03 20:28:56 -07:00
})
);
2017-07-03 20:28:56 -07:00
const mfs = devMiddleWare.fileSystem;
const file = path.join(webpackConfig.output.path, 'index.html');
2017-07-03 20:28:56 -07:00
devMiddleWare.waitUntilValid();
app.get('*', (req, res) => {
2017-07-03 20:28:56 -07:00
devMiddleWare.waitUntilValid(() => {
const html = mfs.readFileSync(file);
res.end(html);
});
});
2017-07-03 20:28:56 -07:00
app.listen(port);