Skip to content
This repository has been archived by the owner on May 11, 2018. It is now read-only.

Commit

Permalink
Allow use babel-plugin- prefix for include and exclude. (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
yavorsky authored and existentialism committed Apr 4, 2017
1 parent b2057c0 commit f901427
Show file tree
Hide file tree
Showing 3 changed files with 1,042 additions and 75 deletions.
18 changes: 16 additions & 2 deletions src/normalize-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export const validateIncludesAndExcludes = (opts = [], type) => {
return opts;
};

export const normalizePluginName = (plugin) =>
plugin.replace(/^babel-plugin-/, "");

export const normalizePluginNames = (plugins) =>
plugins.map(normalizePluginName);

export const checkDuplicateIncludeExcludes = (include = [], exclude = []) => {
const duplicates = include.filter(
(opt) => exclude.indexOf(opt) >= 0
Expand Down Expand Up @@ -85,12 +91,20 @@ export default function normalizeOptions(opts) {
a time`
);

checkDuplicateIncludeExcludes(opts.whitelist || opts.include, opts.exclude);
if (opts.exclude) {
opts.exclude = normalizePluginNames(opts.exclude);
}

if (opts.whitelist || opts.include) {
opts.include = normalizePluginNames(opts.whitelist || opts.include);
}

checkDuplicateIncludeExcludes(opts.include, opts.exclude);

return {
debug: opts.debug,
exclude: validateIncludesAndExcludes(opts.exclude, "exclude"),
include: validateIncludesAndExcludes(opts.whitelist || opts.include, "include"),
include: validateIncludesAndExcludes(opts.include, "include"),
loose: validateLooseOption(opts.loose),
moduleType: validateModulesOption(opts.modules),
targets: opts.targets,
Expand Down
52 changes: 51 additions & 1 deletion test/normalize-options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,39 @@ const {
checkDuplicateIncludeExcludes,
validateIncludesAndExcludes,
validateLooseOption,
validateModulesOption
validateModulesOption,
normalizePluginNames
} = normalizeOptions;

describe("normalize-options", () => {
describe("normalizeOptions", () => {
it("should return normalized `include` and `exclude`", () => {
const normalized = normalizeOptions.default({
include: [
"babel-plugin-transform-es2015-spread",
"transform-es2015-classes"
]
});
assert.deepEqual(normalized.include, [
"transform-es2015-spread",
"transform-es2015-classes"
]);
});

it("should throw if duplicate names in `include` and `exclude`", () => {
const normalizeWithSameIncludes = () => {
normalizeOptions.default({
include: [
"babel-plugin-transform-es2015-spread",
],
exclude: [
"transform-es2015-spread"
]
});
};
assert.throws(normalizeWithSameIncludes, Error);
});
});
describe("validateLooseOption", () => {
it("`undefined` option returns false", () => {
assert(validateLooseOption() === false);
Expand Down Expand Up @@ -51,6 +80,27 @@ describe("normalize-options", () => {
});
});

describe("normalizePluginNames", function() {
it("should drop `babel-plugin-` prefix if needed", function() {
assert.deepEqual(
normalizePluginNames([
"babel-plugin-transform-es2015-object-super",
"transform-es2015-parameters"
]),
["transform-es2015-object-super", "transform-es2015-parameters"]
);
});

it("should not throw if no duplicate names in both", function() {
assert.doesNotThrow(() => {
checkDuplicateIncludeExcludes(
["transform-regenerator"],
["map"]
);
}, Error);
});
});

describe("validateModulesOption", () => {
it("`undefined` option returns commonjs", () => {
assert(validateModulesOption() === "commonjs");
Expand Down
Loading

0 comments on commit f901427

Please sign in to comment.