Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for preset organization shortcuts #4542

Merged
merged 3 commits into from Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -261,6 +261,18 @@ export default class OptionManager {
try {
if (typeof val === "string") {
presetLoc = resolve(`babel-preset-${val}`, dirname) || resolve(val, dirname);
Copy link
Member

@danez danez Sep 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

babel-preset-${val} will never be a valid name if val contains an @, but I think doing a check if it contains an @ and then doing different things makes it even more complex and it works right now anyway.


// 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/
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the best test but the error is correct

);
});

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