forked from babel/babel
/
tsconfig.js
140 lines (133 loc) 路 4.78 KB
/
tsconfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import fs from "fs";
function importJSON(path) {
return JSON.parse(fs.readFileSync(path));
}
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) {
return fs
.readdirSync(new URL(subRoot, root))
.filter(name => name.startsWith("babel-"))
.map(name => {
const relative = `./${subRoot}/${name}`;
const packageJSON = importJSON(new URL(relative + "/package.json", root));
// Babel 8 exports > Babel 7 exports > {}
const exports =
packageJSON.conditions?.BABEL_8_BREAKING?.[0]?.exports ??
packageJSON.exports ??
{};
const subExports = Object.entries(exports).flatMap(
([_export, exportPath]) => {
// The @babel/standalone has babel.js as exports, but we don't have src/babel.ts
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];
}
if (typeof exportPath === "object") {
exportPath = exportPath.default;
}
if (exportPath.startsWith("./lib") && exportPath.endsWith(".js")) {
// remove the leading `.` and trailing `.js`
const subExport = _export.slice(1).replace(/\.js$/, "");
const subExportPath = exportPath
.replace("./lib", "/src")
.replace(/\.js$/, ".ts")
.replace(/\/index\.ts$/, "");
return [[subExport, subExportPath]];
}
return [];
}
);
return {
name: name.replace(/^babel-/, "@babel/"),
relative,
subExports,
};
})
.filter(({ name, relative }) => {
const ret =
// They are special-cased because them dose not have a index.ts
name === "@babel/register" ||
name === "@babel/cli" ||
name === "@babel/node" ||
// @babel/compat-data is used by preset-env
name === "@babel/compat-data" ||
fs.existsSync(new URL(relative + "/src/index.ts", root));
if (!ret) {
console.log(`Skipping ${name} for tsconfig.json`);
}
return ret;
});
}
const tsPkgs = [
...getTsPkgs("packages"),
...getTsPkgs("eslint"),
...getTsPkgs("codemods"),
];
fs.writeFileSync(
new URL("tsconfig.json", root),
"/* This file is automatically generated by scripts/generators/tsconfig.js */\n" +
JSON.stringify(
{
extends: "./tsconfig.base.json",
include: tsPkgs.map(({ relative }) => `${relative}/src/**/*.ts`),
compilerOptions: {
paths: Object.fromEntries([
...tsPkgs.flatMap(({ name, relative, subExports }) => {
return subExports.map(([subExport, subExportPath]) => {
return [name + subExport, [relative + subExportPath]];
});
}),
...archivedSyntaxPkgs.map(name => [
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"],
],
["globals", ["./node_modules/globals-BABEL_8_BREAKING-true"]],
["js-tokens", ["./node_modules/js-tokens-BABEL_8_BREAKING-true"]],
["regexpu-core", ["./lib/regexpu-core.d.ts"]],
[
"to-fast-properties",
["./node_modules/to-fast-properties-BABEL_8_BREAKING-true"],
],
["slash", ["./node_modules/slash-BABEL_8_BREAKING-true"]],
["kexec", ["./lib/kexec.d.ts"]],
]),
},
},
null,
2
)
);