Skip to content

Instantly share code, notes, and snippets.

@hharnisc
Last active November 28, 2018 06:27
Show Gist options
  • Save hharnisc/f607004718bba276b56f8f72a92f480e to your computer and use it in GitHub Desktop.
Save hharnisc/f607004718bba276b56f8f72a92f480e to your computer and use it in GitHub Desktop.
Webpack Dev Middleware (w/ Hot Module Replacement) served by `Micro`
const fs = require('fs');
const webpack = require('webpack');
const { send } = require('micro');
const config = require('./webpack.config');
const webpackMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const compiler = webpack(config);
const middleware = (next) => {
const mw = webpackMiddleware(compiler, {
publicPath: config.output.publicPath,
});
return (req, res) => mw(req, res, () => next(req, res));
};
const hotMiddleware = (next) => {
const mw = webpackHotMiddleware(compiler);
return (req, res) => mw(req, res, () => next(req, res));
};
const html = fs.readFileSync(`${__dirname}/index.html`, 'utf8');
const microService = async (req, res) => send(res, 200, html);
module.exports = [hotMiddleware, middleware].reduce((p, c) => c(p), microService);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment