Skip to content

Commit

Permalink
Add third party lib typings (#14580)
Browse files Browse the repository at this point in the history
* add @babel/compat-data path mappings

* add typings for third-party Babel plugins

* fix typing errors

* add regenerator-transform and dynamic-import-node
  • Loading branch information
JLHwung committed May 25, 2022
1 parent 52a9089 commit a273a1d
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 6 deletions.
16 changes: 16 additions & 0 deletions lib/babel-plugin-dynamic-import-node.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { PluginAPI } from "@babel/core";
import type * as t from "@babel/types";
// https://github.com/airbnb/babel-plugin-dynamic-import-node/blob/master/src/utils.js
declare module "babel-plugin-dynamic-import-node/utils" {
function getImportSource(
t: typeof import("@babel/types"),
callNode: t.CallExpression
): t.Expression;

function createDynamicImportTransform({
template,
types: t,
}: PluginAPI): (context: PluginPass, path: NodePath) => void;

export { getImportSource, createDynamicImportTransform };
}
45 changes: 45 additions & 0 deletions lib/third-party-libs.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
import type { PluginPass } from "@babel/core";
import { declare } from "@babel/helper-plugin-utils";
import type * as t from "@babel/types";
declare module "js-tokens" {
// TODO(Babel 8): Remove this
export { default } from "js-tokens-BABEL_8_BREAKING-true";
export * from "js-tokens-BABEL_8_BREAKING-true";
}

declare module "@babel/preset-modules/lib/plugins/transform-async-arrows-in-class" {
let plugin: ReturnType<typeof declare>;
export = { default: plugin };
}
declare module "@babel/preset-modules/lib/plugins/transform-edge-default-parameters" {
let plugin: ReturnType<typeof declare>;
export = { default: plugin };
}
declare module "@babel/preset-modules/lib/plugins/transform-edge-function-name" {
let plugin: ReturnType<typeof declare>;
export = { default: plugin };
}
declare module "@babel/preset-modules/lib/plugins/transform-tagged-template-caching" {
let plugin: ReturnType<typeof declare>;
export = { default: plugin };
}
declare module "@babel/preset-modules/lib/plugins/transform-safari-block-shadowing" {
let plugin: ReturnType<typeof declare>;
export = { default: plugin };
}
declare module "@babel/preset-modules/lib/plugins/transform-safari-for-shadowing" {
let plugin: ReturnType<typeof declare>;
export = { default: plugin };
}
declare module "babel-plugin-polyfill-corejs2" {
let plugin: ReturnType<typeof declare>;
export = { default: plugin };
}
declare module "babel-plugin-polyfill-corejs3" {
let plugin: ReturnType<typeof declare>;
export = { default: plugin };
}
declare module "babel-plugin-polyfill-regenerator" {
let plugin: ReturnType<typeof declare>;
export = { default: plugin };
}

declare module "regenerator-transform" {
let plugin: ReturnType<typeof declare>;
export = { default: plugin };
}
2 changes: 1 addition & 1 deletion packages/babel-helper-compilation-targets/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type Target =
| "samsung";

export type Targets = {
[target in Target]: string;
[target in Target]?: string;
};

export type TargetsTuple = {
Expand Down
9 changes: 6 additions & 3 deletions packages/babel-plugin-transform-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import getRuntimePath, { resolveFSPath } from "./get-runtime-path";
import _pluginCorejs2 from "babel-plugin-polyfill-corejs2";
import _pluginCorejs3 from "babel-plugin-polyfill-corejs3";
import _pluginRegenerator from "babel-plugin-polyfill-regenerator";
const pluginCorejs2 = _pluginCorejs2.default || _pluginCorejs2;
const pluginCorejs3 = _pluginCorejs3.default || _pluginCorejs3;
const pluginRegenerator = _pluginRegenerator.default || _pluginRegenerator;
const pluginCorejs2 = (_pluginCorejs2.default ||
_pluginCorejs2) as typeof _pluginCorejs2.default;
const pluginCorejs3 = (_pluginCorejs3.default ||
_pluginCorejs3) as typeof _pluginCorejs3.default;
const pluginRegenerator = (_pluginRegenerator.default ||
_pluginRegenerator) as typeof _pluginRegenerator.default;

const pluginsCompat = "#__secret_key__@babel/runtime__compatibility";

Expand Down
1 change: 0 additions & 1 deletion packages/babel-preset-env/src/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export const checkDuplicateIncludeExcludes = (
const normalizeTargets = (targets): Options["targets"] => {
// TODO: Allow to use only query or strings as a targets from next breaking change.
if (typeof targets === "string" || Array.isArray(targets)) {
// @ts-expect-error
return { browsers: targets };
}
return { ...targets };
Expand Down
33 changes: 32 additions & 1 deletion scripts/generators/tsconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ const archivedSyntaxPkgs = importJSON(
new URL("./archived-syntax-pkgs.json", import.meta.url)
);

const thirdPartyBabelPlugins = [
"@babel/preset-modules/lib/plugins/transform-async-arrows-in-class",
"@babel/preset-modules/lib/plugins/transform-edge-default-parameters",
"@babel/preset-modules/lib/plugins/transform-edge-function-name",
"@babel/preset-modules/lib/plugins/transform-tagged-template-caching",
"@babel/preset-modules/lib/plugins/transform-safari-block-shadowing",
"@babel/preset-modules/lib/plugins/transform-safari-for-shadowing",
"babel-plugin-polyfill-corejs2",
"babel-plugin-polyfill-corejs3",
"babel-plugin-polyfill-regenerator",
"regenerator-transform",
];

const root = new URL("../../", import.meta.url);

function getTsPkgs(subRoot) {
Expand All @@ -28,6 +41,14 @@ function getTsPkgs(subRoot) {
if (name === "babel-standalone") {
return [["", "/src"]];
}
if (name === "babel-compat-data") {
// map ./plugins to ./data/plugins.json
const subExport = _export.slice(1);
const subExportPath = exportPath
.replace("./", "/data/")
.replace(/\.js$/, ".json");
return [[subExport, subExportPath]];
}
// [{esm, default}, "./lib/index.js"]
if (Array.isArray(exportPath)) {
exportPath = exportPath[1];
Expand Down Expand Up @@ -55,8 +76,10 @@ function getTsPkgs(subRoot) {
})
.filter(
({ name, relative }) =>
// babel-register is special-cased because its entry point is a js file
// @babel/register is special-cased because its entry point is a js file
name === "@babel/register" ||
// @babel/compat-data is used by preset-env
name === "@babel/compat-data" ||
fs.existsSync(new URL(relative + "/src/index.ts", root))
);
}
Expand Down Expand Up @@ -85,6 +108,14 @@ fs.writeFileSync(
name,
["./lib/archived-libs.d.ts"],
]),
...thirdPartyBabelPlugins.map(name => [
name,
["./lib/third-party-libs.d.ts"],
]),
[
"babel-plugin-dynamic-import-node/utils",
["./lib/babel-plugin-dynamic-import-node.d.ts"],
],
]),
},
},
Expand Down
52 changes: 52 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"extends": "./tsconfig.base.json",
"include": [
"./packages/babel-code-frame/src/**/*.ts",
"./packages/babel-compat-data/src/**/*.ts",
"./packages/babel-core/src/**/*.ts",
"./packages/babel-generator/src/**/*.ts",
"./packages/babel-helper-annotate-as-pure/src/**/*.ts",
Expand Down Expand Up @@ -152,6 +153,24 @@
"@babel/code-frame": [
"./packages/babel-code-frame/src"
],
"@babel/compat-data/plugins": [
"./packages/babel-compat-data/data/plugins.json"
],
"@babel/compat-data/native-modules": [
"./packages/babel-compat-data/data/native-modules.json"
],
"@babel/compat-data/corejs2-built-ins": [
"./packages/babel-compat-data/data/corejs2-built-ins.json"
],
"@babel/compat-data/corejs3-shipped-proposals": [
"./packages/babel-compat-data/data/corejs3-shipped-proposals.json"
],
"@babel/compat-data/overlapping-plugins": [
"./packages/babel-compat-data/data/overlapping-plugins.json"
],
"@babel/compat-data/plugin-bugfixes": [
"./packages/babel-compat-data/data/plugin-bugfixes.json"
],
"@babel/core": [
"./packages/babel-core/src"
],
Expand Down Expand Up @@ -646,6 +665,39 @@
],
"@babel/plugin-syntax-trailing-function-commas": [
"./lib/archived-libs.d.ts"
],
"@babel/preset-modules/lib/plugins/transform-async-arrows-in-class": [
"./lib/third-party-libs.d.ts"
],
"@babel/preset-modules/lib/plugins/transform-edge-default-parameters": [
"./lib/third-party-libs.d.ts"
],
"@babel/preset-modules/lib/plugins/transform-edge-function-name": [
"./lib/third-party-libs.d.ts"
],
"@babel/preset-modules/lib/plugins/transform-tagged-template-caching": [
"./lib/third-party-libs.d.ts"
],
"@babel/preset-modules/lib/plugins/transform-safari-block-shadowing": [
"./lib/third-party-libs.d.ts"
],
"@babel/preset-modules/lib/plugins/transform-safari-for-shadowing": [
"./lib/third-party-libs.d.ts"
],
"babel-plugin-polyfill-corejs2": [
"./lib/third-party-libs.d.ts"
],
"babel-plugin-polyfill-corejs3": [
"./lib/third-party-libs.d.ts"
],
"babel-plugin-polyfill-regenerator": [
"./lib/third-party-libs.d.ts"
],
"regenerator-transform": [
"./lib/third-party-libs.d.ts"
],
"babel-plugin-dynamic-import-node/utils": [
"./lib/babel-plugin-dynamic-import-node.d.ts"
]
}
}
Expand Down

0 comments on commit a273a1d

Please sign in to comment.