If loading a preset fails, show its name/path (#4506) (#4517)

This commit is contained in:
Moti Zilberman 2016-09-18 06:01:08 +03:00 committed by Henry Zhu
parent cbbc1c7333
commit e64d86c1eb
3 changed files with 47 additions and 25 deletions

View File

@ -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;
});
}

View File

@ -0,0 +1,3 @@
module.exports = function () {
throw new Error('Not a real preset');
}

View File

@ -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/
);
})
});
});