From 85585938e713259525b86d99b30df9b85d052e97 Mon Sep 17 00:00:00 2001 From: Stephen Jennings Date: Fri, 18 Aug 2017 15:35:37 -0700 Subject: [PATCH] Add the message in BabelLoaderError's stack trace (#499) * 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 --- src/index.js | 3 ++- test/loader.test.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 5fbdd933..826bdbf9 100644 --- a/src/index.js +++ b/src/index.js @@ -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); diff --git a/test/loader.test.js b/test/loader.test.js index f9f0883d..11cf7762 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -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(); + }); + }, +);