From 4e5da2ea1dbbab1b81cf303eb27dde434cb71e0a Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Tue, 28 Jan 2025 05:06:54 -0800 Subject: [PATCH] Add extra parameter to define whether codegen is invoked by lib or app (#48995) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48995 This change adds an extra parameter to the codegen script that allow our users to trigger codegen for Apps or for Libraries. When running codegen for Apps, we have to generate some extra files that are not needed by the Libraries. This is causing issues to our library maintainers and this change will provide more flexibility in the DevX of libraries. The default value is App, so if the new parameter is not passed, nothing will change in the current behavior. [iOS][Added] - Add the `source` parameter to generate-codegen-artifacts to avoid generating files not needed by libraries. Reviewed By: cortinico Differential Revision: D68765478 fbshipit-source-id: 8030b4472ad4f5058e58b1c91089de5122a4f60a --- packages/react-native/react-native.config.js | 6 ++++++ .../scripts/codegen/generate-artifacts-executor.js | 12 ++++++++---- .../scripts/generate-codegen-artifacts.js | 7 ++++++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/react-native/react-native.config.js b/packages/react-native/react-native.config.js index 60fc2b395ae..97750587c8d 100644 --- a/packages/react-native/react-native.config.js +++ b/packages/react-native/react-native.config.js @@ -68,12 +68,18 @@ const codegenCommand = { name: '--outputPath ', description: 'Path where generated artifacts will be output to.', }, + { + name: '--source ', + description: 'Whether the script is invoked from an `app` or a `library`', + default: 'app', + }, ], func: (argv, config, args) => require('./scripts/codegen/generate-artifacts-executor').execute( args.path, args.platform, args.outputPath, + args.source, ), }; diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor.js b/packages/react-native/scripts/codegen/generate-artifacts-executor.js index 480bc8c5cce..6b4e7b730a9 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor.js @@ -899,11 +899,12 @@ function generateFBReactNativeSpecIOS(projectRoot /*: string */) /*: void*/ { * @parameter projectRoot: the directory with the app source code, where the package.json lives. * @parameter baseOutputPath: the base output path for the CodeGen. * @parameter targetPlatform: the target platform. Supported values: 'android', 'ios', 'all'. + * @parameter source: the source that is invoking codegen. Supported values: 'app', 'library'. * @throws If it can't find a config file for react-native. * @throws If it can't find a CodeGen configuration in the file. * @throws If it can't find a cli for the CodeGen. */ -function execute(projectRoot, targetPlatform, baseOutputPath) { +function execute(projectRoot, targetPlatform, baseOutputPath, source) { try { codegenLog(`Analyzing ${path.join(projectRoot, 'package.json')}`); @@ -951,9 +952,12 @@ function execute(projectRoot, targetPlatform, baseOutputPath) { platform, ); - generateRCTThirdPartyComponents(libraries, outputPath); - generateCustomURLHandlers(libraries, outputPath); - generateAppDependencyProvider(outputPath); + if (source === 'app') { + // These components are only required by apps, not by libraries + generateRCTThirdPartyComponents(libraries, outputPath); + generateCustomURLHandlers(libraries, outputPath); + generateAppDependencyProvider(outputPath); + } cleanupEmptyFilesAndFolders(outputPath); } diff --git a/packages/react-native/scripts/generate-codegen-artifacts.js b/packages/react-native/scripts/generate-codegen-artifacts.js index a8d4fa2db9c..0f0ecdda7b4 100644 --- a/packages/react-native/scripts/generate-codegen-artifacts.js +++ b/packages/react-native/scripts/generate-codegen-artifacts.js @@ -25,7 +25,12 @@ const argv = yargs alias: 'outputPath', description: 'Path where generated artifacts will be output to.', }) + .option('s', { + alias: 'source', + description: 'Whether the script is invoked from an `app` or a `library`', + default: 'app', + }) .usage('Usage: $0 -p [path to app] -t [target platform] -o [output path]') .demandOption(['p', 't']).argv; -executor.execute(argv.path, argv.targetPlatform, argv.outputPath); +executor.execute(argv.path, argv.targetPlatform, argv.outputPath, argv.source);