Skip to content

Commit a273a1d

Browse files
authored
Add third party lib typings (#14580)
* add @babel/compat-data path mappings * add typings for third-party Babel plugins * fix typing errors * add regenerator-transform and dynamic-import-node
1 parent 52a9089 commit a273a1d

7 files changed

Lines changed: 152 additions & 6 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { PluginAPI } from "@babel/core";
2+
import type * as t from "@babel/types";
3+
// https://github.com/airbnb/babel-plugin-dynamic-import-node/blob/master/src/utils.js
4+
declare module "babel-plugin-dynamic-import-node/utils" {
5+
function getImportSource(
6+
t: typeof import("@babel/types"),
7+
callNode: t.CallExpression
8+
): t.Expression;
9+
10+
function createDynamicImportTransform({
11+
template,
12+
types: t,
13+
}: PluginAPI): (context: PluginPass, path: NodePath) => void;
14+
15+
export { getImportSource, createDynamicImportTransform };
16+
}

lib/third-party-libs.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
1+
import type { PluginPass } from "@babel/core";
2+
import { declare } from "@babel/helper-plugin-utils";
3+
import type * as t from "@babel/types";
14
declare module "js-tokens" {
25
// TODO(Babel 8): Remove this
36
export { default } from "js-tokens-BABEL_8_BREAKING-true";
47
export * from "js-tokens-BABEL_8_BREAKING-true";
58
}
9+
10+
declare module "@babel/preset-modules/lib/plugins/transform-async-arrows-in-class" {
11+
let plugin: ReturnType<typeof declare>;
12+
export = { default: plugin };
13+
}
14+
declare module "@babel/preset-modules/lib/plugins/transform-edge-default-parameters" {
15+
let plugin: ReturnType<typeof declare>;
16+
export = { default: plugin };
17+
}
18+
declare module "@babel/preset-modules/lib/plugins/transform-edge-function-name" {
19+
let plugin: ReturnType<typeof declare>;
20+
export = { default: plugin };
21+
}
22+
declare module "@babel/preset-modules/lib/plugins/transform-tagged-template-caching" {
23+
let plugin: ReturnType<typeof declare>;
24+
export = { default: plugin };
25+
}
26+
declare module "@babel/preset-modules/lib/plugins/transform-safari-block-shadowing" {
27+
let plugin: ReturnType<typeof declare>;
28+
export = { default: plugin };
29+
}
30+
declare module "@babel/preset-modules/lib/plugins/transform-safari-for-shadowing" {
31+
let plugin: ReturnType<typeof declare>;
32+
export = { default: plugin };
33+
}
34+
declare module "babel-plugin-polyfill-corejs2" {
35+
let plugin: ReturnType<typeof declare>;
36+
export = { default: plugin };
37+
}
38+
declare module "babel-plugin-polyfill-corejs3" {
39+
let plugin: ReturnType<typeof declare>;
40+
export = { default: plugin };
41+
}
42+
declare module "babel-plugin-polyfill-regenerator" {
43+
let plugin: ReturnType<typeof declare>;
44+
export = { default: plugin };
45+
}
46+
47+
declare module "regenerator-transform" {
48+
let plugin: ReturnType<typeof declare>;
49+
export = { default: plugin };
50+
}

packages/babel-helper-compilation-targets/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type Target =
1313
| "samsung";
1414

1515
export type Targets = {
16-
[target in Target]: string;
16+
[target in Target]?: string;
1717
};
1818

1919
export type TargetsTuple = {

packages/babel-plugin-transform-runtime/src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import getRuntimePath, { resolveFSPath } from "./get-runtime-path";
88
import _pluginCorejs2 from "babel-plugin-polyfill-corejs2";
99
import _pluginCorejs3 from "babel-plugin-polyfill-corejs3";
1010
import _pluginRegenerator from "babel-plugin-polyfill-regenerator";
11-
const pluginCorejs2 = _pluginCorejs2.default || _pluginCorejs2;
12-
const pluginCorejs3 = _pluginCorejs3.default || _pluginCorejs3;
13-
const pluginRegenerator = _pluginRegenerator.default || _pluginRegenerator;
11+
const pluginCorejs2 = (_pluginCorejs2.default ||
12+
_pluginCorejs2) as typeof _pluginCorejs2.default;
13+
const pluginCorejs3 = (_pluginCorejs3.default ||
14+
_pluginCorejs3) as typeof _pluginCorejs3.default;
15+
const pluginRegenerator = (_pluginRegenerator.default ||
16+
_pluginRegenerator) as typeof _pluginRegenerator.default;
1417

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

packages/babel-preset-env/src/normalize-options.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ export const checkDuplicateIncludeExcludes = (
116116
const normalizeTargets = (targets): Options["targets"] => {
117117
// TODO: Allow to use only query or strings as a targets from next breaking change.
118118
if (typeof targets === "string" || Array.isArray(targets)) {
119-
// @ts-expect-error
120119
return { browsers: targets };
121120
}
122121
return { ...targets };

scripts/generators/tsconfig.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ const archivedSyntaxPkgs = importJSON(
88
new URL("./archived-syntax-pkgs.json", import.meta.url)
99
);
1010

11+
const thirdPartyBabelPlugins = [
12+
"@babel/preset-modules/lib/plugins/transform-async-arrows-in-class",
13+
"@babel/preset-modules/lib/plugins/transform-edge-default-parameters",
14+
"@babel/preset-modules/lib/plugins/transform-edge-function-name",
15+
"@babel/preset-modules/lib/plugins/transform-tagged-template-caching",
16+
"@babel/preset-modules/lib/plugins/transform-safari-block-shadowing",
17+
"@babel/preset-modules/lib/plugins/transform-safari-for-shadowing",
18+
"babel-plugin-polyfill-corejs2",
19+
"babel-plugin-polyfill-corejs3",
20+
"babel-plugin-polyfill-regenerator",
21+
"regenerator-transform",
22+
];
23+
1124
const root = new URL("../../", import.meta.url);
1225

1326
function getTsPkgs(subRoot) {
@@ -28,6 +41,14 @@ function getTsPkgs(subRoot) {
2841
if (name === "babel-standalone") {
2942
return [["", "/src"]];
3043
}
44+
if (name === "babel-compat-data") {
45+
// map ./plugins to ./data/plugins.json
46+
const subExport = _export.slice(1);
47+
const subExportPath = exportPath
48+
.replace("./", "/data/")
49+
.replace(/\.js$/, ".json");
50+
return [[subExport, subExportPath]];
51+
}
3152
// [{esm, default}, "./lib/index.js"]
3253
if (Array.isArray(exportPath)) {
3354
exportPath = exportPath[1];
@@ -55,8 +76,10 @@ function getTsPkgs(subRoot) {
5576
})
5677
.filter(
5778
({ name, relative }) =>
58-
// babel-register is special-cased because its entry point is a js file
79+
// @babel/register is special-cased because its entry point is a js file
5980
name === "@babel/register" ||
81+
// @babel/compat-data is used by preset-env
82+
name === "@babel/compat-data" ||
6083
fs.existsSync(new URL(relative + "/src/index.ts", root))
6184
);
6285
}
@@ -85,6 +108,14 @@ fs.writeFileSync(
85108
name,
86109
["./lib/archived-libs.d.ts"],
87110
]),
111+
...thirdPartyBabelPlugins.map(name => [
112+
name,
113+
["./lib/third-party-libs.d.ts"],
114+
]),
115+
[
116+
"babel-plugin-dynamic-import-node/utils",
117+
["./lib/babel-plugin-dynamic-import-node.d.ts"],
118+
],
88119
]),
89120
},
90121
},

tsconfig.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"extends": "./tsconfig.base.json",
44
"include": [
55
"./packages/babel-code-frame/src/**/*.ts",
6+
"./packages/babel-compat-data/src/**/*.ts",
67
"./packages/babel-core/src/**/*.ts",
78
"./packages/babel-generator/src/**/*.ts",
89
"./packages/babel-helper-annotate-as-pure/src/**/*.ts",
@@ -152,6 +153,24 @@
152153
"@babel/code-frame": [
153154
"./packages/babel-code-frame/src"
154155
],
156+
"@babel/compat-data/plugins": [
157+
"./packages/babel-compat-data/data/plugins.json"
158+
],
159+
"@babel/compat-data/native-modules": [
160+
"./packages/babel-compat-data/data/native-modules.json"
161+
],
162+
"@babel/compat-data/corejs2-built-ins": [
163+
"./packages/babel-compat-data/data/corejs2-built-ins.json"
164+
],
165+
"@babel/compat-data/corejs3-shipped-proposals": [
166+
"./packages/babel-compat-data/data/corejs3-shipped-proposals.json"
167+
],
168+
"@babel/compat-data/overlapping-plugins": [
169+
"./packages/babel-compat-data/data/overlapping-plugins.json"
170+
],
171+
"@babel/compat-data/plugin-bugfixes": [
172+
"./packages/babel-compat-data/data/plugin-bugfixes.json"
173+
],
155174
"@babel/core": [
156175
"./packages/babel-core/src"
157176
],
@@ -646,6 +665,39 @@
646665
],
647666
"@babel/plugin-syntax-trailing-function-commas": [
648667
"./lib/archived-libs.d.ts"
668+
],
669+
"@babel/preset-modules/lib/plugins/transform-async-arrows-in-class": [
670+
"./lib/third-party-libs.d.ts"
671+
],
672+
"@babel/preset-modules/lib/plugins/transform-edge-default-parameters": [
673+
"./lib/third-party-libs.d.ts"
674+
],
675+
"@babel/preset-modules/lib/plugins/transform-edge-function-name": [
676+
"./lib/third-party-libs.d.ts"
677+
],
678+
"@babel/preset-modules/lib/plugins/transform-tagged-template-caching": [
679+
"./lib/third-party-libs.d.ts"
680+
],
681+
"@babel/preset-modules/lib/plugins/transform-safari-block-shadowing": [
682+
"./lib/third-party-libs.d.ts"
683+
],
684+
"@babel/preset-modules/lib/plugins/transform-safari-for-shadowing": [
685+
"./lib/third-party-libs.d.ts"
686+
],
687+
"babel-plugin-polyfill-corejs2": [
688+
"./lib/third-party-libs.d.ts"
689+
],
690+
"babel-plugin-polyfill-corejs3": [
691+
"./lib/third-party-libs.d.ts"
692+
],
693+
"babel-plugin-polyfill-regenerator": [
694+
"./lib/third-party-libs.d.ts"
695+
],
696+
"regenerator-transform": [
697+
"./lib/third-party-libs.d.ts"
698+
],
699+
"babel-plugin-dynamic-import-node/utils": [
700+
"./lib/babel-plugin-dynamic-import-node.d.ts"
649701
]
650702
}
651703
}

0 commit comments

Comments
 (0)