convert @babel/helper-compilation-targets to typescript (#13218)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
parent
12190042e6
commit
8b1bcd1079
@ -216,3 +216,7 @@ declare module "@babel/helper-module-transforms" {
|
|||||||
declare module "@babel/plugin-transform-classes" {
|
declare module "@babel/plugin-transform-classes" {
|
||||||
declare module.exports: any;
|
declare module.exports: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare module "@babel/helper-compilation-targets" {
|
||||||
|
declare module.exports: any;
|
||||||
|
}
|
||||||
|
|||||||
@ -30,6 +30,8 @@
|
|||||||
"@babel/core": "^7.0.0"
|
"@babel/core": "^7.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "workspace:*"
|
"@babel/core": "workspace:*",
|
||||||
|
"@babel/helper-plugin-test-runner": "workspace:*",
|
||||||
|
"@types/semver": "^5.5.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
// @flow
|
|
||||||
|
|
||||||
import semver from "semver";
|
import semver from "semver";
|
||||||
import { prettifyVersion } from "./pretty";
|
import { prettifyVersion } from "./pretty";
|
||||||
import {
|
import {
|
||||||
@ -7,16 +5,16 @@ import {
|
|||||||
isUnreleasedVersion,
|
isUnreleasedVersion,
|
||||||
getLowestImplementedVersion,
|
getLowestImplementedVersion,
|
||||||
} from "./utils";
|
} from "./utils";
|
||||||
import type { Targets } from "./types";
|
import type { Target, Targets } from "./types";
|
||||||
|
|
||||||
export function getInclusionReasons(
|
export function getInclusionReasons(
|
||||||
item: string,
|
item: string,
|
||||||
targetVersions: Targets,
|
targetVersions: Targets,
|
||||||
list: { [key: string]: Targets },
|
list: { [key: string]: Targets },
|
||||||
) {
|
) {
|
||||||
const minVersions = list[item] || {};
|
const minVersions = list[item] || ({} as Targets);
|
||||||
|
|
||||||
return Object.keys(targetVersions).reduce((result, env) => {
|
return (Object.keys(targetVersions) as Target[]).reduce((result, env) => {
|
||||||
const minVersion = getLowestImplementedVersion(minVersions, env);
|
const minVersion = getLowestImplementedVersion(minVersions, env);
|
||||||
const targetVersion = targetVersions[env];
|
const targetVersion = targetVersions[env];
|
||||||
|
|
||||||
@ -1,5 +1,3 @@
|
|||||||
// @flow
|
|
||||||
|
|
||||||
import semver from "semver";
|
import semver from "semver";
|
||||||
|
|
||||||
import pluginsCompatData from "@babel/compat-data/plugins";
|
import pluginsCompatData from "@babel/compat-data/plugins";
|
||||||
@ -12,7 +10,7 @@ import {
|
|||||||
} from "./utils";
|
} from "./utils";
|
||||||
|
|
||||||
export function targetsSupported(target: Targets, support: Targets) {
|
export function targetsSupported(target: Targets, support: Targets) {
|
||||||
const targetEnvironments = Object.keys(target);
|
const targetEnvironments = Object.keys(target) as Array<keyof Targets>;
|
||||||
|
|
||||||
if (targetEnvironments.length === 0) {
|
if (targetEnvironments.length === 0) {
|
||||||
return false;
|
return false;
|
||||||
@ -65,9 +63,9 @@ export function isRequired(
|
|||||||
includes,
|
includes,
|
||||||
excludes,
|
excludes,
|
||||||
}: {
|
}: {
|
||||||
compatData?: { [feature: string]: Targets },
|
compatData?: { [feature: string]: Targets };
|
||||||
includes?: Set<string>,
|
includes?: Set<string>;
|
||||||
excludes?: Set<string>,
|
excludes?: Set<string>;
|
||||||
} = {},
|
} = {},
|
||||||
) {
|
) {
|
||||||
if (excludes?.has(name)) return false;
|
if (excludes?.has(name)) return false;
|
||||||
@ -1,5 +1,3 @@
|
|||||||
// @flow
|
|
||||||
|
|
||||||
import browserslist from "browserslist";
|
import browserslist from "browserslist";
|
||||||
import { findSuggestion } from "@babel/helper-validator-option";
|
import { findSuggestion } from "@babel/helper-validator-option";
|
||||||
import browserModulesData from "@babel/compat-data/native-modules";
|
import browserModulesData from "@babel/compat-data/native-modules";
|
||||||
@ -26,6 +24,7 @@ export { TargetNames };
|
|||||||
|
|
||||||
const ESM_SUPPORT = browserModulesData["es6.module"];
|
const ESM_SUPPORT = browserModulesData["es6.module"];
|
||||||
|
|
||||||
|
declare const PACKAGE_JSON: { name: string; version: string };
|
||||||
const v = new OptionValidator(PACKAGE_JSON.name);
|
const v = new OptionValidator(PACKAGE_JSON.name);
|
||||||
|
|
||||||
function validateTargetNames(targets: Targets): TargetsTuple {
|
function validateTargetNames(targets: Targets): TargetsTuple {
|
||||||
@ -39,17 +38,17 @@ function validateTargetNames(targets: Targets): TargetsTuple {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (targets: any);
|
return targets as any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isBrowsersQueryValid(browsers: mixed): boolean %checks {
|
export function isBrowsersQueryValid(browsers: unknown): boolean {
|
||||||
return (
|
return (
|
||||||
typeof browsers === "string" ||
|
typeof browsers === "string" ||
|
||||||
(Array.isArray(browsers) && browsers.every(b => typeof b === "string"))
|
(Array.isArray(browsers) && browsers.every(b => typeof b === "string"))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateBrowsers(browsers: Browsers | void) {
|
function validateBrowsers(browsers: Browsers | undefined) {
|
||||||
v.invariant(
|
v.invariant(
|
||||||
browsers === undefined || isBrowsersQueryValid(browsers),
|
browsers === undefined || isBrowsersQueryValid(browsers),
|
||||||
`'${String(browsers)}' is not a valid browserslist query`,
|
`'${String(browsers)}' is not a valid browserslist query`,
|
||||||
@ -59,7 +58,7 @@ function validateBrowsers(browsers: Browsers | void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getLowestVersions(browsers: Array<string>): Targets {
|
function getLowestVersions(browsers: Array<string>): Targets {
|
||||||
return browsers.reduce((all: Object, browser: string): Object => {
|
return browsers.reduce((all: any, browser: string): any => {
|
||||||
const [browserName, browserVersion] = browser.split(" ");
|
const [browserName, browserVersion] = browser.split(" ");
|
||||||
const normalizedBrowserName = browserNameMap[browserName];
|
const normalizedBrowserName = browserNameMap[browserName];
|
||||||
|
|
||||||
@ -102,7 +101,7 @@ function getLowestVersions(browsers: Array<string>): Targets {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function outputDecimalWarning(
|
function outputDecimalWarning(
|
||||||
decimalTargets: Array<{| target: string, value: string |}>,
|
decimalTargets: Array<{ target: string; value: string }>,
|
||||||
): void {
|
): void {
|
||||||
if (!decimalTargets.length) {
|
if (!decimalTargets.length) {
|
||||||
return;
|
return;
|
||||||
@ -152,7 +151,7 @@ function generateTargets(inputTargets: InputTargets): Targets {
|
|||||||
const input = { ...inputTargets };
|
const input = { ...inputTargets };
|
||||||
delete input.esmodules;
|
delete input.esmodules;
|
||||||
delete input.browsers;
|
delete input.browsers;
|
||||||
return ((input: any): Targets);
|
return input as any as Targets;
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveTargets(queries: Browsers): Targets {
|
function resolveTargets(queries: Browsers): Targets {
|
||||||
@ -162,20 +161,17 @@ function resolveTargets(queries: Browsers): Targets {
|
|||||||
|
|
||||||
type GetTargetsOption = {
|
type GetTargetsOption = {
|
||||||
// This is not the path of the config file, but the path where start searching it from
|
// This is not the path of the config file, but the path where start searching it from
|
||||||
configPath?: string,
|
configPath?: string;
|
||||||
|
|
||||||
// The path of the config file
|
// The path of the config file
|
||||||
configFile?: string,
|
configFile?: string;
|
||||||
|
|
||||||
// The env to pass to browserslist
|
// The env to pass to browserslist
|
||||||
browserslistEnv?: string,
|
browserslistEnv?: string;
|
||||||
|
|
||||||
// true to disable config loading
|
// true to disable config loading
|
||||||
ignoreBrowserslistConfig?: boolean,
|
ignoreBrowserslistConfig?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function getTargets(
|
export default function getTargets(
|
||||||
inputTargets: InputTargets = {},
|
inputTargets: InputTargets = {} as InputTargets,
|
||||||
options: GetTargetsOption = {},
|
options: GetTargetsOption = {},
|
||||||
): Targets {
|
): Targets {
|
||||||
let { browsers, esmodules } = inputTargets;
|
let { browsers, esmodules } = inputTargets;
|
||||||
@ -243,7 +239,7 @@ export default function getTargets(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse remaining targets
|
// Parse remaining targets
|
||||||
const result: Targets = {};
|
const result: Targets = {} as Targets;
|
||||||
const decimalWarnings = [];
|
const decimalWarnings = [];
|
||||||
for (const target of Object.keys(targets).sort()) {
|
for (const target of Object.keys(targets).sort()) {
|
||||||
const value = targets[target];
|
const value = targets[target];
|
||||||
@ -1,5 +1,3 @@
|
|||||||
// @flow
|
|
||||||
|
|
||||||
export const TargetNames = {
|
export const TargetNames = {
|
||||||
node: "node",
|
node: "node",
|
||||||
chrome: "chrome",
|
chrome: "chrome",
|
||||||
@ -1,5 +1,6 @@
|
|||||||
import semver from "semver";
|
import semver from "semver";
|
||||||
import { unreleasedLabels } from "./targets";
|
import { unreleasedLabels } from "./targets";
|
||||||
|
import type { Targets } from "./types";
|
||||||
|
|
||||||
export function prettifyVersion(version: string) {
|
export function prettifyVersion(version: string) {
|
||||||
if (typeof version !== "string") {
|
if (typeof version !== "string") {
|
||||||
@ -32,5 +33,5 @@ export function prettifyTargets(targets: Targets): Targets {
|
|||||||
|
|
||||||
results[target] = value;
|
results[target] = value;
|
||||||
return results;
|
return results;
|
||||||
}, {});
|
}, {} as Targets);
|
||||||
}
|
}
|
||||||
@ -1,5 +1,3 @@
|
|||||||
// @flow
|
|
||||||
|
|
||||||
// Targets
|
// Targets
|
||||||
export type Target =
|
export type Target =
|
||||||
| "node"
|
| "node"
|
||||||
@ -15,24 +13,21 @@ export type Target =
|
|||||||
| "samsung";
|
| "samsung";
|
||||||
|
|
||||||
export type Targets = {
|
export type Targets = {
|
||||||
[target: Target]: string,
|
[target in Target]: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TargetsTuple = {|
|
export type TargetsTuple = {
|
||||||
[target: Target]: string,
|
[target in Target]: string;
|
||||||
|};
|
};
|
||||||
|
|
||||||
export type Browsers = string | $ReadOnlyArray<string>;
|
export type Browsers = string | ReadonlyArray<string>;
|
||||||
|
|
||||||
export type InputTargets = {
|
export type InputTargets = {
|
||||||
...Targets,
|
browsers?: Browsers;
|
||||||
|
|
||||||
browsers?: Browsers,
|
|
||||||
|
|
||||||
// When `true`, this completely replaces the `browsers` option.
|
// When `true`, this completely replaces the `browsers` option.
|
||||||
// When `intersect`, this is intersected with the `browsers`
|
// When `intersect`, this is intersected with the `browsers`
|
||||||
// option (giving the higher browsers as the result).
|
// option (giving the higher browsers as the result).
|
||||||
// TODO(Babel 8): Make `true` behave like `intersect` and
|
// TODO(Babel 8): Make `true` behave like `intersect` and
|
||||||
// remove `intersect`.
|
// remove `intersect`.
|
||||||
esmodules?: boolean | "intersect",
|
esmodules?: boolean | "intersect";
|
||||||
};
|
} & Targets;
|
||||||
@ -1,14 +1,18 @@
|
|||||||
// @flow
|
|
||||||
import semver from "semver";
|
import semver from "semver";
|
||||||
import { OptionValidator } from "@babel/helper-validator-option";
|
import { OptionValidator } from "@babel/helper-validator-option";
|
||||||
import { unreleasedLabels } from "./targets";
|
import { unreleasedLabels } from "./targets";
|
||||||
import type { Target, Targets } from "./types";
|
import type { Target, Targets } from "./types";
|
||||||
|
|
||||||
|
declare const PACKAGE_JSON: { name: string; version: string };
|
||||||
|
|
||||||
const versionRegExp = /^(\d+|\d+.\d+)$/;
|
const versionRegExp = /^(\d+|\d+.\d+)$/;
|
||||||
|
|
||||||
const v = new OptionValidator(PACKAGE_JSON.name);
|
const v = new OptionValidator(PACKAGE_JSON.name);
|
||||||
|
|
||||||
export function semverMin(first: ?string, second: string): string {
|
export function semverMin(
|
||||||
|
first: string | undefined | null,
|
||||||
|
second: string,
|
||||||
|
): string {
|
||||||
return first && semver.lt(first, second) ? first : second;
|
return first && semver.lt(first, second) ? first : second;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,6 +50,7 @@ export function getLowestUnreleased(a: string, b: string, env: string): string {
|
|||||||
const unreleasedLabel = unreleasedLabels[env];
|
const unreleasedLabel = unreleasedLabels[env];
|
||||||
const hasUnreleased = [a, b].some(item => item === unreleasedLabel);
|
const hasUnreleased = [a, b].some(item => item === unreleasedLabel);
|
||||||
if (hasUnreleased) {
|
if (hasUnreleased) {
|
||||||
|
// @ts-expect-error todo(flow->ts): probably a bug - types of a hasUnreleased to not overlap
|
||||||
return a === hasUnreleased ? b : a || b;
|
return a === hasUnreleased ? b : a || b;
|
||||||
}
|
}
|
||||||
return semverMin(a, b);
|
return semverMin(a, b);
|
||||||
@ -27,7 +27,6 @@ export const logPlugin = (
|
|||||||
if (!first) formattedTargets += `,`;
|
if (!first) formattedTargets += `,`;
|
||||||
first = false;
|
first = false;
|
||||||
formattedTargets += ` ${target}`;
|
formattedTargets += ` ${target}`;
|
||||||
// $FlowIgnore
|
|
||||||
if (support[target]) formattedTargets += ` < ${support[target]}`;
|
if (support[target]) formattedTargets += ` < ${support[target]}`;
|
||||||
}
|
}
|
||||||
formattedTargets += ` }`;
|
formattedTargets += ` }`;
|
||||||
|
|||||||
@ -249,11 +249,11 @@ function getLocalTargets(
|
|||||||
`);
|
`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return getTargets(
|
return getTargets((optionsTargets: InputTargets), {
|
||||||
// $FlowIgnore optionsTargets doesn't have an "uglify" property anymore
|
ignoreBrowserslistConfig,
|
||||||
(optionsTargets: InputTargets),
|
configPath,
|
||||||
{ ignoreBrowserslistConfig, configPath, browserslistEnv },
|
browserslistEnv,
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function supportsStaticESM(caller) {
|
function supportsStaticESM(caller) {
|
||||||
|
|||||||
@ -111,7 +111,7 @@ export const checkDuplicateIncludeExcludes = (
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const normalizeTargets = targets => {
|
const normalizeTargets = (targets): $PropertyType<Options, "targets"> => {
|
||||||
// TODO: Allow to use only query or strings as a targets from next breaking change.
|
// TODO: Allow to use only query or strings as a targets from next breaking change.
|
||||||
if (typeof targets === "string" || Array.isArray(targets)) {
|
if (typeof targets === "string" || Array.isArray(targets)) {
|
||||||
return { browsers: targets };
|
return { browsers: targets };
|
||||||
|
|||||||
@ -32,7 +32,7 @@ export type Options = {
|
|||||||
modules: ModuleOption,
|
modules: ModuleOption,
|
||||||
shippedProposals: boolean,
|
shippedProposals: boolean,
|
||||||
spec: boolean,
|
spec: boolean,
|
||||||
targets: { ...InputTargets, uglify?: boolean },
|
targets: { ...InputTargets, uglify?: boolean, esmodules?: boolean },
|
||||||
useBuiltIns: BuiltInsOption,
|
useBuiltIns: BuiltInsOption,
|
||||||
browserslistEnv: string,
|
browserslistEnv: string,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
"./packages/babel-core/src/**/*.ts",
|
"./packages/babel-core/src/**/*.ts",
|
||||||
"./packages/babel-generator/src/**/*.ts",
|
"./packages/babel-generator/src/**/*.ts",
|
||||||
"./packages/babel-helper-annotate-as-pure/src/**/*.ts",
|
"./packages/babel-helper-annotate-as-pure/src/**/*.ts",
|
||||||
|
"./packages/babel-helper-compilation-targets/src/**/*.ts",
|
||||||
"./packages/babel-helper-create-regexp-features-plugin/src/**/*.ts",
|
"./packages/babel-helper-create-regexp-features-plugin/src/**/*.ts",
|
||||||
"./packages/babel-helper-explode-assignable-expression/src/**/*.ts",
|
"./packages/babel-helper-explode-assignable-expression/src/**/*.ts",
|
||||||
"./packages/babel-helper-fixtures/src/**/*.ts",
|
"./packages/babel-helper-fixtures/src/**/*.ts",
|
||||||
@ -49,6 +50,9 @@
|
|||||||
"@babel/helper-annotate-as-pure": [
|
"@babel/helper-annotate-as-pure": [
|
||||||
"./packages/babel-helper-annotate-as-pure/src"
|
"./packages/babel-helper-annotate-as-pure/src"
|
||||||
],
|
],
|
||||||
|
"@babel/helper-compilation-targets": [
|
||||||
|
"./packages/babel-helper-compilation-targets/src"
|
||||||
|
],
|
||||||
"@babel/helper-create-regexp-features-plugin": [
|
"@babel/helper-create-regexp-features-plugin": [
|
||||||
"./packages/babel-helper-create-regexp-features-plugin/src"
|
"./packages/babel-helper-create-regexp-features-plugin/src"
|
||||||
],
|
],
|
||||||
|
|||||||
@ -410,7 +410,9 @@ __metadata:
|
|||||||
dependencies:
|
dependencies:
|
||||||
"@babel/compat-data": "workspace:^7.13.15"
|
"@babel/compat-data": "workspace:^7.13.15"
|
||||||
"@babel/core": "workspace:*"
|
"@babel/core": "workspace:*"
|
||||||
|
"@babel/helper-plugin-test-runner": "workspace:*"
|
||||||
"@babel/helper-validator-option": "workspace:^7.12.17"
|
"@babel/helper-validator-option": "workspace:^7.12.17"
|
||||||
|
"@types/semver": ^5.5.0
|
||||||
browserslist: ^4.14.5
|
browserslist: ^4.14.5
|
||||||
semver: "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
|
semver: "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
@ -4243,7 +4245,7 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"@types/semver@npm:^5.4.0":
|
"@types/semver@npm:^5.4.0, @types/semver@npm:^5.5.0":
|
||||||
version: 5.5.0
|
version: 5.5.0
|
||||||
resolution: "@types/semver@npm:5.5.0"
|
resolution: "@types/semver@npm:5.5.0"
|
||||||
checksum: df74589466e171c36dd868b760609e518830f212134c238674ddd6eb83653368c59f4510aa6523b7692ec99c5d8ab40b818e30f9d65e0df97c56bdbacef06661
|
checksum: df74589466e171c36dd868b760609e518830f212134c238674ddd6eb83653368c59f4510aa6523b7692ec99c5d8ab40b818e30f9d65e0df97c56bdbacef06661
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user