Skip to content

Commit 3eae11e

Browse files
p-sunfacebook-github-bot
authored andcommitted
Migrate dev method codegenNativeComponent to be Bridgeless compatible
Summary: Changelog: [Internal] Migrate dev method codegenNativeComponent to be Bridgeless compatible Reviewed By: RSNara Differential Revision: D34513074 fbshipit-source-id: d71fbf066453ac8c407d0cf41c2dc7fa80c87688
1 parent 34d3373 commit 3eae11e

File tree

4 files changed

+28
-37
lines changed

4 files changed

+28
-37
lines changed

Libraries/Utilities/__tests__/codegenNativeComponent-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jest.mock(
2323
() => componentName => componentName,
2424
);
2525
jest
26-
.spyOn(UIManager, 'getViewManagerConfig')
26+
.spyOn(UIManager, 'hasViewManagerConfig')
2727
.mockImplementation(componentName =>
2828
componentName.includes('ComponentNameDoesNotExist') ? false : true,
2929
);

Libraries/Utilities/codegenNativeComponent.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,36 @@ type Options = $ReadOnly<{|
2424

2525
export type NativeComponentType<T> = HostComponent<T>;
2626

27+
// If this function runs then that means the view configs were not
28+
// generated at build time using `GenerateViewConfigJs.js`. Thus
29+
// we need to `requireNativeComponent` to get the view configs from view managers.
30+
// `requireNativeComponent` is not available in Bridgeless mode.
31+
// e.g. This function runs at runtime if `codegenNativeComponent` was not called
32+
// from a file suffixed with NativeComponent.js.
2733
function codegenNativeComponent<Props>(
2834
componentName: string,
2935
options?: Options,
3036
): NativeComponentType<Props> {
37+
const errorMessage =
38+
"Native Component '" +
39+
componentName +
40+
"' that calls codegenNativeComponent was not code generated at build time. Please check its definition.";
41+
if (global.RN$Bridgeless === true) {
42+
console.error(errorMessage);
43+
} else {
44+
console.warn(errorMessage);
45+
}
3146
let componentNameInUse =
3247
options && options.paperComponentName != null
3348
? options.paperComponentName
3449
: componentName;
3550

3651
if (options != null && options.paperComponentNameDeprecated != null) {
37-
if (UIManager.getViewManagerConfig(componentName)) {
52+
if (UIManager.hasViewManagerConfig(componentName)) {
3853
componentNameInUse = componentName;
3954
} else if (
4055
options.paperComponentNameDeprecated != null &&
41-
UIManager.getViewManagerConfig(options.paperComponentNameDeprecated)
56+
UIManager.hasViewManagerConfig(options.paperComponentNameDeprecated)
4257
) {
4358
componentNameInUse = options.paperComponentNameDeprecated;
4459
} else {
@@ -50,10 +65,6 @@ function codegenNativeComponent<Props>(
5065
}
5166
}
5267

53-
// If this function is run at runtime then that means the view configs were not
54-
// generated with the view config babel plugin, so we need to require the native component.
55-
//
56-
// This will be useful during migration, but eventually this will error.
5768
return (requireNativeComponent<Props>(
5869
componentNameInUse,
5970
): HostComponent<Props>);

packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,12 @@ const DeprecatedComponentNameCheckTemplate = ({
141141
paperComponentNameDeprecated: string,
142142
}) =>
143143
`
144-
if (global.RN$Bridgeless) {
145-
if (UIManager.hasViewManagerConfig('${componentName}')) {
146-
nativeComponentName = '${componentName}';
147-
} else if (UIManager.hasViewManagerConfig('${paperComponentNameDeprecated}')) {
148-
nativeComponentName = '${paperComponentNameDeprecated}';
149-
} else {
150-
throw new Error('Failed to find native component for either "${componentName}" or "${paperComponentNameDeprecated}", with SVC enabled.');
151-
}
144+
if (UIManager.hasViewManagerConfig('${componentName}')) {
145+
nativeComponentName = '${componentName}';
146+
} else if (UIManager.hasViewManagerConfig('${paperComponentNameDeprecated}')) {
147+
nativeComponentName = '${paperComponentNameDeprecated}';
152148
} else {
153-
if (UIManager.getViewManagerConfig('${componentName}')) {
154-
nativeComponentName = '${componentName}';
155-
} else if (UIManager.getViewManagerConfig('${paperComponentNameDeprecated}')) {
156-
nativeComponentName = '${paperComponentNameDeprecated}';
157-
} else {
158-
throw new Error('Failed to find native component for either "${componentName}" or "${paperComponentNameDeprecated}", with SVC disabled.');
159-
}
149+
throw new Error('Failed to find native component for either "${componentName}" or "${paperComponentNameDeprecated}"');
160150
}
161151
`.trim();
162152

packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap

+5-15
Original file line numberDiff line numberDiff line change
@@ -1070,22 +1070,12 @@ const NativeComponentRegistry = require('react-native/Libraries/NativeComponent/
10701070
const {UIManager} = require(\\"react-native\\")
10711071
10721072
let nativeComponentName = 'NativeComponentName';
1073-
if (global.RN$Bridgeless) {
1074-
if (UIManager.hasViewManagerConfig('NativeComponentName')) {
1075-
nativeComponentName = 'NativeComponentName';
1076-
} else if (UIManager.hasViewManagerConfig('DeprecatedNativeComponentName')) {
1077-
nativeComponentName = 'DeprecatedNativeComponentName';
1078-
} else {
1079-
throw new Error('Failed to find native component for either \\"NativeComponentName\\" or \\"DeprecatedNativeComponentName\\", with SVC enabled.');
1080-
}
1073+
if (UIManager.hasViewManagerConfig('NativeComponentName')) {
1074+
nativeComponentName = 'NativeComponentName';
1075+
} else if (UIManager.hasViewManagerConfig('DeprecatedNativeComponentName')) {
1076+
nativeComponentName = 'DeprecatedNativeComponentName';
10811077
} else {
1082-
if (UIManager.getViewManagerConfig('NativeComponentName')) {
1083-
nativeComponentName = 'NativeComponentName';
1084-
} else if (UIManager.getViewManagerConfig('DeprecatedNativeComponentName')) {
1085-
nativeComponentName = 'DeprecatedNativeComponentName';
1086-
} else {
1087-
throw new Error('Failed to find native component for either \\"NativeComponentName\\" or \\"DeprecatedNativeComponentName\\", with SVC disabled.');
1088-
}
1078+
throw new Error('Failed to find native component for either \\"NativeComponentName\\" or \\"DeprecatedNativeComponentName\\"');
10891079
}
10901080
10911081
export const __INTERNAL_VIEW_CONFIG = {

0 commit comments

Comments
 (0)