kimjuny/koa-webpack-server

View on GitHub
src/hot.js

Summary

Maintainability
A
0 mins
Test Coverage
const hotMiddleware = require('webpack-hot-middleware');
const { PassThrough } = require('stream');

module.exports = (compiler, opts) => {
  const expressMiddleware = hotMiddleware(compiler, opts);
  return async (ctx, next) => {
    let stream = new PassThrough();

    await expressMiddleware(ctx.req, {
      write: stream.write.bind(stream),
      writeHead: (status, headers) => {
        ctx.body = stream;
        ctx.status = status;
        ctx.set(headers);
      },
    }, next);
  };
}