const stream = require('stream');
const webpackHot = require('webpack-hot-middleware');
const PassThrough = stream.PassThrough;
module.exports = ({compiler, others}) => {
const middleware = webpackHot(compiler, others);
return async (ctx, next) => {
const res = Object.assign({}, ctx.res);
const streamInstance = new PassThrough();
streamInstance.on('data', chunk => {
if(chunk && global.cache &&global.cache.updateCache){
global.cache.updateCache();
}
});
ctx.body = streamInstance;
await middleware(
ctx.req,
Object.assign(res, {
write: streamInstance.write.bind(streamInstance),
writeHead: (
status ,
headers,
) => {
ctx.status = status;
ctx.set(headers);
},
end: (content) => {
ctx.body = content || 'devMiddleWare no body';
},
}),
next
)
}
}