Add support for preset organization shortcuts (#4542)

* add support for @org shortcats, fixes #4362

* add shortcut test

* fixes
This commit is contained in:
Henry Zhu
2016-09-21 16:44:13 -04:00
committed by GitHub
parent f3a6e4b0ae
commit c07919bc9b
2 changed files with 34 additions and 0 deletions

View File

@@ -261,6 +261,18 @@ export default class OptionManager {
try {
if (typeof val === "string") {
presetLoc = resolve(`babel-preset-${val}`, dirname) || resolve(val, dirname);
// trying to resolve @organization shortcat
// @foo/es2015 -> @foo/babel-preset-es2015
if (!presetLoc) {
let matches = val.match(/^(@[^/]+)\/(.+)$/);
if (matches) {
let [, orgName, presetPath] = matches;
val = `${orgName}/babel-preset-${presetPath}`;
presetLoc = resolve(val, dirname);
}
}
if (!presetLoc) {
throw new Error(`Couldn't find preset ${JSON.stringify(val)} relative to directory ` +
JSON.stringify(dirname));

View File

@@ -185,6 +185,28 @@ suite("api", function () {
});
test("handles preset shortcuts (adds babel-preset-)", function () {
return assert.throws(
function () {
babel.transform("", {
presets: ["@babel/es2015"]
});
},
/Couldn\'t find preset \"\@babel\/babel\-preset\-es2015\" relative to directory/
);
});
test("handles preset shortcuts 2 (adds babel-preset-)", function () {
return assert.throws(
function () {
babel.transform("", {
presets: ["@babel/react/optimizations"]
});
},
/Couldn\'t find preset \"\@babel\/babel\-preset\-react\/optimizations\" relative to directory/
);
});
test("source map merging", function () {
var result = babel.transform([
'function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }',