Skip to content

Commit 26f0b40

Browse files
lbdremyshellscape
authored andcommitted
Fix webpackDevMiddleware to work with Koa2, a Promise must always be returned (#177)
* Fix webpackDevMiddleware to always return a Promise in order to be able to await on it for koa flow and always chain with next if required. * fix(eslintrc): Add es6 env * Wrap shared.ready method call into a Promise for koa2 support * Use normal form for an anonymous function not the arrow form * fix missing closing } * Remove unexpected space before function parentheses, fix lint
1 parent a02a3cf commit 26f0b40

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"env": {
3+
"es6": true,
34
"node": true,
45
"mocha": true
56
},

middleware.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ module.exports = function(compiler, options) {
2626
function webpackDevMiddleware(req, res, next) {
2727
function goNext() {
2828
if(!context.options.serverSideRender) return next();
29-
shared.ready(function() {
30-
res.locals.webpackStats = context.webpackStats;
31-
next();
32-
}, req);
29+
return new Promise(function(resolve) {
30+
shared.ready(function() {
31+
res.locals.webpackStats = context.webpackStats;
32+
resolve(next());
33+
}, req);
34+
});
3335
}
3436

3537
if(req.method !== "GET") {
@@ -39,39 +41,41 @@ module.exports = function(compiler, options) {
3941
var filename = getFilenameFromUrl(context.options.publicPath, context.compiler, req.url);
4042
if(filename === false) return goNext();
4143

42-
43-
shared.handleRequest(filename, processRequest, req);
44-
45-
function processRequest() {
46-
try {
47-
var stat = context.fs.statSync(filename);
48-
if(!stat.isFile()) {
49-
if(stat.isDirectory()) {
50-
filename = pathJoin(filename, context.options.index || "index.html");
51-
stat = context.fs.statSync(filename);
52-
if(!stat.isFile()) throw "next";
53-
} else {
54-
throw "next";
44+
return new Promise(function(resolve) {
45+
shared.handleRequest(filename, processRequest, req);
46+
function processRequest() {
47+
try {
48+
var stat = context.fs.statSync(filename);
49+
if(!stat.isFile()) {
50+
if(stat.isDirectory()) {
51+
filename = pathJoin(filename, context.options.index || "index.html");
52+
stat = context.fs.statSync(filename);
53+
if(!stat.isFile()) throw "next";
54+
} else {
55+
throw "next";
56+
}
5557
}
58+
} catch(e) {
59+
return resolve(goNext());
5660
}
57-
} catch(e) {
58-
return goNext();
59-
}
6061

61-
// server content
62-
var content = context.fs.readFileSync(filename);
63-
content = shared.handleRangeHeaders(content, req, res);
64-
res.setHeader("Content-Type", mime.lookup(filename) + "; charset=UTF-8");
65-
res.setHeader("Content-Length", content.length);
66-
if(context.options.headers) {
67-
for(var name in context.options.headers) {
68-
res.setHeader(name, context.options.headers[name]);
62+
// server content
63+
var content = context.fs.readFileSync(filename);
64+
content = shared.handleRangeHeaders(content, req, res);
65+
res.setHeader("Content-Type", mime.lookup(filename) + "; charset=UTF-8");
66+
res.setHeader("Content-Length", content.length);
67+
if(context.options.headers) {
68+
for(var name in context.options.headers) {
69+
res.setHeader(name, context.options.headers[name]);
70+
}
6971
}
72+
// Express automatically sets the statusCode to 200, but not all servers do (Koa).
73+
res.statusCode = res.statusCode || 200;
74+
if(res.send) res.send(content);
75+
else res.end(content);
76+
resolve();
7077
}
71-
72-
if(res.send) res.send(content);
73-
else res.end(content);
74-
}
78+
});
7579
}
7680

7781
webpackDevMiddleware.getFilenameFromUrl = getFilenameFromUrl.bind(this, context.options.publicPath, context.compiler);

0 commit comments

Comments
 (0)