Fix handling scoped packages in preset-env include/exclude options (#9219)

This commit is contained in:
Brian Ng 2018-12-27 09:54:28 -06:00 committed by GitHub
parent 3f9a1c08cc
commit ea1c436ea1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 11 deletions

View File

@ -76,7 +76,7 @@ const validBrowserslistTargets = [
]; ];
export const normalizePluginName = (plugin: string): string => export const normalizePluginName = (plugin: string): string =>
plugin.replace(/^babel-plugin-/, ""); plugin.replace(/^(@babel\/|babel-)(plugin-)?/, "");
export const checkDuplicateIncludeExcludes = ( export const checkDuplicateIncludeExcludes = (
include: Array<string> = [], include: Array<string> = [],

View File

@ -0,0 +1 @@
class Foo {}

View File

@ -0,0 +1,14 @@
{
"presets": [
[
"../../../../lib",
{
"targets": {
"chrome": 61
},
"include": ["@babel/plugin-transform-classes"],
"modules": false
}
]
]
}

View File

@ -0,0 +1,5 @@
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
let Foo = function Foo() {
_classCallCheck(this, Foo);
};

View File

@ -12,11 +12,30 @@ describe("normalize-options", () => {
describe("normalizeOptions", () => { describe("normalizeOptions", () => {
it("should return normalized `include` and `exclude`", () => { it("should return normalized `include` and `exclude`", () => {
const normalized = normalizeOptions.default({ const normalized = normalizeOptions.default({
include: ["babel-plugin-transform-spread", "transform-classes"], include: [
"babel-plugin-transform-spread",
"transform-classes",
"@babel/plugin-transform-unicode-regex",
"@babel/transform-block-scoping",
],
exclude: [
"babel-plugin-transform-for-of",
"transform-parameters",
"@babel/plugin-transform-regenerator",
"@babel/transform-new-target",
],
}); });
expect(normalized.include).toEqual([ expect(normalized.include).toEqual([
"transform-spread", "transform-spread",
"transform-classes", "transform-classes",
"transform-unicode-regex",
"transform-block-scoping",
]);
expect(normalized.exclude).toEqual([
"transform-for-of",
"transform-parameters",
"transform-regenerator",
"transform-new-target",
]); ]);
}); });
@ -25,15 +44,26 @@ describe("normalize-options", () => {
expect(normalized).toBe("prefix-babel-plugin-postfix"); expect(normalized).toBe("prefix-babel-plugin-postfix");
}); });
it("should throw if duplicate names in `include` and `exclude`", () => { test.each`
const normalizeWithSameIncludes = () => { include | exclude
normalizeOptions.default({ ${["babel-plugin-transform-spread"]} | ${["transform-spread"]}
include: ["babel-plugin-transform-spread"], ${["@babel/plugin-transform-spread"]} | ${["transform-spread"]}
exclude: ["transform-spread"], ${["transform-spread"]} | ${["babel-plugin-transform-spread"]}
}); ${["transform-spread"]} | ${["@babel/plugin-transform-spread"]}
}; ${["babel-plugin-transform-spread"]} | ${["@babel/plugin-transform-spread"]}
expect(normalizeWithSameIncludes).toThrow(); ${["@babel/plugin-transform-spread"]} | ${["babel-plugin-transform-spread"]}
}); ${["@babel/plugin-transform-spread"]} | ${["@babel/transform-spread"]}
${["@babel/transform-spread"]} | ${["@babel/plugin-transform-spread"]}
${["babel-plugin-transform-spread"]} | ${["@babel/transform-spread"]}
${["@babel/transform-spread"]} | ${["babel-plugin-transform-spread"]}
`(
"should throw if with includes $include and excludes $exclude",
({ include, exclude }) => {
expect(() =>
normalizeOptions.default({ include, exclude }),
).toThrowError(/were found in both/);
},
);
}); });
describe("Config format validation", () => { describe("Config format validation", () => {