From d4f4ea4f80674250027783afdae220578b72f24f Mon Sep 17 00:00:00 2001 From: Nikita Gusakov Date: Thu, 8 Sep 2016 17:41:05 +0300 Subject: [PATCH 1/3] add support for @org shortcats, fixes #4362 --- .../src/transformation/file/options/option-manager.js | 11 +++++++++++ 1 file changed, 11 insertions(+) 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 896e5225ef60..a277b4aa3775 100644 --- a/packages/babel-core/src/transformation/file/options/option-manager.js +++ b/packages/babel-core/src/transformation/file/options/option-manager.js @@ -261,6 +261,17 @@ 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; + presetLoc = resolve(`${orgName}/babel-preset-${presetPath}`, dirname); + } + } + if (!presetLoc) { throw new Error(`Couldn't find preset ${JSON.stringify(val)} relative to directory ` + JSON.stringify(dirname)); From 16e4ba39176380136cd7e1ede23edd89b64c37a5 Mon Sep 17 00:00:00 2001 From: Nikita Gusakov Date: Fri, 9 Sep 2016 13:42:31 +0300 Subject: [PATCH 2/3] add shortcut test --- packages/babel-core/test/api.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 3e79693e2bc3..ddc8a064bd5e 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -185,6 +185,14 @@ suite("api", function () { }); + test("preset shortcut doesn\'t throw errors", function () { + return transformAsync("", { + presets: ["@babel/es2015", "@babel/react/optimizations"] + }).then(function (result) { + assert.ok(result.options.presets); + }); + }); + 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"); } }', From 6eacdf1fabef145183d003433fafb735a28d363f Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Wed, 21 Sep 2016 15:15:54 -0400 Subject: [PATCH 3/3] fixes --- .../file/options/option-manager.js | 3 ++- packages/babel-core/test/api.js | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) 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 a277b4aa3775..ad4fbf4ed966 100644 --- a/packages/babel-core/src/transformation/file/options/option-manager.js +++ b/packages/babel-core/src/transformation/file/options/option-manager.js @@ -268,7 +268,8 @@ export default class OptionManager { let matches = val.match(/^(@[^/]+)\/(.+)$/); if (matches) { let [, orgName, presetPath] = matches; - presetLoc = resolve(`${orgName}/babel-preset-${presetPath}`, dirname); + val = `${orgName}/babel-preset-${presetPath}`; + presetLoc = resolve(val, dirname); } } diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index ddc8a064bd5e..bae3b506b128 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -185,12 +185,26 @@ suite("api", function () { }); - test("preset shortcut doesn\'t throw errors", function () { - return transformAsync("", { - presets: ["@babel/es2015", "@babel/react/optimizations"] - }).then(function (result) { - assert.ok(result.options.presets); - }); + 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 () {