diff --git a/packages/react-native/scripts/codegen/__tests__/__snapshots__/generate-artifacts-executor-test.js.snap b/packages/react-native/scripts/codegen/__tests__/__snapshots__/generate-artifacts-executor-test.js.snap index 6bc79e6875e4..4f2318466f51 100644 --- a/packages/react-native/scripts/codegen/__tests__/__snapshots__/generate-artifacts-executor-test.js.snap +++ b/packages/react-native/scripts/codegen/__tests__/__snapshots__/generate-artifacts-executor-test.js.snap @@ -328,10 +328,24 @@ exports[`execute test-app "ReactCodegen/RCTThirdPartyComponentsProvider.mm" shou static dispatch_once_t nativeComponentsToken; dispatch_once(&nativeComponentsToken, ^{ - thirdPartyComponents = @{ - @\\"TestAppComponent\\": NSClassFromString(@\\"RCTTestAppComponent\\"), // test-app - @\\"TestLibraryComponent\\": NSClassFromString(@\\"RCTTestLibraryComponent\\"), // test-library + NSDictionary * componentMapping = @{ + @\\"TestAppComponent\\": @\\"RCTTestAppComponent\\", // test-app + @\\"TestLibraryComponent\\": @\\"RCTTestLibraryComponent\\", // test-library }; + + NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:componentMapping.count]; + + for (NSString *key in componentMapping) { + // Nil for a library this target does not link (an app extension, macOS). + Class klass = NSClassFromString(componentMapping[key]); + if (!klass) { + continue; + } + + [dict setObject:klass forKey:key]; + } + + thirdPartyComponents = dict; }); return thirdPartyComponents; @@ -822,9 +836,23 @@ exports[`execute test-app-legacy "ReactCodegen/RCTThirdPartyComponentsProvider.m static dispatch_once_t nativeComponentsToken; dispatch_once(&nativeComponentsToken, ^{ - thirdPartyComponents = @{ - @\\"TestAppDeprecatedComponent\\": NSClassFromString(@\\"RCTTestAppDeprecatedComponentClass\\"), // test-app-legacy + NSDictionary * componentMapping = @{ + @\\"TestAppDeprecatedComponent\\": @\\"RCTTestAppDeprecatedComponentClass\\", // test-app-legacy }; + + NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:componentMapping.count]; + + for (NSString *key in componentMapping) { + // Nil for a library this target does not link (an app extension, macOS). + Class klass = NSClassFromString(componentMapping[key]); + if (!klass) { + continue; + } + + [dict setObject:klass forKey:key]; + } + + thirdPartyComponents = dict; }); return thirdPartyComponents; diff --git a/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js b/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js index ea1aca23327f..207feaa12dcf 100644 --- a/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js +++ b/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js @@ -297,7 +297,7 @@ describe('generateRCTThirdPartyComponents', () => { 'utf8', ); expect(generatedFile).toContain( - '@"ExampleComponent": NSClassFromString(@"RCTExampleComponent"), // component-library', + '@"ExampleComponent": @"RCTExampleComponent", // component-library', ); } finally { fs.rmSync(libraryPath, {recursive: true, force: true}); diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTThirdPartyComponents.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTThirdPartyComponents.js index 84857672fa0f..dfe462782a8a 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTThirdPartyComponents.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTThirdPartyComponents.js @@ -139,7 +139,7 @@ function generateRCTThirdPartyComponents( .flatMap(library => { const components = componentsInLibraries[library]; return components.map(({componentName, className}) => { - return `\t\t@"${componentName}": NSClassFromString(@"${className}"), // ${library}`; + return `\t\t@"${componentName}": @"${className}", // ${library}`; }); }) .join('\n'); diff --git a/packages/react-native/scripts/codegen/templates/RCTThirdPartyComponentsProviderMM.template b/packages/react-native/scripts/codegen/templates/RCTThirdPartyComponentsProviderMM.template index 4ce510709e33..6c50d6d9c98c 100644 --- a/packages/react-native/scripts/codegen/templates/RCTThirdPartyComponentsProviderMM.template +++ b/packages/react-native/scripts/codegen/templates/RCTThirdPartyComponentsProviderMM.template @@ -19,9 +19,23 @@ static dispatch_once_t nativeComponentsToken; dispatch_once(&nativeComponentsToken, ^{ - thirdPartyComponents = @{ + NSDictionary * componentMapping = @{ {thirdPartyComponentsMapping} }; + + NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:componentMapping.count]; + + for (NSString *key in componentMapping) { + // Nil for a library this target does not link (an app extension, macOS). + Class klass = NSClassFromString(componentMapping[key]); + if (!klass) { + continue; + } + + [dict setObject:klass forKey:key]; + } + + thirdPartyComponents = dict; }); return thirdPartyComponents;