Skip to content

Commit 72eddcf

Browse files
committed
feat: add babel transpiler
1 parent 3d83019 commit 72eddcf

2 files changed

Lines changed: 109 additions & 8 deletions

File tree

apps/RNApp/ios/RNApp.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
0BC82F3F05A4B925A817D9C2 /* Pods_RNApp_BrownfieldLib.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50AA0434554B7C955DF2969F /* Pods_RNApp_BrownfieldLib.framework */; };
1110
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
12-
5813568A9E846D4865BA27B6 /* Pods_RNApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5BCEA0D1E2A73E8A5E8F35ED /* Pods_RNApp.framework */; };
1311
761780ED2CA45674006654EE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 761780EC2CA45674006654EE /* AppDelegate.swift */; };
1412
79BD1EE92EEBFB76003AA29F /* BrownfieldLib.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 79BD1EE32EEBFB76003AA29F /* BrownfieldLib.framework */; };
1513
79BD1EEA2EEBFB76003AA29F /* BrownfieldLib.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 79BD1EE32EEBFB76003AA29F /* BrownfieldLib.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1614
79F35E8C2EEC1D4500E64860 /* BrownfieldLib.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79F35E8A2EEC1D4500E64860 /* BrownfieldLib.swift */; };
1715
81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */; };
1816
C66C2A65406C527E9529D08F /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB81A68108700A75B9A /* PrivacyInfo.xcprivacy */; };
17+
D3FEBEFB042C029B09B8C0CB /* Pods_RNApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0B1A9990496F5E91A78843A1 /* Pods_RNApp.framework */; };
18+
E652A655257F24686D40360B /* Pods_RNApp_BrownfieldLib.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3113F60F0500A8CFC39FD6F4 /* Pods_RNApp_BrownfieldLib.framework */; };
1919
/* End PBXBuildFile section */
2020

2121
/* Begin PBXContainerItemProxy section */
@@ -43,14 +43,14 @@
4343
/* End PBXCopyFilesBuildPhase section */
4444

4545
/* Begin PBXFileReference section */
46+
0B1A9990496F5E91A78843A1 /* Pods_RNApp.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RNApp.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4647
13B07F961A680F5B00A75B9A /* RNApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RNApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
4748
13B07FB51A68108700A75B9A /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = RNApp/Images.xcassets; sourceTree = "<group>"; };
4849
13B07FB61A68108700A75B9A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = RNApp/Info.plist; sourceTree = "<group>"; };
4950
13B07FB81A68108700A75B9A /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = PrivacyInfo.xcprivacy; path = RNApp/PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
51+
3113F60F0500A8CFC39FD6F4 /* Pods_RNApp_BrownfieldLib.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RNApp_BrownfieldLib.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5052
3B4392A12AC88292D35C810B /* Pods-RNApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RNApp.debug.xcconfig"; path = "Target Support Files/Pods-RNApp/Pods-RNApp.debug.xcconfig"; sourceTree = "<group>"; };
51-
50AA0434554B7C955DF2969F /* Pods_RNApp_BrownfieldLib.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RNApp_BrownfieldLib.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5253
5709B34CF0A7D63546082F79 /* Pods-RNApp.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RNApp.release.xcconfig"; path = "Target Support Files/Pods-RNApp/Pods-RNApp.release.xcconfig"; sourceTree = "<group>"; };
53-
5BCEA0D1E2A73E8A5E8F35ED /* Pods_RNApp.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RNApp.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5454
761780EC2CA45674006654EE /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = AppDelegate.swift; path = RNApp/AppDelegate.swift; sourceTree = "<group>"; };
5555
79BD1EE32EEBFB76003AA29F /* BrownfieldLib.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = BrownfieldLib.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5656
79F35E8A2EEC1D4500E64860 /* BrownfieldLib.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrownfieldLib.swift; sourceTree = "<group>"; };
@@ -66,15 +66,15 @@
6666
buildActionMask = 2147483647;
6767
files = (
6868
79BD1EE92EEBFB76003AA29F /* BrownfieldLib.framework in Frameworks */,
69-
5813568A9E846D4865BA27B6 /* Pods_RNApp.framework in Frameworks */,
69+
D3FEBEFB042C029B09B8C0CB /* Pods_RNApp.framework in Frameworks */,
7070
);
7171
runOnlyForDeploymentPostprocessing = 0;
7272
};
7373
79BD1EE02EEBFB76003AA29F /* Frameworks */ = {
7474
isa = PBXFrameworksBuildPhase;
7575
buildActionMask = 2147483647;
7676
files = (
77-
0BC82F3F05A4B925A817D9C2 /* Pods_RNApp_BrownfieldLib.framework in Frameworks */,
77+
E652A655257F24686D40360B /* Pods_RNApp_BrownfieldLib.framework in Frameworks */,
7878
);
7979
runOnlyForDeploymentPostprocessing = 0;
8080
};
@@ -97,8 +97,8 @@
9797
isa = PBXGroup;
9898
children = (
9999
ED297162215061F000B7C4FE /* JavaScriptCore.framework */,
100-
5BCEA0D1E2A73E8A5E8F35ED /* Pods_RNApp.framework */,
101-
50AA0434554B7C955DF2969F /* Pods_RNApp_BrownfieldLib.framework */,
100+
0B1A9990496F5E91A78843A1 /* Pods_RNApp.framework */,
101+
3113F60F0500A8CFC39FD6F4 /* Pods_RNApp_BrownfieldLib.framework */,
102102
);
103103
name = Frameworks;
104104
sourceTree = "<group>";

packages/brownfield-navigation/src/scripts/brownfield-navigation.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
import * as fs from 'node:fs';
22+
import { createRequire } from 'node:module';
2223
import * 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+
215278
function 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

Comments
 (0)