Skip to content

Commit

Permalink
iOS Codegen: bring back support for defining external libraries in re…
Browse files Browse the repository at this point in the history
…act-native.config.js (facebook#42771)

Summary:

This feature was first introduced here facebook#34580
And then removed here facebook#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
  • Loading branch information
dmytrorykun authored and facebook-github-bot committed Feb 1, 2024
1 parent 7b36233 commit ec8a5cb
Showing 1 changed file with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.');

Expand Down Expand Up @@ -447,7 +489,11 @@ function findCodegenEnabledLibraries(pkgJson, projectRoot) {
if (pkgJsonIncludesGeneratedCode(pkgJson)) {
return projectLibraries;
} else {
return [...projectLibraries, ...findExternalLibraries(pkgJson)];
return [
...projectLibraries,
...findExternalLibraries(pkgJson),
...findLibrariesFromReactNativeConfig(projectRoot),
];
}
}

Expand Down

0 comments on commit ec8a5cb

Please sign in to comment.