diff --git a/.gitignore b/.gitignore index 693bb50a6..17160ea7a 100644 --- a/.gitignore +++ b/.gitignore @@ -89,9 +89,6 @@ packages/**/*.tgz # Jest coverage -# Auto-generated iOS Header -packages/react-native-session-replay/ios/Sources/RCTVersion.h - # Datadog temporary files .tmp *.tgz diff --git a/packages/react-native-session-replay/DatadogSDKReactNativeSessionReplay.podspec b/packages/react-native-session-replay/DatadogSDKReactNativeSessionReplay.podspec index 60ff29d7d..366846bb3 100644 --- a/packages/react-native-session-replay/DatadogSDKReactNativeSessionReplay.podspec +++ b/packages/react-native-session-replay/DatadogSDKReactNativeSessionReplay.podspec @@ -2,6 +2,39 @@ require "json" package = JSON.parse(File.read(File.join(__dir__, "package.json"))) +# The native code branches on the React Native version at compile time (RCT_VERSION_* macros). +# Read it from the React Native used by the app being installed, rather than from this package: +# in a monorepo, this package can be shared by apps on different React Native versions. +installation_root = Pod::Config.instance.installation_root +rn_package_json = nil +if ENV['REACT_NATIVE_PATH'] + # Set by `use_react_native!` (RN >= 0.72) to the React Native configured in the Podfile. + # Relative to the installation root on older versions, absolute on newer ones. + rn_package_json = installation_root.join(ENV['REACT_NATIVE_PATH'], 'package.json').to_s +end +unless rn_package_json && File.exist?(rn_package_json) + # Autolinking loads this podspec before `use_react_native!` runs, so resolve react-native from + # the app like its Podfile does. The path is passed as an argument, so it is never parsed as code. + begin + rn_package_json = Pod::Executable.execute_command('node', [ + '--print', + "require.resolve('react-native/package.json', {paths: [process.argv[1]]})", + installation_root.to_s + ], false).lines.map(&:strip).find { |line| File.exist?(line) } + rescue StandardError + rn_package_json = nil + end +end + +if rn_package_json && File.exist?(rn_package_json) + rn_major, rn_minor, rn_patch = JSON.parse(File.read(rn_package_json))["version"].split(".").map(&:to_i) + rn_version_flags = "-DRCT_VERSION_MAJOR=#{rn_major} -DRCT_VERSION_MINOR=#{rn_minor} -DRCT_VERSION_PATCH=#{rn_patch}" +else + # Don't fail here: this podspec is evaluated again once REACT_NATIVE_PATH is set. + Pod::UI.warn "DatadogSDKReactNativeSessionReplay: unable to find react-native from #{installation_root}." + rn_version_flags = "" +end + Pod::Spec.new do |s| s.name = "DatadogSDKReactNativeSessionReplay" s.version = package["version"] @@ -48,7 +81,7 @@ Pod::Spec.new do |s| } if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then - s.compiler_flags = "-DRCT_NEW_ARCH_ENABLED=1" + s.compiler_flags = "-DRCT_NEW_ARCH_ENABLED=1 #{rn_version_flags}" xcconfig.merge!({ "DEFINES_MODULE" => "YES", @@ -64,6 +97,7 @@ Pod::Spec.new do |s| Pod::UI.warn "Using Datadog React Native Session Replay with new architecture on RN < 0.71 is discouraged and not officially supported." end else + s.compiler_flags = rn_version_flags s.pod_target_xcconfig = xcconfig end end diff --git a/packages/react-native-session-replay/ios/Sources/RCTFabricWrapper.mm b/packages/react-native-session-replay/ios/Sources/RCTFabricWrapper.mm index c3b8e3053..7817e182b 100644 --- a/packages/react-native-session-replay/ios/Sources/RCTFabricWrapper.mm +++ b/packages/react-native-session-replay/ios/Sources/RCTFabricWrapper.mm @@ -7,7 +7,6 @@ #import "RCTFabricWrapper.h" #if RCT_NEW_ARCH_ENABLED -#import "RCTVersion.h" #import "ParagraphProps.h" #if RCT_VERSION_MINOR >= 87 diff --git a/packages/react-native-session-replay/package.json b/packages/react-native-session-replay/package.json index 4ef31a938..f11e0fe6d 100644 --- a/packages/react-native-session-replay/package.json +++ b/packages/react-native-session-replay/package.json @@ -44,8 +44,7 @@ "test": "jest", "lint": "eslint .", "build:assets": "node scripts/build-assets.js", - "prepare": "rm -rf lib && yarn build:assets && yarn bob build", - "postinstall": "node scripts/set-ios-rn-version.js" + "prepare": "rm -rf lib && yarn build:assets && yarn bob build" }, "peerDependencies": { "react": ">=16.13.1", diff --git a/packages/react-native-session-replay/release-content.txt b/packages/react-native-session-replay/release-content.txt index d80c057d8..0fa2d16dd 100644 --- a/packages/react-native-session-replay/release-content.txt +++ b/packages/react-native-session-replay/release-content.txt @@ -87,7 +87,6 @@ package/ios/Sources/RCTTextExtractor.mm package/ios/Sources/RCTTextPropertiesWrapper.h package/ios/Sources/RCTTextPropertiesWrapper.mm package/ios/Sources/RCTTextViewRecorder.swift -package/ios/Sources/RCTVersion.h package/ios/Sources/SvgViewRecorder.swift package/ios/Sources/Utils/Bundle+SessionReplay.swift package/lib/commonjs/SessionReplay.js @@ -189,7 +188,6 @@ package/lib/typescript/types/DdPrivacyView.d.ts.map package/metro.js package/package.json package/scripts/build-assets.js -package/scripts/set-ios-rn-version.js package/src/SessionReplay.ts package/src/__tests__/SessionReplay.test.ts package/src/components/SessionReplayView/HideView.tsx diff --git a/packages/react-native-session-replay/scripts/set-ios-rn-version.js b/packages/react-native-session-replay/scripts/set-ios-rn-version.js deleted file mode 100644 index 83bd5d249..000000000 --- a/packages/react-native-session-replay/scripts/set-ios-rn-version.js +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. - * This product includes software developed at Datadog (https://www.datadoghq.com/). - * Copyright 2016-Present Datadog, Inc. - */ -/* eslint-disable @typescript-eslint/no-var-requires */ -const fs = require('fs'); -const path = require('path'); - -function getReactNativeVersion() { - try { - // eslint-disable-next-line global-require - return require('react-native/package.json').version; - } catch (error) { - throw new Error( - 'Failed to find React Native. Ensure it is installed in your project.' - ); - } -} - -const rnVersion = getReactNativeVersion(); - -const outputDir = path.resolve(__dirname, '../ios/Sources'); -const outputFile = path.join(outputDir, 'RCTVersion.h'); - -if (!fs.existsSync(outputDir)) { - fs.mkdirSync(outputDir, { recursive: true }); -} - -const [major, minor, patch] = rnVersion.split('.').map(Number); - -const headerContent = `#ifndef RCTVersion_h -#define RCTVersion_h - -#define RCT_VERSION_MAJOR ${major || 0} -#define RCT_VERSION_MINOR ${minor || 0} -#define RCT_VERSION_PATCH ${patch || 0} - -#endif /* RCTVersion_h */ -`; - -try { - fs.writeFileSync(outputFile, headerContent, 'utf8'); -} catch (error) { - console.error(`Failed to write RCTVersion.h: ${error.message}`); - process.exit(1); -}