1- import { createRequire } from 'node:module'
2- import { join } from 'node:path'
3- import {
4- type ConfigAPI ,
5- type ConfigFunction ,
6- type TransformOptions ,
7- transformFileAsync ,
8- } from '@babel/core'
1+ import { type TransformOptions , transformFileAsync } from '@babel/core'
92import type { Logger } from 'babel-plugin-react-compiler'
103
11- function loadConfig ( configPath : string ) {
12- const require = createRequire ( import . meta. url )
13- const babelConfigPath = join ( process . cwd ( ) , configPath )
14- const babelConfigFn : ConfigFunction = require ( babelConfigPath )
15- const babelConfig = babelConfigFn ( {
16- cache : {
17- using : ( callback ) => callback ( ) ,
18- } ,
19- } as ConfigAPI )
20-
21- return babelConfig
22- }
23-
24- function setCustomReactCompilerLogger (
25- babelConfig : TransformOptions ,
26- customReactCompilerLogger : Logger ,
27- ) {
28- if ( ! babelConfig ?. plugins ) {
29- throw new Error ( 'Failed to load Babel config' )
30- }
31-
32- const reactCompilerPlugin = babelConfig . plugins . find (
33- ( plugin ) => Array . isArray ( plugin ) && plugin [ 0 ] === 'babel-plugin-react-compiler' ,
34- )
35- if ( ! reactCompilerPlugin || ! Array . isArray ( reactCompilerPlugin ) ) {
36- throw new Error ( 'Failed to find React Compiler plugin in Babel config' )
4+ function createConfig ( logger : Logger ) : TransformOptions {
5+ return {
6+ presets : [ [ '@babel/preset-react' , { runtime : 'automatic' } ] , '@babel/preset-typescript' ] ,
7+ plugins : [ [ 'babel-plugin-react-compiler' , { logger } ] ] ,
378 }
38- reactCompilerPlugin [ 1 ] = { ...reactCompilerPlugin [ 1 ] , logger : customReactCompilerLogger }
399}
4010
4111async function compileFileWithBabel ( filePath : string , config : TransformOptions ) {
@@ -49,23 +19,13 @@ async function compileFileWithBabel(filePath: string, config: TransformOptions)
4919
5020async function compileFiles ( {
5121 filePaths,
52- configPath,
5322 customReactCompilerLogger,
5423} : {
5524 filePaths : string [ ]
56- configPath : string
5725 customReactCompilerLogger : Logger
5826} ) {
59- const config = loadConfig ( configPath )
60- setCustomReactCompilerLogger ( config , customReactCompilerLogger )
61-
62- const processJobs : Promise < void > [ ] = [ ]
63-
64- for ( const filePath of filePaths ) {
65- processJobs . push ( compileFileWithBabel ( filePath , config ) )
66- }
67-
68- await Promise . all ( processJobs )
27+ const config = createConfig ( customReactCompilerLogger )
28+ await Promise . all ( filePaths . map ( ( filePath ) => compileFileWithBabel ( filePath , config ) ) )
6929}
7030
7131export { compileFiles }
0 commit comments