diff --git a/packages/babel-core/src/transformation/file/options/option-manager.js b/packages/babel-core/src/transformation/file/options/option-manager.js index 55fdfc20d6..896e5225ef 100644 --- a/packages/babel-core/src/transformation/file/options/option-manager.js +++ b/packages/babel-core/src/transformation/file/options/option-manager.js @@ -258,34 +258,41 @@ export default class OptionManager { } let presetLoc; - if (typeof val === "string") { - presetLoc = resolve(`babel-preset-${val}`, dirname) || resolve(val, dirname); - if (!presetLoc) { - throw new Error(`Couldn't find preset ${JSON.stringify(val)} relative to directory ` + - JSON.stringify(dirname)); + try { + if (typeof val === "string") { + presetLoc = resolve(`babel-preset-${val}`, dirname) || resolve(val, dirname); + if (!presetLoc) { + throw new Error(`Couldn't find preset ${JSON.stringify(val)} relative to directory ` + + JSON.stringify(dirname)); + } + + val = require(presetLoc); } - val = require(presetLoc); + // For compatibility with babel-core < 6.13.x, allow presets to export an object with a + // a 'buildPreset' function that will return the preset itself, while still exporting a + // simple object (rather than a function), for supporting old Babel versions. + if (typeof val === "object" && val.buildPreset) val = val.buildPreset; + + + if (typeof val !== "function" && options !== undefined) { + throw new Error(`Options ${JSON.stringify(options)} passed to ` + + (presetLoc || "a preset") + " which does not accept options."); + } + + if (typeof val === "function") val = val(context, options); + + if (typeof val !== "object") { + throw new Error(`Unsupported preset format: ${val}.`); + } + + onResolve && onResolve(val, presetLoc); + } catch (e) { + if (presetLoc) { + e.message += ` (While processing preset: ${JSON.stringify(presetLoc)})`; + } + throw e; } - - // For compatibility with babel-core < 6.13.x, allow presets to export an object with a - // a 'buildPreset' function that will return the preset itself, while still exporting a - // simple object (rather than a function), for supporting old Babel versions. - if (typeof val === "object" && val.buildPreset) val = val.buildPreset; - - - if (typeof val !== "function" && options !== undefined) { - throw new Error(`Options ${JSON.stringify(options)} passed to ` + - (presetLoc || "a preset") + " which does not accept options."); - } - - if (typeof val === "function") val = val(context, options); - - if (typeof val !== "object") { - throw new Error(`Unsupported preset format: ${val}.`); - } - - onResolve && onResolve(val); return val; }); } diff --git a/packages/babel-core/test/fixtures/option-manager/not-a-preset.js b/packages/babel-core/test/fixtures/option-manager/not-a-preset.js new file mode 100644 index 0000000000..7059dfdbe3 --- /dev/null +++ b/packages/babel-core/test/fixtures/option-manager/not-a-preset.js @@ -0,0 +1,3 @@ +module.exports = function () { + throw new Error('Not a real preset'); +} diff --git a/packages/babel-core/test/option-manager.js b/packages/babel-core/test/option-manager.js index 222275ff9c..c577723bdb 100644 --- a/packages/babel-core/test/option-manager.js +++ b/packages/babel-core/test/option-manager.js @@ -1,6 +1,7 @@ var assert = require("assert"); var OptionManager = require("../lib/transformation/file/options/option-manager"); var Logger = require("../lib/transformation/file/logger"); +var path = require("path"); suite("option-manager", function () { suite("memoisePluginContainer", function () { @@ -43,5 +44,16 @@ suite("option-manager", function () { /Using removed Babel 5 option: base.auxiliaryComment - Use `auxiliaryCommentBefore` or `auxiliaryCommentAfter`/ ); }) + test("throws for resolved but erroring preset", function() { + return assert.throws( + function () { + var opt = new OptionManager(new Logger(null, "unknown")); + opt.init({ + 'presets': [path.resolve(__dirname, "fixtures", "option-manager", "not-a-preset")] + }); + }, + /While processing preset: .*option-manager(?:\/|\\\\)not-a-preset\.js/ + ); + }) }); });