From ec8a5cb91afa08aa84765dac31425e15b40f9d31 Mon Sep 17 00:00:00 2001 From: Dmitry Rykun Date: Thu, 1 Feb 2024 02:06:56 -0800 Subject: [PATCH] iOS Codegen: bring back support for defining external libraries in react-native.config.js (#42771) Summary: 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 --- .../codegen/generate-artifacts-executor.js | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor.js b/packages/react-native/scripts/codegen/generate-artifacts-executor.js index cf0f594affc7a3..fc98288592f833 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor.js @@ -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), + ]; } }