Skip to content

Commit

Permalink
Add support for preset organization shortcuts (#4542)
Browse files Browse the repository at this point in the history
* add support for @org shortcats, fixes #4362

* add shortcut test

* fixes
  • Loading branch information
hzoo committed Sep 21, 2016
1 parent f3a6e4b commit c07919b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Expand Up @@ -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));
Expand Down
22 changes: 22 additions & 0 deletions packages/babel-core/test/api.js
Expand Up @@ -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"); } }',
Expand Down

0 comments on commit c07919b

Please sign in to comment.