-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathbabel.config.js
More file actions
510 lines (437 loc) · 15.2 KB
/
babel.config.js
File metadata and controls
510 lines (437 loc) · 15.2 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
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const { SourceMapConsumer } = require('source-map');
const { globSync } = require('glob');
// Project root directory (where babel.config.js lives)
const PROJECT_ROOT = __dirname;
const fileFilter = process.env.SESSION_RC_FILE_FILTER;
const allowErrors = process.env.SESSION_RC_ALLOW_ERRORS;
// File patterns for babel
const babelInclude = 'dist/ts/**/*.js';
const babelIgnore = ['dist/ts/test/**', 'dist/ts/svgs/**'];
// NOTE: [react-compiler] we are telling the compiler to not attempt
// to compile these files in the babel config as they are highly
// complex and have a lot of very fine tuned callbacks and useEffect
// logic, so it's probably not worth trying to refactor at this stage.
const filesIgnoredByReactCompiler = new Set(
[
'dist/ts/components/conversation/composition/CompositionTextArea.js',
'dist/ts/components/conversation/SessionStagedLinkPreview.js',
'dist/ts/components/conversation/message/message-content/MessageReactBar.js',
].map(f => path.join(PROJECT_ROOT, f))
);
// ANSI color codes
const c = code => (process.env.NO_COLOR ? '' : `\x1b[${code}m`);
const colors = {
reset: c(0),
bright: c(1),
dim: c(2),
black: c(30),
red: c(31),
green: c(32),
yellow: c(33),
blue: c(34),
magenta: c(35),
cyan: c(36),
white: c(37),
bgRed: c(41),
bgYellow: c(43),
};
// Cache for source map consumers
const sourceMapCache = new Map();
// Number of lines to show before/after error
const CONTEXT_LINES = 2;
// Collect all errors grouped by file
const errorsByFile = new Map();
const skippedFiles = new Set();
const filenameMap = new Map();
const pendingPromises = [];
// File size tracking - measure all js files at startup
function getTotalSize() {
const files = globSync('dist/ts/**/*.js', {
cwd: PROJECT_ROOT,
ignore: babelIgnore,
});
let total = 0;
for (const file of files) {
try {
total += fs.statSync(path.join(PROJECT_ROOT, file)).size;
} catch {
// Skip files that don't exist
}
}
return { total, count: files.length };
}
const initialStats = getTotalSize();
const startTime = Date.now();
// Progress tracking
let filesProcessed = 0;
const totalFiles = initialStats.count;
function updateProgress() {
filesProcessed++;
process.stdout.write(
`\r${colors.dim}Compiling... ${filesProcessed}/${totalFiles}${colors.reset}`
);
}
function clearProgress() {
process.stdout.write('\r\x1b[K');
}
function formatBytes(bytes) {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
}
function printSizeSummary() {
clearProgress();
const finalStats = getTotalSize();
const inputSize = initialStats.total;
const outputSize = finalStats.total;
if (inputSize === 0) return;
const diff = outputSize - inputSize;
const percent = ((diff / inputSize) * 100).toFixed(1);
const sign = diff >= 0 ? '+' : '';
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
console.log(
`${colors.green}Compiled ${formatBytes(inputSize)} → ${formatBytes(outputSize)} (${sign}${percent}%) [${finalStats.count} files in ${elapsed}s]${colors.reset}`
);
}
function resolveFilename(filename) {
const resultName = filenameMap.get(filename);
if (resultName) {
return resultName;
}
const relativeFilename = path.relative(PROJECT_ROOT, filename);
filenameMap.set(filename, relativeFilename);
return relativeFilename;
}
async function getOriginalLocation(jsFile, line, column) {
try {
const mapFile = `${jsFile}.map`;
if (!fs.existsSync(mapFile)) {
return null;
}
let consumer = sourceMapCache.get(mapFile);
if (!consumer) {
const rawSourceMap = JSON.parse(fs.readFileSync(mapFile, 'utf-8'));
consumer = await new SourceMapConsumer(rawSourceMap);
sourceMapCache.set(mapFile, consumer);
}
const original = consumer.originalPositionFor({ line, column });
return original.source ? original : null;
} catch (err) {
return null;
}
}
function getSourceContent(jsFile) {
try {
const mapFile = `${jsFile}.map`;
if (!fs.existsSync(mapFile)) {
return null;
}
const rawSourceMap = JSON.parse(fs.readFileSync(mapFile, 'utf-8'));
if (rawSourceMap.sourcesContent && rawSourceMap.sourcesContent[0]) {
return {
content: rawSourceMap.sourcesContent[0],
sourceName: rawSourceMap.sources[0],
};
}
return null;
} catch (err) {
return null;
}
}
async function processErrorInternal(filename, event) {
const errorLoc = event.detail?.options?.loc;
const fnLoc = event.fnLoc;
const reason = event.detail?.options?.reason || 'Unknown error';
const category = event.detail?.options?.category || 'Unknown';
const errorData = {
reason,
category,
lines: [],
};
if (!fnLoc || !errorLoc) {
errorData.lines.push(` ${colors.yellow}Reason:${colors.reset} [${category}] ${reason}`);
return errorData;
}
// Try to get original TS source from source map
const sourceData = getSourceContent(filename);
let source;
let sourceName;
if (sourceData) {
source = sourceData.content;
sourceName = sourceData.sourceName;
} else {
source = fs.readFileSync(filename, 'utf-8');
sourceName = filename;
}
const lines = source.split('\n');
// Map JS locations to TS locations
let errStartLine = errorLoc.start.line;
let errEndLine = errorLoc.end.line;
let errColStart = errorLoc.start.column;
let errColEnd = errorLoc.end.column;
if (sourceData) {
const errStartOrig = await getOriginalLocation(
filename,
errorLoc.start.line,
errorLoc.start.column
);
const errEndOrig = await getOriginalLocation(filename, errorLoc.end.line, errorLoc.end.column);
if (errStartOrig) {
errStartLine = errStartOrig.line;
errColStart = errStartOrig.column;
}
if (errEndOrig) {
errEndLine = errEndOrig.line;
errColEnd = errEndOrig.column;
}
}
// Validate line numbers are within bounds
const maxLine = lines.length;
errStartLine = Math.max(1, Math.min(errStartLine, maxLine));
errEndLine = Math.max(1, Math.min(errEndLine, maxLine));
// Show context around the error
const contextStart = Math.max(0, errStartLine - CONTEXT_LINES - 1);
const contextEnd = Math.min(lines.length, errEndLine + CONTEXT_LINES);
errorData.sourceName = sourceName;
errorData.errStartLine = errStartLine;
errorData.errColStart = errColStart;
errorData.errColEnd = errColEnd;
errorData.lines.push(` ${colors.yellow}Reason:${colors.reset} [${category}] ${reason}`);
errorData.lines.push(
` ${colors.yellow}Source:${colors.reset} ${colors.dim}${sourceName}${colors.reset} line ${errStartLine}, columns ${errColStart}-${errColEnd}`
);
// Only show source context if we have valid lines to show
if (contextEnd > contextStart && lines.length > 0) {
errorData.lines.push(` ${colors.dim}${'─'.repeat(60)}${colors.reset}`);
for (let i = contextStart; i < contextEnd; i++) {
const lineNum = i + 1;
const lineNumStr = lineNum.toString().padStart(4, ' ');
const line = lines[i] ?? '';
const isErrorLine = lineNum >= errStartLine && lineNum <= errEndLine;
if (isErrorLine) {
const gutter = `${colors.bgRed}${colors.black} ${lineNumStr} ${colors.reset}`;
if (errColStart !== null && errColEnd !== null && lineNum === errStartLine) {
// Clamp column values to line length
const safeColStart = Math.min(errColStart, line.length);
const safeColEnd = Math.min(errColEnd, line.length);
const before = line.substring(0, safeColStart);
const highlight = line.substring(safeColStart, safeColEnd);
const after = line.substring(safeColEnd);
if (highlight.length > 0) {
errorData.lines.push(
` ${gutter} ${before}${colors.bgYellow}${colors.black}${highlight}${colors.reset}${after}`
);
const underline =
' '.repeat(safeColStart) + '^'.repeat(Math.max(1, safeColEnd - safeColStart));
errorData.lines.push(
` ${colors.dim} ${colors.reset} ${colors.red}${underline}${colors.reset}`
);
} else {
// No highlight range, just show the line in red
errorData.lines.push(` ${gutter} ${colors.red}${line}${colors.reset}`);
}
} else {
errorData.lines.push(` ${gutter} ${colors.red}${line}${colors.reset}`);
}
} else {
const gutter = `${colors.dim} ${lineNumStr} ${colors.reset}`;
errorData.lines.push(` ${gutter} ${line}`);
}
}
errorData.lines.push(` ${colors.dim}${'─'.repeat(60)}${colors.reset}`);
} else {
// No source context available
errorData.lines.push(
` ${colors.dim}(source context not available - line ${errStartLine} may be out of range for file with ${lines.length} lines)${colors.reset}`
);
}
return errorData;
}
async function processError(filename, event) {
const errorData = await processErrorInternal(filename, event);
if (!errorsByFile.has(filename)) {
errorsByFile.set(filename, []);
}
errorsByFile.get(filename).push(errorData);
}
function printAllErrors() {
console.log(`\n${colors.red}${colors.bright}${'═'.repeat(70)}${colors.reset}`);
console.log(`${colors.red}${colors.bright} REACT COMPILER ERRORS SUMMARY${colors.reset}`);
console.log(`${colors.red}${colors.bright}${'═'.repeat(70)}${colors.reset}`);
let totalErrors = 0;
const errorsByFileArray = [];
errorsByFile.forEach((errors, filename) => {
const relativeFilename = resolveFilename(filename);
totalErrors += errors.length;
errorsByFileArray.push({ errors, filename: relativeFilename });
});
errorsByFileArray.sort((a, b) => b.errors.length - a.errors.length);
if (fileFilter) {
console.log(
`${colors.red} File filter specified, filtering output for: ${fileFilter} ${colors.reset}`
);
}
let hiddenFiles = 0;
errorsByFileArray.forEach(({ errors, filename }) => {
if (fileFilter && !filename.includes(fileFilter)) {
hiddenFiles++;
return;
}
console.log(
`\n${colors.cyan}${colors.bright}📁 ${filename}${colors.reset} ${colors.dim}(${errors.length} error${errors.length > 1 ? 's' : ''})${colors.reset}`
);
console.log(`${colors.dim}${'─'.repeat(70)}${colors.reset}`);
errors.forEach((error, index) => {
if (errors.length > 1) {
console.log(`\n ${colors.magenta}Error ${index + 1}:${colors.reset}`);
}
error.lines.forEach(line => console.log(line));
});
});
const logTotals = () => {
console.log(`\n${colors.red}${colors.bright}${'═'.repeat(70)}${colors.reset}`);
console.log(
`${colors.red}${colors.bright} Total: ${totalErrors} error${totalErrors > 1 ? 's' : ''} in ${errorsByFile.size} file${errorsByFile.size > 1 ? 's' : ''}${colors.reset}`
);
console.log(`${colors.red}${colors.bright}${'═'.repeat(70)}${colors.reset}\n`);
};
console.log(
`${colors.red} ${errorsByFile.size} files could not be compiled with the react compiler: ${colors.reset}`
);
errorsByFileArray.forEach(({ filename, errors }) =>
console.log(` - (${errors.length}) ${colors.red}${colors.bright}${filename}${colors.reset}`)
);
logTotals();
console.log(
`${colors.red} Babel compilation complete with react compiler errors. Note: react compiler errors mean the file was not compiled with the react compiler, so is left in the same state it was in before compilation. ${colors.reset}\n`
);
if (hiddenFiles > 0) {
console.log(
`${colors.red} ${hiddenFiles} files were hidden from the compiler output because of the filter: ${fileFilter} ${colors.reset}`
);
}
}
function printSkippedFiles() {
const multiple = skippedFiles.size > 1;
console.log(
`${colors.yellow}${skippedFiles.size} file${multiple ? 's were' : ' was'} skipped by the react compiler based on the config: ${colors.reset}`
);
skippedFiles.forEach(filename => {
const resolvedFileName = resolveFilename(filename);
console.log(`${colors.yellow} - ${resolvedFileName} ${colors.reset}`);
});
}
async function handleExit() {
await Promise.all(pendingPromises);
printSizeSummary();
if (errorsByFile.size > 0) {
printAllErrors();
} else {
console.log(
`${colors.green}${totalFiles - skippedFiles.size} files were successfully parsed by the React Compiler ${colors.reset}`
);
}
if (skippedFiles.size) {
printSkippedFiles();
}
if (errorsByFile.size > 0) {
if (allowErrors) {
console.log(
`${colors.red}SESSION_RC_ALLOW_ERRORS was enabled, the compiler will report no errors and the build will continue! ${colors.reset}`
);
} else {
process.exit(1);
}
}
}
let cleanupRegistered = false;
function registerCleanup() {
if (cleanupRegistered) {
return;
}
cleanupRegistered = true;
process.on('beforeExit', () => {
void handleExit();
});
}
// Always register cleanup to print size summary
registerCleanup();
const packageJson = require('./package.json');
const electron = packageJson.devDependencies.electron;
if (!electron) {
throw new Error('Unable to find electron version in package.json devDependencies');
}
console.log(`Babel is targeting Electron ${electron}`);
const react = packageJson.dependencies.react;
if (!react) {
throw new Error('Unable to find react version in package.json dependencies');
}
const reactTargetMajor = react.split('.')[0];
if (!reactTargetMajor || Number.isNaN(Number.parseInt(reactTargetMajor))) {
throw new Error(`Unable to parse react version from package.json dependencies: "${react}"`);
}
console.log(`React compiler is targeting React ${reactTargetMajor}`);
/** @type {import('@types/babel__core').TransformOptions} */
module.exports = {
targets: {
electron,
},
presets: [
[
'@babel/preset-env',
{
modules: 'commonjs',
targets: {
electron,
},
bugfixes: true, // Use smaller transforms
exclude: [
// Exclude transforms Electron doesn't need
'transform-typeof-symbol',
'transform-regenerator',
'transform-async-to-generator',
],
},
],
],
plugins: [
// Progress tracking plugin
function progressPlugin() {
return {
post() {
updateProgress();
},
};
},
// Add data-component attribute to styled-components for debugging
require.resolve('./babel-plugins/styled-components-data-component'),
[
require.resolve('babel-plugin-react-compiler'),
{
target: reactTargetMajor,
sources: filename => {
// Skip specific file(s) from React Compiler
if (filesIgnoredByReactCompiler.has(filename)) {
skippedFiles.add(filename);
registerCleanup();
return false;
}
return true;
},
logger: {
logEvent(filename, event) {
if (event.kind === 'CompileError') {
registerCleanup();
pendingPromises.push(processError(filename, event));
}
},
},
},
],
],
only: [babelInclude],
ignore: babelIgnore,
};