From f8cf068ee2422b1a8b3edcfc89926723885598ac Mon Sep 17 00:00:00 2001 From: Dmitry Rykun Date: Mon, 27 Nov 2023 07:14:49 -0800 Subject: [PATCH] Introduce "codegenConfig.isLibrary" property (#41655) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/41655 This diff adds support for checked-in codegen artifacts for libraries. It introduces a new property to `coegenConfig`, called `isLibrary`. If codegen sees `isLibrary: true` in a project's dependency, it assumes that the library has codegen artifacts in it, and will not generate any code. Changelog: [General][Added] - Introduce "codegenConfig.isLibrary" property. Differential Revision: D51207265 fbshipit-source-id: 938cf998331a7841731dfcf24e2a3e6ed8bcf8dc --- .../codegen/generate-artifacts-executor.js | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor.js b/packages/react-native/scripts/codegen/generate-artifacts-executor.js index 088f7a1858f435..36ac944ae33b44 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor.js @@ -241,6 +241,16 @@ function needsThirdPartyComponentProvider(schemaInfo) { return !isReactNativeCoreLibrary(schemaInfo.library.config.name); } +function mustGenerateNativeCode(includeLibraryPath, schemaInfo) { + // If library's 'codegenConfig' sets 'isLibrary' to 'true', + // then we assume that native code is shipped with the library, + // and we don't need to generate it. + return ( + schemaInfo.library.libraryPath === includeLibraryPath || + !schemaInfo.library.config.isLibrary + ); +} + function createComponentProvider(schemas) { console.log('\n\n>>>>> Creating component provider'); const outputDir = path.join( @@ -262,10 +272,12 @@ function createComponentProvider(schemas) { } function findCodegenEnabledLibraries(pkgJson, projectRoot) { - return [ - ...findExternalLibraries(pkgJson), - ...findProjectRootLibraries(pkgJson, projectRoot), - ]; + const projectLibraries = findProjectRootLibraries(pkgJson, projectRoot); + if (pkgJson.codegenConfig.isLibrary) { + return projectLibraries; + } else { + return [...projectLibraries, ...findExternalLibraries(pkgJson)]; + } } // It removes all the empty files and empty folders @@ -343,12 +355,19 @@ function execute(projectRoot, outputPath) { const iosOutputDir = computeIOSOutputDir(outputPath); const schemaInfos = generateSchemaInfos(libraries); - generateNativeCode(iosOutputDir, schemaInfos); + generateNativeCode( + iosOutputDir, + schemaInfos.filter(schemaInfo => + mustGenerateNativeCode(projectRoot, schemaInfo), + ), + ); - const schemas = schemaInfos - .filter(needsThirdPartyComponentProvider) - .map(schemaInfo => schemaInfo.schema); - createComponentProvider(schemas); + if (!pkgJson.codegenConfig.isLibrary) { + const schemas = schemaInfos + .filter(needsThirdPartyComponentProvider) + .map(schemaInfo => schemaInfo.schema); + createComponentProvider(schemas); + } cleanupEmptyFilesAndFolders(iosOutputDir); } catch (err) { console.error(err);