Vue-SSR-Koa2-Scaffold/config/koa/hot.js

32 lines
792 B
JavaScript
Raw Normal View History

2018-11-11 23:47:41 +08:00
// 参考自 https://github.com/ccqgithub/koa-webpack-hot/blob/master/index.js
const hotMiddleware = require('webpack-hot-middleware')
const PassThrough = require('stream').PassThrough
module.exports = (compiler, opts = {}) => {
opts.path = opts.path || '/__webpack_hmr'
const middleware = hotMiddleware(compiler, opts)
return async (ctx, next) => {
if (ctx.request.path !== opts.path) {
return next()
}
const stream = new PassThrough()
ctx.body = stream
middleware(ctx.req, {
write: stream.write.bind(stream),
writeHead: (status, headers) => {
ctx.status = status
Object.keys(headers).forEach(key => {
ctx.set(key, headers[key])
})
},
end: () => {
stream.end()
}
}, next)
}
}