From 7c63e0d5bc895e4377fbae461f66f5bc2f8ed774 Mon Sep 17 00:00:00 2001 From: Paige Sun Date: Mon, 10 Jan 2022 17:24:43 -0800 Subject: [PATCH] 3/5 For codegenNativeComponent, with SVC enabled, use UIManager hasViewManagerConfig instead of getViewManagerConfig Summary: With SVC enabled for codegenNativeComponent, use `UIManager.hasViewManagerConfig(viewManagerName)` instead of `UIManager.getViewManagerConfig(viewManagerName)` to check for whether the native component is in the app BUCK binary. This is safe because `global.__fbStaticViewConfig` is gated with MC, and the purpose of SVCs is to stop using `getViewManagerConfig` to get view configs from the Paper view managers. Currently no QEs have SVCs enabled. Changelog: [Fabric][JS] For codegenNativeComponent, with SVC enabled, use UIManager hasViewManagerConfig instead of getViewManagerConfig Reviewed By: RSNara Differential Revision: D33511365 fbshipit-source-id: 58c7020903137e2b5c80ef34a956be04de51628b --- .../components/GenerateViewConfigJs.js | 25 +++++++++++++++---- .../GenerateViewConfigJs-test.js.snap | 21 ++++++++++++---- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js b/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js index 7e69231a9a2..259571e1683 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js +++ b/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js @@ -127,6 +127,10 @@ export default NativeComponentRegistry.get(nativeComponentName, () => VIEW_CONFI `.trim(); }; +// If static view configs are enabled, get whether the native component exists +// in the app binary using hasViewManagerConfig() instead of getViewManagerConfig(). +// Old getViewManagerConfig() checks for the existance of the native Paper view manager. +// New hasViewManagerConfig() queries Fabric’s native component registry directly. const DeprecatedComponentNameCheckTemplate = ({ componentName, paperComponentNameDeprecated, @@ -135,12 +139,23 @@ const DeprecatedComponentNameCheckTemplate = ({ paperComponentNameDeprecated: string, }) => ` -if (UIManager.getViewManagerConfig('${componentName}')) { - nativeComponentName = '${componentName}'; -} else if (UIManager.getViewManagerConfig('${paperComponentNameDeprecated}')) { - nativeComponentName = '${paperComponentNameDeprecated}'; +const staticViewConfigsEnabled = global.__fbStaticViewConfig === true; +if (staticViewConfigsEnabled) { + if (UIManager.hasViewManagerConfig('${componentName}')) { + nativeComponentName = '${componentName}'; + } else if (UIManager.hasViewManagerConfig('${paperComponentNameDeprecated}')) { + nativeComponentName = '${paperComponentNameDeprecated}'; + } else { + throw new Error('Failed to find native component for either "${componentName}" or "${paperComponentNameDeprecated}", with SVC enabled.'); + } } else { - throw new Error('Failed to find native component for either "${componentName}" or "${paperComponentNameDeprecated}"'); + if (UIManager.getViewManagerConfig('${componentName}')) { + nativeComponentName = '${componentName}'; + } else if (UIManager.getViewManagerConfig('${paperComponentNameDeprecated}')) { + nativeComponentName = '${paperComponentNameDeprecated}'; + } else { + throw new Error('Failed to find native component for either "${componentName}" or "${paperComponentNameDeprecated}", with SVC disabled.'); + } } `.trim(); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap index 2978af1e757..b4975e0d639 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap +++ b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap @@ -973,12 +973,23 @@ const NativeComponentRegistry = require('react-native/Libraries/NativeComponent/ const {UIManager} = require(\\"react-native\\") let nativeComponentName = 'NativeComponentName'; -if (UIManager.getViewManagerConfig('NativeComponentName')) { - nativeComponentName = 'NativeComponentName'; -} else if (UIManager.getViewManagerConfig('DeprecatedNativeComponentName')) { - nativeComponentName = 'DeprecatedNativeComponentName'; +const staticViewConfigsEnabled = global.__fbStaticViewConfig === true; +if (staticViewConfigsEnabled) { + if (UIManager.hasViewManagerConfig('NativeComponentName')) { + nativeComponentName = 'NativeComponentName'; + } else if (UIManager.hasViewManagerConfig('DeprecatedNativeComponentName')) { + nativeComponentName = 'DeprecatedNativeComponentName'; + } else { + throw new Error('Failed to find native component for either \\"NativeComponentName\\" or \\"DeprecatedNativeComponentName\\", with SVC enabled.'); + } } else { - throw new Error('Failed to find native component for either \\"NativeComponentName\\" or \\"DeprecatedNativeComponentName\\"'); + if (UIManager.getViewManagerConfig('NativeComponentName')) { + nativeComponentName = 'NativeComponentName'; + } else if (UIManager.getViewManagerConfig('DeprecatedNativeComponentName')) { + nativeComponentName = 'DeprecatedNativeComponentName'; + } else { + throw new Error('Failed to find native component for either \\"NativeComponentName\\" or \\"DeprecatedNativeComponentName\\", with SVC disabled.'); + } } export default NativeComponentRegistry.get(nativeComponentName, () => ({ uiViewClassName: 'NativeComponentName',