1919 */
2020
2121import * as fs from 'node:fs' ;
22+ import { createRequire } from 'node:module' ;
2223import * as path from 'node:path' ;
2324
2425// Resolve script directory in both CJS and ESM runtimes.
@@ -212,6 +213,68 @@ export default BrownfieldNavigation;
212213` ;
213214}
214215
216+ function transpileWithConsumerBabel ( tsCode : string ) : string {
217+ const nodeRequire = createRequire ( scriptFile ) ;
218+ const moduleCandidates = [ process . cwd ( ) , PACKAGE_ROOT ] ;
219+
220+ function resolveOrThrow ( moduleName : string ) : string {
221+ for ( const modulePath of moduleCandidates ) {
222+ try {
223+ return nodeRequire . resolve ( moduleName , { paths : [ modulePath ] } ) ;
224+ } catch {
225+ // Try next location
226+ }
227+ }
228+
229+ throw new Error (
230+ `Could not resolve "${ moduleName } ". Install it in your app devDependencies.`
231+ ) ;
232+ }
233+
234+ const babelCorePath = resolveOrThrow ( '@babel/core' ) ;
235+ const rnPresetPath = resolveOrThrow ( '@react-native/babel-preset' ) ;
236+ const babelCore = nodeRequire ( babelCorePath ) as {
237+ transformSync : (
238+ source : string ,
239+ options : Record < string , unknown >
240+ ) => { code ?: string | null } | null ;
241+ } ;
242+
243+ const transformed = babelCore . transformSync ( tsCode , {
244+ filename : 'index.ts' ,
245+ babelrc : false ,
246+ configFile : false ,
247+ comments : false ,
248+ compact : true ,
249+ minified : true ,
250+ presets : [ [ rnPresetPath , { } ] ] ,
251+ } ) ;
252+
253+ if ( ! transformed ?. code ) {
254+ throw new Error ( 'Babel transpilation failed for generated index.ts' ) ;
255+ }
256+
257+ return transformed . code ;
258+ }
259+
260+ function generateIndexDts ( methods : MethodSignature [ ] ) : string {
261+ const methodSignatures = methods
262+ . map ( ( m ) => {
263+ const params = m . params
264+ . map ( ( p ) => `${ p . name } ${ p . optional ? '?' : '' } : ${ p . type } ` )
265+ . join ( ', ' ) ;
266+ return ` ${ m . name } : (${ params } ) => ${ m . returnType } ;` ;
267+ } )
268+ . join ( '\n' ) ;
269+
270+ return `declare const BrownfieldNavigation: {
271+ ${ methodSignatures }
272+ };
273+
274+ export default BrownfieldNavigation;
275+ ` ;
276+ }
277+
215278function generateSwiftDelegate ( methods : MethodSignature [ ] ) : string {
216279 const protocolMethods = methods
217280 . map ( ( m ) => {
@@ -410,6 +473,8 @@ function main(): void {
410473 // Generate all files
411474 const turboModuleSpec = generateTurboModuleSpec ( methods ) ;
412475 const indexTS = generateIndexTs ( methods ) ;
476+ const transpiledIndexJs = transpileWithConsumerBabel ( indexTS ) ;
477+ const indexDts = generateIndexDts ( methods ) ;
413478 const swiftDelegate = generateSwiftDelegate ( methods ) ;
414479 const objcImpl = generateObjCImplementation ( methods ) ;
415480
@@ -418,6 +483,12 @@ function main(): void {
418483 console . log ( turboModuleSpec ) ;
419484 console . log ( '\n--- Generated: src/index.ts ---' ) ;
420485 console . log ( indexTS ) ;
486+ console . log ( '\n--- Generated (Babel): lib/{commonjs,module}/index.js ---' ) ;
487+ console . log ( transpiledIndexJs ) ;
488+ console . log (
489+ '\n--- Generated: lib/typescript/{commonjs,module}/src/index.d.ts ---'
490+ ) ;
491+ console . log ( indexDts ) ;
421492 console . log ( '\n--- Generated: ios/BrownfieldNavigationDelegate.swift ---' ) ;
422493 console . log ( swiftDelegate ) ;
423494 console . log ( '\n--- Generated: ios/NativeBrownfieldNavigation.mm ---' ) ;
@@ -433,6 +504,24 @@ function main(): void {
433504 'NativeBrownfieldNavigation.ts'
434505 ) ,
435506 navigationTs : path . join ( PACKAGE_ROOT , 'src' , 'index.ts' ) ,
507+ commonjsIndexJs : path . join ( PACKAGE_ROOT , 'lib' , 'commonjs' , 'index.js' ) ,
508+ moduleIndexJs : path . join ( PACKAGE_ROOT , 'lib' , 'module' , 'index.js' ) ,
509+ commonjsIndexDts : path . join (
510+ PACKAGE_ROOT ,
511+ 'lib' ,
512+ 'typescript' ,
513+ 'commonjs' ,
514+ 'src' ,
515+ 'index.d.ts'
516+ ) ,
517+ moduleIndexDts : path . join (
518+ PACKAGE_ROOT ,
519+ 'lib' ,
520+ 'typescript' ,
521+ 'module' ,
522+ 'src' ,
523+ 'index.d.ts'
524+ ) ,
436525 swiftDelegate : path . join (
437526 PACKAGE_ROOT ,
438527 'ios' ,
@@ -447,6 +536,18 @@ function main(): void {
447536 fs . writeFileSync ( paths . navigationTs , indexTS ) ;
448537 console . log ( `Generated: ${ paths . navigationTs } ` ) ;
449538
539+ fs . writeFileSync ( paths . commonjsIndexJs , transpiledIndexJs ) ;
540+ console . log ( `Generated: ${ paths . commonjsIndexJs } ` ) ;
541+
542+ fs . writeFileSync ( paths . moduleIndexJs , transpiledIndexJs ) ;
543+ console . log ( `Generated: ${ paths . moduleIndexJs } ` ) ;
544+
545+ fs . writeFileSync ( paths . commonjsIndexDts , indexDts ) ;
546+ console . log ( `Generated: ${ paths . commonjsIndexDts } ` ) ;
547+
548+ fs . writeFileSync ( paths . moduleIndexDts , indexDts ) ;
549+ console . log ( `Generated: ${ paths . moduleIndexDts } ` ) ;
550+
450551 fs . writeFileSync ( paths . swiftDelegate , swiftDelegate ) ;
451552 console . log ( `Generated: ${ paths . swiftDelegate } ` ) ;
452553
0 commit comments