Skip to content

fix: pass source flag to codegen cli when possible #815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/create-react-native-library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { createInitialGitCommit } from './utils/initialCommit';
import { prompt } from './utils/prompt';
import { resolveNpmPackageVersion } from './utils/resolveNpmPackageVersion';

const FALLBACK_BOB_VERSION = '0.40.4';
const FALLBACK_BOB_VERSION = '0.40.5';
const FALLBACK_NITRO_MODULES_VERSION = '0.22.1';
const SUPPORTED_REACT_NATIVE_VERSION = '0.78.2';

Expand Down
18 changes: 15 additions & 3 deletions packages/react-native-builder-bob/src/targets/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { patchCodegenAndroidPackage } from './patches/patchCodegenAndroidPackage
import fs from 'fs-extra';
import path from 'path';
import del from 'del';
import { removeCodegenAppLevelCode } from './patches/removeCodegenAppLevelCode';
import {
getCodegenCLISourceSupport,
removeCodegenAppLevelCode,
} from './patches/removeCodegenAppLevelCode';
import { spawn } from '../../utils/spawn';

type Options = Omit<Input, 'output'>;
Expand Down Expand Up @@ -42,12 +45,21 @@ export default async function build({ root, report }: Options) {
}

try {
await spawn('npx', ['@react-native-community/cli', 'codegen']);
const codegenCLISupportsSource = await getCodegenCLISourceSupport();

await spawn('npx', [
'@react-native-community/cli',
'codegen',
...(codegenCLISupportsSource ? ['--source', 'library'] : []),
]);

if (codegenType === 'modules' || codegenType === 'all') {
await patchCodegenAndroidPackage(root, packageJson, report);
}
await removeCodegenAppLevelCode(root, packageJson);

if (!codegenCLISupportsSource) {
await removeCodegenAppLevelCode(root, packageJson);
}

report.success('Generated native code with codegen');
} catch (e: unknown) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs-extra';
import path from 'path';
import { CODEGEN_DOCS } from './patchCodegenAndroidPackage';
import { spawn } from '../../../utils/spawn';

const FILES_TO_REMOVE = [
'RCTAppDependencyProvider.h',
Expand Down Expand Up @@ -68,3 +69,18 @@ export async function removeCodegenAppLevelCode(

await Promise.allSettled([...androidPromises, ...iosPromises]);
}

/**
* Codegen generates a different set of files if the target is an app instead.
* The following commit adds support for a --source argument to support calling codegen as a library:
* https://github.com/facebook/react-native/commit/98b8f178110472e5fed97de80766c03b0b5e988c
* Here we just check if the --source argument is supported.
*/
export async function getCodegenCLISourceSupport(): Promise<boolean> {
const codegenCLIHelpOutput = await spawn('npx', [
'@react-native-community/cli',
'codegen',
'--help',
]);
return codegenCLIHelpOutput.includes('--source');
}
Loading