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);