Repository navigation
Expand file tree
/
Copy pathbabel.config.ts
More file actions
643 lines (593 loc) · 19.1 KB
/
Copy pathbabel.config.ts
File metadata and controls
643 lines (593 loc) · 19.1 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
import pathUtils from "node:path";
import fs from "node:fs";
import type { PluginItem } from "@babel/core";
import {
parseSync,
traverse,
type InputOptions,
type types as t,
type NodePath,
type PluginObject,
type PluginAPI,
type ConfigAPI,
} from "@babel/core";
import packageJson from "./package.json" with { type: "json" };
import babelCorePackageJson from "./packages/babel-core/package.json" with { type: "json" };
import pluginToggleBooleanFlag from "./scripts/babel-plugin-toggle-boolean-flag/plugin.ts";
import pluginBitDecorator from "./scripts/babel-plugin-bit-decorator/plugin.ts";
import pluginTransformNodeProtocolImport from "./scripts/babel-plugin-transform-node-protocol-import/plugin.ts";
import { commonJS } from "$repo-utils";
let jestSnapshot = false;
if (typeof it === "function") {
// Jest loads the Babel config to parse file and update inline snapshots.
// This is ok, as it's not loading the Babel config to test Babel itself.
if (!new Error().stack!.includes("jest-snapshot")) {
throw new Error("Monorepo root's babel.config.js loaded by a test.");
}
jestSnapshot = true;
}
const { __dirname } = commonJS(import.meta.url);
function normalize(src: string) {
return src.replace(/\//, pathUtils.sep);
}
export default function (api: ConfigAPI) {
const env = api.env();
const sources = ["packages/*/src", "codemods/*/src", "eslint/*/src"];
const envOpts = {
shippedProposals: true,
modules: false,
exclude: [
"transform-typeof-symbol",
// We need to enable useBuiltIns
"transform-object-rest-spread",
],
debug: false,
};
const presetTsOpts = {
onlyRemoveTypeImports: true,
optimizeConstEnums: true,
};
// These are "safe" assumptions, that we can enable globally
const assumptions = {
constantSuper: true,
ignoreFunctionLength: true,
ignoreToPrimitiveHint: true,
mutableTemplateObject: true,
noClassCalls: true,
noDocumentAll: true,
noNewArrows: true,
setClassMethods: true,
setComputedProperties: true,
setSpreadProperties: true,
skipForOfIteratorClosing: true,
superIsCallableConstructor: true,
};
// These are "less safe": we only enable them on our own code
// and not when compiling dependencies.
const sourceAssumptions = {
objectRestNoSymbols: true,
pureGetters: true,
setPublicClassFields: true,
};
const parserAssumptions = {
iterableIsArray: true,
};
let targets = {};
let replaceTSImportExtension = true;
let ignoreLib = true;
const nodeVersion = "22.18";
// The vast majority of our src files are modules, but we use
// unambiguous to keep things simple until we get around to renaming
// the modules to be more easily distinguished from CommonJS
const unambiguousSources = [
...sources,
"packages/*/test",
"codemods/*/test",
"eslint/*/test",
];
switch (env) {
// Configs used during bundling builds.
case "standalone":
replaceTSImportExtension = false;
ignoreLib = false;
// rollup-commonjs will converts node_modules to ESM
unambiguousSources.push(
"/**/node_modules",
"packages/babel-preset-env/data",
"packages/babel-compat-data",
"packages/babel-runtime/regenerator"
);
targets = { ie: 7 };
break;
case "rollup":
replaceTSImportExtension = false;
ignoreLib = false;
// rollup-commonjs will converts node_modules to ESM
unambiguousSources.push(
"/**/node_modules",
"packages/babel-preset-env/data",
"packages/babel-compat-data"
);
targets = { node: nodeVersion };
break;
case "test-legacy": // In test-legacy environment, we build babel on latest node but test on minimum supported legacy versions
// fall through
case "production":
// Config during builds before publish.
targets = { node: nodeVersion };
break;
case "test":
targets = { node: "current" };
break;
case "development":
envOpts.debug = true;
targets = { node: "current" };
break;
}
const config: InputOptions = {
targets,
assumptions,
babelrc: false,
browserslistConfigFile: false,
// Our dependencies are all standard CommonJS, along with all sorts of
// other random files in Babel's codebase, so we use script as the default,
// and then mark actual modules as modules farther down.
sourceType: "script",
comments: false,
ignore: (
[
// These may not be strictly necessary with the newly-limited scope of
// babelrc searching, but including them for now because we had them
// in our .babelignore before.
"packages/*/test/fixtures",
ignoreLib ? "packages/*/lib" : null,
"packages/babel-standalone/babel.js",
].filter(Boolean) as string[]
).map(normalize),
parserOpts: {
createImportExpressions: true,
},
presets: [
// presets are applied from right to left
["@babel/env", envOpts],
["@babel/preset-typescript", presetTsOpts],
],
plugins: [
env === "standalone" && [
"@babel/transform-object-rest-spread",
{ useBuiltIns: true },
],
pluginBitDecorator,
pluginTransformNodeProtocolImport,
].filter(Boolean) as PluginItem[],
overrides: [
{
test: ["packages/babel-parser"].map(normalize),
plugins: [
"babel-plugin-transform-charcodes",
pluginBabelParserTokenType,
],
assumptions: parserAssumptions,
},
{
test: [
"packages/babel-code-frame",
"packages/babel-generator",
"packages/babel-helper-create-class-features-plugin",
"packages/babel-helper-string-parser",
"packages/babel-helper-validator-identifier",
].map(normalize),
plugins: ["babel-plugin-transform-charcodes"],
},
{
test: ["packages/babel-generator/src"].map(normalize),
plugins: [pluginGeneratorOptimization],
},
{
test: sources.map(normalize),
assumptions: sourceAssumptions,
plugins: [
transformNamedBabelTypesImportToDestructuring,
replaceTSImportExtension ? pluginReplaceTSImportExtension : null,
[
pluginToggleBooleanFlag,
{ name: "IS_STANDALONE", value: env === "standalone" },
"flag-IS_STANDALONE",
],
[
pluginToggleBooleanFlag,
{
name: "process.env.IS_PUBLISH",
value: bool(process.env.IS_PUBLISH),
},
"flag-IS_PUBLISH",
],
[
pluginToggleBooleanFlag,
{
name: "process.env.BABEL_9_BREAKING",
value: process.env.STRIP_BABEL_VERSION_FLAG
? bool(process.env.BABEL_9_BREAKING)
: null,
},
"flag-BABEL_9_BREAKING",
],
pluginPackageJsonMacro,
[
pluginRequiredVersionMacro,
{
overwrite(requiredVersion: string) {
if (!process.env.IS_PUBLISH || env === "standalone") {
if (bool(process.env.BABEL_9_BREAKING)) {
// Match packages/babel-core/src/index.ts
return babelCorePackageJson.version.replace(
/0*$/,
"999999999"
);
}
return babelCorePackageJson.version;
}
if (packageJson.version.includes("-")) {
// for pre-releases
requiredVersion = `${requiredVersion} || ${packageJson.version}`;
}
return requiredVersion;
},
},
],
].filter(Boolean) as PluginItem[],
},
{
test: unambiguousSources.map(normalize),
sourceType: "unambiguous",
} satisfies InputOptions,
].filter(Boolean),
};
if (jestSnapshot) {
config.plugins = [];
config.presets = [];
config.overrides = [];
config.parserOpts = {
plugins: ["typescript"],
};
config.sourceType = "unambiguous";
}
return config;
}
// env vars from the cli are always strings, so !!ENV_VAR returns true for "false"
function bool(value: unknown) {
return Boolean(value) && value !== "false" && value !== "0";
}
// A minimum semver GTE implementation
// Limitation:
// - it only supports comparing major and minor version, assuming Node.js will never ship
// features in patch release so we will never need to compare a version with "1.2.3"
//
// @example
// semverGte("8.10", "8.9") // true
// semverGte("8.9", "8.9") // true
// semverGte("9.0", "8.9") // true
// semverGte("8.9", "8.10") // false
// TODO: figure out how to inject it to the `@babel/template` usage so we don't need to
// copy and paste it.
// `((v,w)=>(v=v.split("."),w=w.split("."),+v[0]>+w[0]||v[0]==w[0]&&+v[1]>=+w[1]))`;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function pluginPolyfillsOldNode() {
const polyfills: {
name: string;
necessary: (path: NodePath<t.MemberExpression>) => boolean;
supported: (path: NodePath<t.MemberExpression>) => boolean;
replacement: () => t.Expression;
}[] = [
// EXAMPLE:
// {
// name: "Object.entries",
// necessary({ parent, node }) {
// // To avoid infinite replacement loops
// return !t.isLogicalExpression(parent, { operator: "||", left: node });
// },
// supported: path =>
// path.parentPath.isCallExpression({ callee: path.node }),
// replacement: template`Object.entries || (o => Object.keys(o).map(k => [k, o[k]]))`,
// },
];
return {
visitor: {
MemberExpression(path) {
for (const polyfill of polyfills) {
if (!path.matchesPattern(polyfill.name)) continue;
if (!polyfill.necessary(path)) return;
if (!polyfill.supported(path)) {
throw path.buildCodeFrameError(
`This '${polyfill.name}' usage is not supported by the inline polyfill.\n` +
path.parentPath.toString()
);
}
path.replaceWith(polyfill.replacement());
break;
}
},
},
} satisfies PluginObject;
}
function pluginPackageJsonMacro({ types: t }: PluginAPI) {
const fnName = "PACKAGE_JSON";
return {
visitor: {
ReferencedIdentifier(path) {
if (path.isIdentifier({ name: fnName })) {
throw path.buildCodeFrameError(
`"${fnName}" is only supported in member expressions.`
);
}
},
MemberExpression(path) {
if (!path.get("object").isIdentifier({ name: fnName })) return;
if (path.node.computed) {
throw path.buildCodeFrameError(
`"${fnName}" does not support computed properties.`
);
}
const field = (path.node.property as t.Identifier).name;
// TODO: When dropping old Node.js versions, use require.resolve
// instead of looping through the folders hierarchy
let pkg;
for (let dir = pathUtils.dirname(this.filename!); ;) {
try {
pkg = fs.readFileSync(pathUtils.join(dir, "package.json"), "utf8");
break;
} catch (_) {}
const prev = dir;
dir = pathUtils.resolve(dir, "..");
// We are in the root and didn't find a package.json file
if (dir === prev) return;
}
const value = JSON.parse(pkg)[field];
path.replaceWith(t.valueToNode(value));
},
},
} satisfies PluginObject;
}
function pluginRequiredVersionMacro(
{ types: t }: PluginAPI,
{
overwrite,
}: {
overwrite: (requiredVersion: string, filename: string) => string | null;
}
) {
const fnName = "REQUIRED_VERSION";
return {
visitor: {
ReferencedIdentifier(path) {
if (path.isIdentifier({ name: fnName })) {
throw path.buildCodeFrameError(
`"${fnName}" is only supported in call expressions.`
);
}
},
CallExpression(path) {
if (!path.get("callee").isIdentifier({ name: fnName })) return;
if (path.node.arguments.length !== 1) {
throw path.buildCodeFrameError(
`"${fnName}" expects exactly one argument.`
);
}
const arg = path.get("arguments.0").evaluate().value;
if (!arg) {
throw path.buildCodeFrameError(
`"${fnName}" expects a literal argument.`
);
}
const version = overwrite(arg, this.filename!);
if (version != null) {
path.replaceWith(t.stringLiteral(version));
return;
}
path.replaceWith(path.node.arguments[0]);
},
},
} satisfies PluginObject;
}
// transform `import { x } from "@babel/types"` to `import * as _t from "@babel/types"; const { x } = _t;
function transformNamedBabelTypesImportToDestructuring({
types: {
cloneNode,
importNamespaceSpecifier,
objectPattern,
objectProperty,
variableDeclarator,
variableDeclaration,
},
}: PluginAPI) {
return {
name: "transform-babel-types-named-imports",
visitor: {
ImportDeclaration(path) {
const { node } = path;
if (
node.importKind === "value" &&
node.source.value === "@babel/types" &&
node.specifiers[0].type === "ImportSpecifier"
) {
const hoistedDestructuringProperties = [];
for (const {
imported,
local,
} of node.specifiers as t.ImportSpecifier[]) {
hoistedDestructuringProperties.push(
objectProperty(
imported,
local,
false,
(imported as t.Identifier).name === local.name
)
);
}
const babelTypeNsImport = path.scope.generateUidIdentifier("t");
node.specifiers = [importNamespaceSpecifier(babelTypeNsImport)];
path.insertAfter([
variableDeclaration("const", [
variableDeclarator(
objectPattern(hoistedDestructuringProperties),
cloneNode(babelTypeNsImport)
),
]),
]);
}
},
},
} satisfies PluginObject;
}
function pluginReplaceTSImportExtension() {
return {
visitor: traverse.explode({
"ImportDeclaration|ExportDeclaration"({
node,
}: NodePath<t.ImportDeclaration | t.ExportDeclaration>) {
if (!("source" in node)) return;
const { source } = node;
if (source) {
source.value = source.value.replace(/(\.[mc]?)ts$/, "$1js");
}
},
TSImportEqualsDeclaration({ node }) {
const { moduleReference } = node;
if (moduleReference.type !== "TSExternalModuleReference") return;
const { expression } = moduleReference;
expression.value = expression.value.replace(/(\.[mc]?)ts$/, "$1js");
},
}),
} satisfies PluginObject;
}
const tokenTypesMapping = new Map();
const tokenTypeSourcePath = "./packages/babel-parser/src/tokenizer/types.ts";
function getTokenTypesMapping() {
if (tokenTypesMapping.size === 0) {
const tokenTypesAst = parseSync(
fs.readFileSync(tokenTypeSourcePath, {
encoding: "utf-8",
}),
{
configFile: false,
parserOpts: { attachComment: false, plugins: ["typescript"] },
}
)!;
let typesDeclaration;
for (const n of tokenTypesAst.program.body) {
if (n.type === "ExportNamedDeclaration" && n.exportKind === "value") {
// @ts-expect-error checked by the if
const declarations = n.declaration!.declarations;
if (declarations !== undefined) typesDeclaration = declarations[0];
if (
typesDeclaration !== undefined &&
typesDeclaration.id.name === "types"
) {
break;
}
}
}
if (typesDeclaration === undefined) {
throw new Error(
"The plugin can not find TokenType definition in " + tokenTypeSourcePath
);
}
const tokenTypesDefinition = typesDeclaration.init.expression.properties;
for (let i = 0; i < tokenTypesDefinition.length; i++) {
tokenTypesMapping.set(tokenTypesDefinition[i].key.name, i);
}
}
return tokenTypesMapping;
}
function pluginBabelParserTokenType({
types: { isIdentifier, numericLiteral },
}: PluginAPI) {
const tokenTypesMapping = getTokenTypesMapping();
return {
visitor: {
MemberExpression(path) {
const { node } = path;
if (
isIdentifier(node.object, { name: "tt" }) &&
isIdentifier(node.property) &&
!node.computed
) {
const tokenName = node.property.name;
const tokenType = tokenTypesMapping.get(node.property.name);
if (tokenType === undefined) {
throw path.buildCodeFrameError(
`${tokenName} is not defined in ${tokenTypeSourcePath}`
);
}
path.replaceWith(numericLiteral(tokenType));
}
},
},
} as PluginObject;
}
function pluginGeneratorOptimization({ types: t }: PluginAPI) {
const generatorNames: string[] = [];
fs.globSync(
pathUtils.join(__dirname, "./packages/babel-generator/src/generators/*.ts")
).forEach(file => {
if (file.endsWith("deprecated.ts")) return;
const content = fs.readFileSync(file, "utf8");
const ast = parseSync(content, {
configFile: false,
parserOpts: { plugins: ["typescript"] },
});
t.traverseFast(ast, node => {
let name;
if (t.isExportSpecifier(node)) {
name = t.isStringLiteral(node.exported)
? node.exported.value
: node.exported.name;
} else if (
t.isExportNamedDeclaration(node) &&
t.isFunctionDeclaration(node.declaration)
) {
name = node.declaration.id!.name;
}
if (name && !name.startsWith("_")) generatorNames.push(name);
});
});
generatorNames.sort();
return {
visitor: {
CallExpression: {
exit(path) {
const { callee, arguments: args } = path.node;
if (t.isIdentifier(callee) && callee.name === "__node") {
if (!t.isStringLiteral(args[0])) {
throw new Error("Expected StringLiteral");
}
const type = args[0].value;
const generatorIndex = generatorNames.indexOf(type);
if (generatorIndex === -1) {
throw path.buildCodeFrameError(`Unknown generator type: ${type}`);
}
path.replaceWith(t.numericLiteral(generatorIndex));
return;
}
if (
t.isMemberExpression(callee) &&
t.isIdentifier(callee.property) &&
t.isThisExpression(callee.object)
) {
if (
callee.property.name === "token" &&
args.length === 1 &&
t.isStringLiteral(args[0])
) {
const str = args[0].value;
if (str.length === 1) {
callee.property.name = "tokenChar";
args[0] = t.numericLiteral(str.charCodeAt(0));
}
}
}
},
},
},
} satisfies PluginObject;
}