Skip to content

Commit

Permalink
Add the message in BabelLoaderError's stack trace (#499)
Browse files Browse the repository at this point in the history
* Add the message in BabelLoaderError's stack trace

When Error.captureStackTrace is called at the top of BabelLoaderError,
the error name and message are not added to the stack property. When
webpack wraps this in a ModuleBuildError, the message is lost.

Capturing at the end of the function ensures the message is part of the
stack trace, so it is eventually added to webpack's output.

* Add test for BabelLoaderError's stack property
  • Loading branch information
jennings authored and danez committed Aug 18, 2017
1 parent ffdf7f1 commit 8558593
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ const fs = require("fs");
*/
function BabelLoaderError(name, message, codeFrame, hideStack, error) {
Error.call(this);
Error.captureStackTrace(this, BabelLoaderError);

this.name = "BabelLoaderError";
this.message = formatMessage(name, message, codeFrame);
this.hideStack = hideStack;
this.error = error;

Error.captureStackTrace(this, BabelLoaderError);
}

BabelLoaderError.prototype = Object.create(Error.prototype);
Expand Down
18 changes: 18 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,21 @@ test.cb("should not throw without config", t => {
t.end();
});
});

test.cb(
"should return compilation errors with the message included in the stack trace",
t => {
const config = Object.assign({}, globalConfig, {
entry: path.join(__dirname, "fixtures/syntax.js"),
output: {
path: t.context.directory,
},
});
webpack(config, (err, stats) => {
const moduleBuildError = stats.compilation.errors[0];
const babelLoaderError = moduleBuildError.error;
t.regex(babelLoaderError.stack, /Unexpected character/);
t.end();
});
},
);

0 comments on commit 8558593

Please sign in to comment.