Functions for determining which features, plugins, or transforms are required based on target browser support and compatibility data.
Main function that filters a feature list based on target compatibility, includes/excludes, and syntax mapping.
/**
* Filters feature list based on target compatibility and inclusion rules
* @param list - Map of feature names to their browser support data
* @param includes - Set of features to force include regardless of target support
* @param excludes - Set of features to force exclude regardless of target support
* @param targets - Target environments and their versions
* @param defaultIncludes - Features to include by default (can be overridden by excludes)
* @param defaultExcludes - Features to exclude by default (can be overridden by includes)
* @param pluginSyntaxMap - Map plugin names to their syntax equivalents
* @returns Set of feature names that should be included
*/
function filterItems(
list: { [feature: string]: Targets },
includes: Set<string>,
excludes: Set<string>,
targets: Targets,
defaultIncludes: Array<string> | null,
defaultExcludes?: Array<string> | null,
pluginSyntaxMap?: Map<string, string | null>
): Set<string>;Usage Examples:
import { filterItems } from "@babel/helper-compilation-targets";
// Example feature support data
const features = {
"arrow-functions": { chrome: "45.0.0", firefox: "22.0.0" },
"async-functions": { chrome: "55.0.0", firefox: "52.0.0" },
"object-spread": { chrome: "60.0.0", firefox: "55.0.0" }
};
const targets = { chrome: "50.0.0", firefox: "50.0.0" };
const required = filterItems(
features,
new Set(), // no forced includes
new Set(), // no forced excludes
targets,
null // no default includes
);
// Result: Set(["async-functions", "object-spread"])
// arrow-functions is not required since targets support it
// With forced includes/excludes
const withOptions = filterItems(
features,
new Set(["arrow-functions"]), // force include
new Set(["object-spread"]), // force exclude
targets,
["async-functions"] // default includes
);
// Result: Set(["arrow-functions", "async-functions"])Determines if target environments support a specific feature based on compatibility data.
/**
* Checks if targets support a specific feature based on compatibility data
* @param target - Target environments and their versions
* @param support - Feature support data mapping environments to minimum versions
* @returns True if all target environments support the feature
*/
function targetsSupported(target: Targets, support: Targets): boolean;Usage Examples:
import { targetsSupported } from "@babel/helper-compilation-targets";
const targets = { chrome: "50.0.0", firefox: "45.0.0" };
const asyncSupport = { chrome: "55.0.0", firefox: "52.0.0" };
console.log(targetsSupported(targets, asyncSupport)); // false
// Chrome 50 < 55 and Firefox 45 < 52, so not supported
const arrowSupport = { chrome: "45.0.0", firefox: "22.0.0" };
console.log(targetsSupported(targets, arrowSupport)); // true
// Both Chrome 50 >= 45 and Firefox 45 >= 22, so supportedChecks whether a specific feature is required for the given targets.
/**
* Determines if a feature/plugin is required for given targets
* @param name - Feature/plugin name to check
* @param targets - Target environments and versions
* @param options - Configuration for compatibility checking
* @returns True if feature is required (not supported by targets)
*/
function isRequired(
name: string,
targets: Targets,
options?: {
/** Compatibility data (defaults to @babel/compat-data plugins data) */
compatData?: { [feature: string]: Targets };
/** Set of features to force include */
includes?: Set<string>;
/** Set of features to force exclude */
excludes?: Set<string>;
}
): boolean;Usage Examples:
import { isRequired } from "@babel/helper-compilation-targets";
const targets = { chrome: "50.0.0", firefox: "45.0.0" };
// Using default Babel compatibility data
console.log(isRequired("async-functions", targets)); // true (not supported in these versions)
console.log(isRequired("arrow-functions", targets)); // false (supported in these versions)
// With custom compatibility data
const customCompatData = {
"my-feature": { chrome: "60.0.0", firefox: "55.0.0" }
};
console.log(isRequired("my-feature", targets, {
compatData: customCompatData
})); // true (requires chrome 60+, firefox 55+)
// With forced includes/excludes
console.log(isRequired("arrow-functions", targets, {
includes: new Set(["arrow-functions"])
})); // true (forced include overrides support check)
console.log(isRequired("async-functions", targets, {
excludes: new Set(["async-functions"])
})); // false (forced exclude overrides requirement)The pluginSyntaxMap parameter in filterItems allows mapping from plugin names to their syntax equivalents. This is useful when a feature plugin is not required (because browsers support it), but the syntax plugin might still be needed.
Example Usage:
const syntaxMap = new Map([
["plugin-async-functions", "syntax-async-functions"],
["plugin-object-spread", "syntax-object-spread"]
]);
const required = filterItems(
compatibilityData,
new Set(),
new Set(),
targets,
null,
null,
syntaxMap
);
// If plugin-async-functions is not required, but syntax is needed,
// the result will include "syntax-async-functions" insteadBy default, the functions use compatibility data from @babel/compat-data/plugins, but you can provide custom compatibility data for checking support of custom features or plugins.
The compatibility data format maps feature names to Targets objects indicating the minimum version each environment supports the feature.