Skip to content

Commit 045db60

Browse files
committed
Use autolinking react-native-config output in iOS artifacts generator
1 parent d2912e7 commit 045db60

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

packages/react-native/scripts/cocoapods/autolinking.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def list_native_modules!(config_command)
4040
packages = config["dependencies"]
4141
ios_project_root = Pathname.new(config["project"]["ios"]["sourceDir"])
4242
react_native_path = Pathname.new(config["reactNativePath"])
43+
codegen_output_path = ios_project_root.join("build/generated/autolinking/autolinking.json")
44+
45+
# Write autolinking react-native-config output to codegen folder
46+
FileUtils.mkdir_p(File.dirname(codegen_output_path))
47+
File.write(codegen_output_path, json)
48+
4349
found_pods = []
4450

4551
packages.each do |package_name, package|

packages/react-native/scripts/cocoapods/codegen_utils.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def self.clean_up_build_folder(rn_path, codegen_dir, dir_manager: Dir, file_mana
8787
codegen_path = file_manager.join(ios_folder, codegen_dir)
8888
return if !dir_manager.exist?(codegen_path)
8989

90-
FileUtils.rm_rf(dir_manager.glob("#{codegen_path}/*"))
90+
FileUtils.rm_rf("#{codegen_path}")
9191
base_provider_path = file_manager.join(rn_path, 'React', 'Fabric', 'RCTThirdPartyFabricComponentsProvider')
9292
FileUtils.rm_rf("#{base_provider_path}.h")
9393
FileUtils.rm_rf("#{base_provider_path}.mm")

packages/react-native/scripts/codegen/generate-artifacts-executor/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,14 @@ function execute(
8686
buildCodegenIfNeeded();
8787
}
8888

89-
const reactNativeConfig = readReactNativeConfig(projectRoot);
89+
const reactNativeConfig = readReactNativeConfig(
90+
projectRoot,
91+
baseOutputPath,
92+
);
9093
const codegenEnabledLibraries = findCodegenEnabledLibraries(
9194
pkgJson,
9295
projectRoot,
96+
baseOutputPath,
9397
reactNativeConfig,
9498
);
9599

packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,40 @@ function cleanupEmptyFilesAndFolders(filepath /*: string */) {
9797
}
9898
}
9999

100-
function readReactNativeConfig(projectRoot /*: string */) /*: $FlowFixMe */ {
101-
const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js');
100+
function readGeneratedReactNativeConfig(
101+
baseOutputPath /*: string */,
102+
) /*: $FlowFixMe */ {
103+
// NOTE: Generated by scripts/cocoapods/autolinking.rb in list_native_modules (called by use_native_modules)
104+
const rnConfigGeneratedPath = path.resolve(
105+
baseOutputPath,
106+
'build/generated/autolinking/autolinking.json',
107+
);
108+
if (fs.existsSync(rnConfigGeneratedPath)) {
109+
/* $FlowFixMe */
110+
return require(rnConfigGeneratedPath);
111+
} else {
112+
console.warn(
113+
`Could not find generated React Native config output at: ${rnConfigGeneratedPath}`,
114+
);
115+
return null;
116+
}
117+
}
102118

103-
if (!fs.existsSync(rnConfigFilePath)) {
119+
function readReactNativeConfig(
120+
projectRoot /*: string */,
121+
baseOutputPath /*: string */,
122+
) /*: $FlowFixMe */ {
123+
const rnGeneratedConfig = readGeneratedReactNativeConfig(baseOutputPath);
124+
const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js');
125+
if (rnGeneratedConfig) {
126+
return rnGeneratedConfig;
127+
} else if (fs.existsSync(rnConfigFilePath)) {
128+
/* $FlowFixMe */
129+
return require(rnConfigFilePath);
130+
} else {
131+
console.warn(`Could not find React Native config at: ${rnConfigFilePath}`);
104132
return {};
105133
}
106-
107-
// $FlowFixMe[unsupported-syntax]
108-
return require(rnConfigFilePath);
109134
}
110135

111136
/**
@@ -114,8 +139,14 @@ function readReactNativeConfig(projectRoot /*: string */) /*: $FlowFixMe */ {
114139
function findCodegenEnabledLibraries(
115140
pkgJson /*: $FlowFixMe */,
116141
projectRoot /*: string */,
142+
baseOutputPath /*: string */,
117143
reactNativeConfig /*: $FlowFixMe */,
118144
) /*: Array<$FlowFixMe> */ {
145+
if (!!readGeneratedReactNativeConfig(baseOutputPath)) {
146+
// If we ran autolinking, we shouldn't try to run our own "autolinking-like"
147+
// library discovery
148+
return findLibrariesFromReactNativeConfig(projectRoot, reactNativeConfig);
149+
}
119150
const projectLibraries = findProjectRootLibraries(pkgJson, projectRoot);
120151
if (pkgJsonIncludesGeneratedCode(pkgJson)) {
121152
return projectLibraries;

0 commit comments

Comments
 (0)