kitchensink
Version:
Dispatch's awesome components and style guide
50 lines (45 loc) • 1.42 kB
JavaScript
import compress from 'compression';
import path from 'path';
import express from 'express';
import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import config from './webpack.config.js';
const isDeveloping = process.env.NODE_ENV !== 'production';
const port = process.env.PORT || 9000;
const app = express();
app.use(compress());
// We don't build for production yet so we will just run in dev mode.
if (isDeveloping) {
const compiler = webpack(config);
const middleware = webpackMiddleware(compiler, {
publicPath: config.output.publicPath,
contentBase: 'src',
stats: {
colors: true,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
modules: false,
},
});
app.use(middleware);
app.use(webpackHotMiddleware(compiler));
app.get('*', function response(req, res) {
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
res.end();
});
} else {
app.use(express.static(__dirname + '/dist'));
app.get('*', function response(req, res) {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
}
app.listen(port, '0.0.0.0', function onStart(err) {
if (err) {
console.log(err);
} else {
console.log(`Listening on port ${port}. Open up http://localhost:${port}/ in your browser.`);
}
});