Last active
November 28, 2018 06:27
-
-
Save hharnisc/f607004718bba276b56f8f72a92f480e to your computer and use it in GitHub Desktop.
Webpack Dev Middleware (w/ Hot Module Replacement) served by `Micro`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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