iOS Codegen: bring back support for defining external libraries in react-native.config.js (#42771)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/42771

This feature was first introduced here https://github.com/facebook/react-native/pull/34580
And then removed here https://github.com/facebook/react-native/pull/41654
The motivation for its removing was that Node resolver should handle all those cases for which `react-native.config.js` was used. But it turns out that it fails for the setup that `react-native-builder-bob` has.
This diff brings back support for defining external libraries in `react-native.config.js`.

Changelog: [iOS][Fixed] - Bring back support for defining external libraries in react-native.config.js

Reviewed By: cipolleschi

Differential Revision: D53267857

fbshipit-source-id: 7625dfe7b4a4651eb60eaec725f94f222a244e30
This commit is contained in:
Dmitry Rykun
2024-02-01 02:53:48 -08:00
committed by Facebook GitHub Bot
parent 7b36233ae1
commit c6c23042a4
@@ -227,6 +227,48 @@ function findExternalLibraries(pkgJson) {
});
}
function findLibrariesFromReactNativeConfig(projectRoot) {
const rnConfigFileName = 'react-native.config.js';
console.log(
`\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in ${rnConfigFileName}`,
);
const rnConfigFilePath = path.resolve(projectRoot, rnConfigFileName);
if (!fs.existsSync(rnConfigFilePath)) {
return [];
}
const rnConfig = require(rnConfigFilePath);
if (rnConfig.dependencies == null) {
return [];
}
return Object.keys(rnConfig.dependencies).flatMap(name => {
const dependencyConfig = rnConfig.dependencies[name];
if (!dependencyConfig.root) {
return [];
}
const codegenConfigFileDir = path.resolve(
projectRoot,
dependencyConfig.root,
);
let configFile;
try {
configFile = readPkgJsonInDirectory(codegenConfigFileDir);
} catch {
return [];
}
return extractLibrariesFromJSON(
configFile,
configFile.name,
codegenConfigFileDir,
);
});
}
function findProjectRootLibraries(pkgJson, projectRoot) {
console.log('[Codegen] Searching for codegen-enabled libraries in the app.');
@@ -447,7 +489,11 @@ function findCodegenEnabledLibraries(pkgJson, projectRoot) {
if (pkgJsonIncludesGeneratedCode(pkgJson)) {
return projectLibraries;
} else {
return [...projectLibraries, ...findExternalLibraries(pkgJson)];
return [
...projectLibraries,
...findExternalLibraries(pkgJson),
...findLibrariesFromReactNativeConfig(projectRoot),
];
}
}