Core functionality for resolving and normalizing compilation targets from various input formats including browserslist queries, individual environment versions, and ES modules support.
The primary function that resolves compilation targets from various input formats into a normalized targets object.
/**
* Resolves compilation targets from various input formats
* @param inputTargets - Target configuration including browsers, esmodules, and individual targets
* @param options - Additional options for target resolution
* @returns Normalized targets object with version strings
*/
function getTargets(inputTargets?: InputTargets, options?: GetTargetsOption): Targets;
interface InputTargets extends Targets {
/** Browserslist query string or array of queries */
browsers?: string | ReadonlyArray<string>;
/** ES modules support flag - true, false, or "intersect" */
esmodules?: boolean | "intersect";
}
interface GetTargetsOption {
/** Directory path to start searching for browserslist config (default: ".") */
configPath?: string;
/** Explicit path to browserslist config file */
configFile?: string;
/** Environment name for browserslist config */
browserslistEnv?: string;
/** Skip automatic browserslist config loading */
ignoreBrowserslistConfig?: boolean;
/** Callback when browserslist config is found */
onBrowserslistConfigFound?: (configFile: string) => void;
}
interface Targets {
[target in Target]?: string;
}
type Target =
| "node" | "deno" | "chrome" | "opera" | "edge" | "firefox"
| "safari" | "ie" | "ios" | "android" | "electron" | "samsung" | "rhino" | "opera_mobile";Usage Examples:
import getTargets from "@babel/helper-compilation-targets";
// Basic target specification
const targets1 = getTargets({
chrome: 60,
firefox: "55.0",
node: "14.15.0"
});
// Result: { chrome: "60.0.0", firefox: "55.0.0", node: "14.15.0" }
// Using browserslist queries
const targets2 = getTargets({
browsers: ["last 2 versions", "> 1%"]
});
// ES modules support
const targets3 = getTargets({
browsers: "defaults",
esmodules: true
});
// With configuration options
const targets4 = getTargets({
browsers: "defaults"
}, {
configPath: "./my-app",
browserslistEnv: "production",
onBrowserslistConfigFound: (file) => console.log(`Using config: ${file}`)
});The getTargets function provides special handling for Node.js targets:
node: true - Uses current Node.js version (process.versions.node)node: "current" - Same as node: truenode: "14.15.0" - Uses specified version stringValidates that browser queries are in the correct format for browserslist.
/**
* Validates that browsers input is a valid browserslist query
* @param browsers - Potential browserslist query
* @returns True if valid query format
*/
function isBrowsersQueryValid(browsers: unknown): boolean;Usage Examples:
import { isBrowsersQueryValid } from "@babel/helper-compilation-targets";
console.log(isBrowsersQueryValid("last 2 versions")); // true
console.log(isBrowsersQueryValid(["chrome > 60", "firefox > 55"])); // true
console.log(isBrowsersQueryValid(123)); // false
console.log(isBrowsersQueryValid({ chrome: 60 })); // falseConstants and utilities exported by the package for target validation and management.
const TargetNames: {
readonly node: "node";
readonly deno: "deno";
readonly chrome: "chrome";
readonly opera: "opera";
readonly edge: "edge";
readonly firefox: "firefox";
readonly safari: "safari";
readonly ie: "ie";
readonly ios: "ios";
readonly android: "android";
readonly electron: "electron";
readonly samsung: "samsung";
readonly rhino: "rhino";
readonly opera_mobile: "opera_mobile";
};
const unreleasedLabels: {
readonly safari: "tp";
};
/**
* Validates that browsers input is a valid browserslist query
* @param browsers - Potential browserslist query
* @returns True if valid query format
*/
function isBrowsersQueryValid(browsers: unknown): boolean;Usage Examples:
import { TargetNames, unreleasedLabels, isBrowsersQueryValid } from "@babel/helper-compilation-targets";
// Check valid target names
console.log(TargetNames.chrome); // "chrome"
console.log(TargetNames.node); // "node"
// Check for unreleased labels
console.log(unreleasedLabels.safari); // "tp"
// Validate browser queries
console.log(isBrowsersQueryValid("last 2 versions")); // true
console.log(isBrowsersQueryValid(["chrome > 60", "firefox > 55"])); // true
console.log(isBrowsersQueryValid(123)); // falseThe getTargets function automatically loads browserslist configuration from several sources in order of priority:
BROWSERSLIST environment variableBROWSERSLIST_CONFIG environment variable (explicit config file path)configPathBrowserslist Config Discovery:
.browserslistrc filesbrowserslist key in package.jsonEnvironment Support:
browserslistEnv option specifies which environment section to useproduction, developmentThe esmodules option provides special handling for targeting browsers with ES modules support:
esmodules: true (Babel 7): Completely replaces browsers option with ES modules supporting browsersesmodules: true (Babel 8): Intersects with browsers option (higher versions win)esmodules: "intersect": Explicitly intersects with browsers option regardless of Babel versionES Modules Browser Support: The function uses compatibility data to determine which browser versions support ES modules and generates appropriate browserslist queries like:
chrome >= 61firefox >= 60safari >= 10.1