-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
55 lines (49 loc) · 1.29 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Qi Yu @iyuq
*/
/*global Promise*/
const webpackMiddleware = require("webpack-dev-middleware");
// constructor for the middleware
module.exports = function(compiler, options) {
const instance = webpackMiddleware(compiler, options);
function waitMiddleware() {
return new Promise((resolve, reject) => {
instance.waitUntilValid(() => {
resolve(true);
});
compiler.plugin("failed", (error) => {
reject(error);
});
});
}
function webpackKoaMiddleware(ctx, next) {
return waitMiddleware().then(() => {
const res = {
locals: ctx.state,
send(content) {
if(ctx.status === 404) {
ctx.status = 200;
}
ctx.body = content;
},
setHeader(field, value) {
ctx.set(field, value);
},
get statusCode() {
return ctx.status;
},
set statusCode(code) {
ctx.status = code;
}
};
return instance(ctx.req, res, next);
});
}
webpackKoaMiddleware.getFilenameFromUrl = instance.getFilenameFromUrl;
webpackKoaMiddleware.waitUntilValid = instance.waitUntilValid;
webpackKoaMiddleware.invalidate = instance.invalidate;
webpackKoaMiddleware.close = instance.close;
webpackKoaMiddleware.fileSystem = instance.fileSystem;
return webpackKoaMiddleware;
};