Skip to content

Commit

Permalink
Propagate async errors to webpack (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinpickle authored and TimothyGu committed Feb 24, 2017
1 parent e1f27be commit 7459460
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ module.exports = function(source) {
missingFileMode = false;
return;
}
throw e;
loaderContext.callback(e);
return;
}
var runtime = "var pug = require(" + loaderUtils.stringifyRequest(loaderContext, "!" + modulePaths.runtime) + ");\n\n";
loaderContext.callback(null, runtime + tmplFunc.toString() + ";\nmodule.exports = template;");
Expand Down
19 changes: 19 additions & 0 deletions test/extend+error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var should = require("should");

var fs = require("fs");
var path = require("path");

var runLoader = require("./fakeModuleSystem");
var pugLoader = require("../");

var fixtures = path.join(__dirname, "fixtures");

describe("extend+error", function() {
it("should propagate async errors to Webpack", function(done) {
var template = path.join(fixtures, "extend+error", "error.pug");
runLoader(pugLoader, path.join(fixtures, "extend+error"), template, fs.readFileSync(template, "utf-8"), function(err, result) {
should(err).be.not.null();
done();
});
});
});
26 changes: 18 additions & 8 deletions test/fakeModuleSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var fs = require("fs");
var path = require("path");

module.exports = function runLoader(loader, directory, filename, arg, callback) {
var async = true;
var async = false;
var loaderContext = {
async: function() {
async = true;
Expand All @@ -17,16 +17,26 @@ module.exports = function runLoader(loader, directory, filename, arg, callback)
async = true;
return callback.apply(this, arguments);
},
resolve: function(context, request, callback) {
callback(null, path.resolve(context, request));
resolve: function(context, request, resolveCallback) {
process.nextTick(function() {
resolveCallback(null, path.resolve(context, request));
});
},
loadModule: function(request, callback) {
loadModule: function(request, loadCallback) {
request = request.replace(/^-?!+/, "");
request = request.split("!");
var content = fs.readFileSync(request.pop(), "utf-8");
if(request[0] && /stringify/.test(request[0]))
content = JSON.stringify(content);
return callback(null, content);
fs.readFile(request.pop(), 'utf-8', function(err, content) {
if (err) {
loadCallback(err);
return;
}

if(request[0] && /stringify/.test(request[0])) {
content = JSON.stringify(content);
}

loadCallback(null, content);
});
}
};
var res = loader.call(loaderContext, arg);
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/extend+error/error.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extend /parent.pug

p This will cause an error
2 changes: 2 additions & 0 deletions test/fixtures/extend+error/parent.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
block output
p This might not see the light of day

0 comments on commit 7459460

Please sign in to comment.