From 3ad5e817b982d4e71987620e9d3db0ac0b1925ec Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Thu, 19 Nov 2026 16:27:14 +0000 Subject: [PATCH] [DevTools][iOS] Compile React DevTools runtime settings modules out of production builds --- packages/react-native/Package.swift | 5 ++++- .../CoreModules/RCTDevToolsRuntimeSettingsModule.mm | 8 ++++++++ .../ReactCommon/devtoolsruntimesettings/CMakeLists.txt | 6 ++++++ .../DevToolsRuntimeSettings.cpp | 4 ++++ .../devtoolsruntimesettings/CMakeLists.txt | 6 ++++++ .../DevToolsRuntimeSettingsModule.cpp | 10 +++++++++- packages/react-native/scripts/cocoapods/utils.rb | 1 + 7 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/react-native/Package.swift b/packages/react-native/Package.swift index ac1611c59c07..c6a1dc3a7691 100644 --- a/packages/react-native/Package.swift +++ b/packages/react-native/Package.swift @@ -420,7 +420,10 @@ let reactCoreModules = RNTarget( path: "React/CoreModules", linkedFrameworks: ["Accessibility"], excludedPaths: ["PlatformStubs/RCTStatusBarManager.mm"], - dependencies: [.reactNativeDependencies, .jsi, .yoga, .reactTurboModuleCore, .reactFeatureFlags] + dependencies: [.reactNativeDependencies, .jsi, .yoga, .reactTurboModuleCore, .reactFeatureFlags], + defines: [ + CXXSetting.define("REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY", to: "1", .when(configuration: BuildConfiguration.debug)) + ] ) /// React-runtimeCore.podspec diff --git a/packages/react-native/React/CoreModules/RCTDevToolsRuntimeSettingsModule.mm b/packages/react-native/React/CoreModules/RCTDevToolsRuntimeSettingsModule.mm index 5b5a986da14b..b68aad2b3a06 100644 --- a/packages/react-native/React/CoreModules/RCTDevToolsRuntimeSettingsModule.mm +++ b/packages/react-native/React/CoreModules/RCTDevToolsRuntimeSettingsModule.mm @@ -10,6 +10,8 @@ #import "CoreModulesPlugins.h" +#ifdef REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY + struct Config { bool shouldReloadAndProfile = false; bool recordChangeDescriptions = false; @@ -51,7 +53,13 @@ - (void)setReloadAndProfileConfig:(JS::NativeReactDevToolsRuntimeSettingsModule: @end +#endif // REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY + Class RCTDevToolsRuntimeSettingsCls(void) { +#ifdef REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY return RCTDevToolsRuntimeSettingsModule.class; +#else + return nil; +#endif } diff --git a/packages/react-native/ReactCommon/devtoolsruntimesettings/CMakeLists.txt b/packages/react-native/ReactCommon/devtoolsruntimesettings/CMakeLists.txt index f2d83dc92472..f9c7e7cfb389 100644 --- a/packages/react-native/ReactCommon/devtoolsruntimesettings/CMakeLists.txt +++ b/packages/react-native/ReactCommon/devtoolsruntimesettings/CMakeLists.txt @@ -15,3 +15,9 @@ target_include_directories(devtoolsruntimesettings PUBLIC .) target_link_libraries(devtoolsruntimesettings jsi react_codegen_rncore react_cxxstableapi) target_compile_reactnative_options(devtoolsruntimesettings PRIVATE) + +if(${CMAKE_BUILD_TYPE} MATCHES Debug OR REACT_NATIVE_DEBUG_OPTIMIZED) + target_compile_options(devtoolsruntimesettings PRIVATE + -DREACT_NATIVE_DEBUGGER_ENABLED_DEVONLY=1 + ) +endif () diff --git a/packages/react-native/ReactCommon/devtoolsruntimesettings/DevToolsRuntimeSettings.cpp b/packages/react-native/ReactCommon/devtoolsruntimesettings/DevToolsRuntimeSettings.cpp index 524005b5bc23..832a4978bedd 100644 --- a/packages/react-native/ReactCommon/devtoolsruntimesettings/DevToolsRuntimeSettings.cpp +++ b/packages/react-native/ReactCommon/devtoolsruntimesettings/DevToolsRuntimeSettings.cpp @@ -7,6 +7,8 @@ #include "DevToolsRuntimeSettings.h" +#ifdef REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY + namespace facebook::react { void DevToolsRuntimeSettings::setReloadAndProfileConfig( @@ -25,3 +27,5 @@ DevToolsRuntimeSettings::getReloadAndProfileConfig() const { }; } // namespace facebook::react + +#endif // REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY diff --git a/packages/react-native/ReactCommon/react/nativemodule/devtoolsruntimesettings/CMakeLists.txt b/packages/react-native/ReactCommon/react/nativemodule/devtoolsruntimesettings/CMakeLists.txt index b6ad599ce0a5..c6116542d52f 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/devtoolsruntimesettings/CMakeLists.txt +++ b/packages/react-native/ReactCommon/react/nativemodule/devtoolsruntimesettings/CMakeLists.txt @@ -19,3 +19,9 @@ target_link_libraries(react_nativemodule_devtoolsruntimesettings ) target_compile_reactnative_options(react_nativemodule_devtoolsruntimesettings PRIVATE) target_compile_options(react_nativemodule_devtoolsruntimesettings PRIVATE -Wpedantic) + +if(${CMAKE_BUILD_TYPE} MATCHES Debug OR REACT_NATIVE_DEBUG_OPTIMIZED) + target_compile_options(react_nativemodule_devtoolsruntimesettings PRIVATE + -DREACT_NATIVE_DEBUGGER_ENABLED_DEVONLY=1 + ) +endif () diff --git a/packages/react-native/ReactCommon/react/nativemodule/devtoolsruntimesettings/DevToolsRuntimeSettingsModule.cpp b/packages/react-native/ReactCommon/react/nativemodule/devtoolsruntimesettings/DevToolsRuntimeSettingsModule.cpp index eac8accbe97c..a74dd10af2fd 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/devtoolsruntimesettings/DevToolsRuntimeSettingsModule.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/devtoolsruntimesettings/DevToolsRuntimeSettingsModule.cpp @@ -12,11 +12,17 @@ std::shared_ptr ReactDevToolsRuntimeSettingsModuleProvider( - std::shared_ptr jsInvoker) { + [[maybe_unused]] std::shared_ptr jsInvoker) { +#ifdef REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY return std::make_shared( std::move(jsInvoker)); +#else + return nullptr; +#endif } +#ifdef REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY + namespace facebook::react { DevToolsRuntimeSettingsModule::DevToolsRuntimeSettingsModule( @@ -35,3 +41,5 @@ DevToolsRuntimeSettingsModule::getReloadAndProfileConfig(jsi::Runtime& /*rt*/) { }; } // namespace facebook::react + +#endif // REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY diff --git a/packages/react-native/scripts/cocoapods/utils.rb b/packages/react-native/scripts/cocoapods/utils.rb index 84c24e88b8ee..d3003f32ea56 100644 --- a/packages/react-native/scripts/cocoapods/utils.rb +++ b/packages/react-native/scripts/cocoapods/utils.rb @@ -116,6 +116,7 @@ def self.set_gcc_preprocessor_definition_for_debugger(installer) self.add_build_settings_to_pod(installer, "GCC_PREPROCESSOR_DEFINITIONS", "REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY=1", "React-jsinspectornetwork", :debug) self.add_build_settings_to_pod(installer, "GCC_PREPROCESSOR_DEFINITIONS", "REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY=1", "React-RCTNetwork", :debug) self.add_build_settings_to_pod(installer, "GCC_PREPROCESSOR_DEFINITIONS", "REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY=1", "React-networking", :debug) + self.add_build_settings_to_pod(installer, "GCC_PREPROCESSOR_DEFINITIONS", "REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY=1", "React-CoreModules", :debug) end # The react/cxxstableapi guards turn a direct include of a fine-grained React Native