diff --git a/packages/babel-helper-compilation-targets/src/filter-items.js b/packages/babel-helper-compilation-targets/src/filter-items.js index 3ef007131d40..ac8809433bb9 100644 --- a/packages/babel-helper-compilation-targets/src/filter-items.js +++ b/packages/babel-helper-compilation-targets/src/filter-items.js @@ -2,6 +2,8 @@ import semver from "semver"; +import pluginsCompatData from "@babel/compat-data/plugins"; + import type { Targets } from "./types"; import { getLowestImplementedVersion, @@ -55,6 +57,24 @@ export function targetsSupported(target: Targets, support: Targets) { return unsupportedEnvironments.length === 0; } +export function isRequired( + name: string, + targets: Targets, + { + compatData = pluginsCompatData, + includes, + excludes, + }: { + compatData?: { [feature: string]: Targets }, + includes?: Set, + excludes?: Set, + } = {}, +) { + if (excludes && excludes.has(name)) return false; + if (includes && includes.has(name)) return true; + return !targetsSupported(targets, compatData[name]); +} + export default function filterItems( list: { [feature: string]: Targets }, includes: Set, @@ -65,12 +85,10 @@ export default function filterItems( pluginSyntaxMap?: Map, ) { const result = new Set(); + const options = { compatData: list, includes, excludes }; for (const item in list) { - if ( - !excludes.has(item) && - (!targetsSupported(targets, list[item]) || includes.has(item)) - ) { + if (isRequired(item, targets, options)) { result.add(item); } else if (pluginSyntaxMap) { const shippedProposalsSyntax = pluginSyntaxMap.get(item); diff --git a/packages/babel-helper-compilation-targets/src/index.js b/packages/babel-helper-compilation-targets/src/index.js index 232027cb340c..747eb431211c 100644 --- a/packages/babel-helper-compilation-targets/src/index.js +++ b/packages/babel-helper-compilation-targets/src/index.js @@ -19,7 +19,7 @@ export type { Targets }; export { prettifyTargets } from "./pretty"; export { getInclusionReasons } from "./debug"; -export { default as filterItems, targetsSupported } from "./filter-items"; +export { default as filterItems, isRequired } from "./filter-items"; export { unreleasedLabels } from "./targets"; const browserslistDefaults = browserslist.defaults; diff --git a/packages/babel-helper-compilation-targets/test/targets-supported.js b/packages/babel-helper-compilation-targets/test/targets-supported.js index ae9bd9a2d578..fd018172ef29 100644 --- a/packages/babel-helper-compilation-targets/test/targets-supported.js +++ b/packages/babel-helper-compilation-targets/test/targets-supported.js @@ -1,6 +1,6 @@ "use strict"; -const { targetsSupported } = require("../lib"); +const { targetsSupported } = require("../lib/filter-items"); describe("targetsSupported", () => { const MAX_VERSION = `${Number.MAX_SAFE_INTEGER}.0.0`; diff --git a/packages/babel-preset-env/src/index.js b/packages/babel-preset-env/src/index.js index 5a5412053cd2..19cc41121464 100644 --- a/packages/babel-preset-env/src/index.js +++ b/packages/babel-preset-env/src/index.js @@ -20,7 +20,7 @@ import removeRegeneratorEntryPlugin from "./polyfills/regenerator/entry-plugin"; import getTargets, { prettifyTargets, filterItems, - targetsSupported, + isRequired, type Targets, } from "@babel/helper-compilation-targets"; import availablePlugins from "./available-plugins"; @@ -31,8 +31,10 @@ import typeof ModuleTransformationsType from "./module-transformations"; import type { BuiltInsOption, ModuleOption } from "./types"; // TODO: Remove in Babel 8 -export function isPluginRequired(target: Targets, support: Targets) { - return !targetsSupported(target, support); +export function isPluginRequired(targets: Targets, support: Targets) { + return !isRequired("fake-name", targets, { + compatData: { "fake-name": support }, + }); } const pluginListWithoutProposals = filterStageFromList(