MyCrypto/webpack_config/server.js

64 lines
1.4 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 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);
});
});
2017-07-04 03:28:56 +00:00
app.listen(port);