Allow @foo/babel-plugin as an unexpanded plugin name, and @foo as a shorthand for it.

This commit is contained in:
Logan Smyth 2018-06-03 16:52:13 -07:00
parent 5895277b32
commit add5f8d0fe
4 changed files with 71 additions and 2 deletions

View File

@ -15,8 +15,9 @@ const BABEL_PLUGIN_PREFIX_RE = /^(?!@|module:|[^/]+\/|babel-plugin-)/;
const BABEL_PRESET_PREFIX_RE = /^(?!@|module:|[^/]+\/|babel-preset-)/;
const BABEL_PLUGIN_ORG_RE = /^(@babel\/)(?!plugin-|[^/]+\/)/;
const BABEL_PRESET_ORG_RE = /^(@babel\/)(?!preset-|[^/]+\/)/;
const OTHER_PLUGIN_ORG_RE = /^(@(?!babel\/)[^/]+\/)(?!babel-plugin-|[^/]+\/)/;
const OTHER_PRESET_ORG_RE = /^(@(?!babel\/)[^/]+\/)(?!babel-preset-|[^/]+\/)/;
const OTHER_PLUGIN_ORG_RE = /^(@(?!babel\/)[^/]+\/)(?!babel-plugin(?:-|\/|$)|[^/]+\/)/;
const OTHER_PRESET_ORG_RE = /^(@(?!babel\/)[^/]+\/)(?!babel-preset(?:-|\/|$)|[^/]+\/)/;
const OTHER_ORG_DEFAULT_RE = /^(@(?!babel$)[^/]+)$/;
export function resolvePlugin(name: string, dirname: string): string | null {
return resolveStandardizedName("plugin", name, dirname);
@ -80,6 +81,8 @@ function standardizeName(type: "plugin" | "preset", name: string) {
isPreset ? OTHER_PRESET_ORG_RE : OTHER_PLUGIN_ORG_RE,
`$1babel-${type}-`,
)
// @foo -> @foo/babel-preset
.replace(OTHER_ORG_DEFAULT_RE, `$1/babel-${type}`)
// module:mypreset -> mypreset
.replace(EXACT_RE, "")
);

View File

@ -0,0 +1,3 @@
module.exports = function() {
return {};
};

View File

@ -0,0 +1,3 @@
module.exports = function() {
return {};
};

View File

@ -154,6 +154,66 @@ describe("addon resolution", function() {
});
});
it("should find @foo/babel-plugin when specified", function() {
process.chdir("foo-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: ["@foo/babel-plugin"],
});
});
it("should find @foo/babel-preset when specified", function() {
process.chdir("foo-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: ["@foo/babel-preset"],
});
});
it("should find @foo/babel-plugin/index when specified", function() {
process.chdir("foo-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: ["@foo/babel-plugin/index"],
});
});
it("should find @foo/babel-preset/index when specified", function() {
process.chdir("foo-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: ["@foo/babel-preset/index"],
});
});
it("should find @foo/babel-plugin when just scope given", function() {
process.chdir("foo-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
plugins: ["@foo"],
});
});
it("should find @foo/babel-preset when just scope given", function() {
process.chdir("foo-org-paths");
babel.transform("", {
filename: "filename.js",
babelrc: false,
presets: ["@foo"],
});
});
it("should find relative path presets", function() {
process.chdir("relative-paths");