-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhmr.js
37 lines (35 loc) · 1.12 KB
/
hmr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const webpackDevMiddleware = require("webpack-dev-middleware")
const webpackHotMiddleware = require("webpack-hot-middleware")
const { PassThrough } = require('stream')
exports.koaDevMiddleware = (webpackCompiler, options) => {
const expressStyled = webpackDevMiddleware(webpackCompiler, options)
const koaStyled = async (ctx, next) => {
await expressStyled(ctx.req, {
end: (content) => {
ctx.body = content
},
setHeader: (name, value) => {
ctx.set(name, value)
},
locals: ctx.state,
}, next)
}
koaStyled.close = expressStyled.close
koaStyled.invalidate = expressStyled.invalidate
koaStyled.waitUntilInvalid = expressStyled.waitUntilInvalid
return koaStyled
}
exports.koaHotMiddleware = (webpackCompiler, options) => {
const expressStyled = webpackHotMiddleware(webpackCompiler, options);
return async (ctx, next) => {
const stream = new PassThrough()
await expressStyled(ctx.req, {
write: stream.write.bind(stream),
writeHead: (status, headers) => {
ctx.body = stream
ctx.status = status
ctx.set(headers)
},
}, next)
}
}