From 3cedb09a650adda0b3f24e931c25f27730af19b1 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Wed, 4 Dec 2024 17:25:17 +0000 Subject: [PATCH] [Codegen] Exclude unlinked libs from codegen (#47712) --- .../codegen/generate-artifacts-executor.js | 56 ++++++++++++++++--- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor.js b/packages/react-native/scripts/codegen/generate-artifacts-executor.js index df2d16a57a3a86..486cb66031c604 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor.js @@ -152,6 +152,7 @@ function extractLibrariesFromJSON(configFile, dependencyPath) { const config = configFile.codegenConfig; return [ { + libraryName: configFile.name, config, libraryPath: dependencyPath, }, @@ -251,19 +252,23 @@ function findExternalLibraries(pkgJson, projectRoot) { }); } -function findLibrariesFromReactNativeConfig(projectRoot) { +function readRNConfigJSFile(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); + return require(rnConfigFilePath); +} + +function findLibrariesFromReactNativeConfig(projectRoot) { + console.log( + `\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in react-native.config.js`, + ); + + const rnConfig = readRNConfigJSFile(projectRoot); if (rnConfig.dependencies == null) { return []; @@ -289,6 +294,33 @@ function findLibrariesFromReactNativeConfig(projectRoot) { }); } +// Function to look for libraries explicitly unlinked from the app +// through the react-native.config.js file. +// If this happens, it might be that the app does not need +// to generate code for that library as it won't be used by that platform +// @return { [libraryName: string]: [platform: string] } +function findNotLinkedLibraries(projectRoot) { + const rnConfig = readRNConfigJSFile(projectRoot); + + if (rnConfig.dependencies == null) { + return {}; + } + + let notLinkedLibraries = {}; + + Object.keys(rnConfig.dependencies).forEach(name => { + const dependency = rnConfig.dependencies[name]; + let notLinkedPlatforms = []; + Object.keys(dependency.platforms).forEach(platform => { + if (dependency.platforms[platform] == null) { + notLinkedPlatforms.push(platform); + } + }); + notLinkedLibraries[name] = notLinkedPlatforms + }); + return notLinkedLibraries; +} + function findProjectRootLibraries(pkgJson, projectRoot) { console.log('[Codegen] Searching for codegen-enabled libraries in the app.'); @@ -694,6 +726,8 @@ function execute(projectRoot, targetPlatform, baseOutputPath) { let platforms = targetPlatform === 'all' ? supportedPlatforms : [targetPlatform]; + const notLinkedLibraries = findNotLinkedLibraries(projectRoot); + for (const platform of platforms) { const outputPath = computeOutputPath( projectRoot, @@ -702,7 +736,15 @@ function execute(projectRoot, targetPlatform, baseOutputPath) { platform, ); - const schemaInfos = generateSchemaInfos(libraries); + const schemaInfos = generateSchemaInfos(libraries.filter(library => { + const unlinkedPlatforms = notLinkedLibraries[library.libraryName]; + if (unlinkedPlatforms && unlinkedPlatforms.includes(platform)) { + console.log(`[Codegen - ${library.libraryName}] Skipping Codegen on ${platform}`); + return false; + } + return true; + })); + generateNativeCode( outputPath, schemaInfos.filter(schemaInfo =>