From c143babf27f08bc2d82cc2cb632e6f537961901c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20G=C3=BCner?= Date: Fri, 29 Nov 2024 12:12:46 +0300 Subject: [PATCH] fix: use proper codegen types (#697) ### Summary We were using `type: "all"` in codegenConfig for all types of libraries. @cipolleschi recommended us to use proper types instead [in this thread](https://github.com/facebook/react-native/issues/46208#issuecomment-2491424662). This updates the templates to use the proper types ### Test plan #### Modules 1. Create a new arch supported module library 2. Go to the `package.json` and make sure `codegenConfig.type` is `"modules"` 3. Make sure the library builds and runs fine with the new architecture #### Components a.k.a views 1. Create a new arch supported view/component 2. Go to the `package.json` and make sure `codegenConfig.type` is `"components"` 3. 3. Make sure the library builds and runs fine with the new architecture --- .../templates/common/$package.json | 2 +- ....ts => patchCodegenAndroidPackage.test.ts} | 22 ++++++++++++++----- .../src/targets/codegen.ts | 15 +++++++++++-- ...degen.ts => patchCodegenAndroidPackage.ts} | 2 +- 4 files changed, 32 insertions(+), 9 deletions(-) rename packages/react-native-builder-bob/src/__tests__/{patchCodegen.test.ts => patchCodegenAndroidPackage.test.ts} (80%) rename packages/react-native-builder-bob/src/utils/{patchCodegen.ts => patchCodegenAndroidPackage.ts} (98%) diff --git a/packages/create-react-native-library/templates/common/$package.json b/packages/create-react-native-library/templates/common/$package.json index de577f1cd..99dd8931d 100644 --- a/packages/create-react-native-library/templates/common/$package.json +++ b/packages/create-react-native-library/templates/common/$package.json @@ -198,7 +198,7 @@ }, "codegenConfig": { "name": "RN<%- project.name -%><%- project.view ? 'View': '' -%>Spec", - "type": "all", + "type": "<%- project.view ? 'components': 'modules' -%>", "jsSrcsDir": "src", "outputDir": { "ios": "ios/generated", diff --git a/packages/react-native-builder-bob/src/__tests__/patchCodegen.test.ts b/packages/react-native-builder-bob/src/__tests__/patchCodegenAndroidPackage.test.ts similarity index 80% rename from packages/react-native-builder-bob/src/__tests__/patchCodegen.test.ts rename to packages/react-native-builder-bob/src/__tests__/patchCodegenAndroidPackage.test.ts index 90d971c6c..e455db2d4 100644 --- a/packages/react-native-builder-bob/src/__tests__/patchCodegen.test.ts +++ b/packages/react-native-builder-bob/src/__tests__/patchCodegenAndroidPackage.test.ts @@ -1,7 +1,7 @@ import { expect, it, describe, beforeEach, afterEach } from '@jest/globals'; import fs from 'fs-extra'; import path from 'node:path'; -import { patchCodegen } from '../utils/patchCodegen'; +import { patchCodegenAndroidPackage } from '../utils/patchCodegenAndroidPackage'; import mockfs from 'mock-fs'; import type { Report } from '../types'; @@ -44,7 +44,7 @@ const mockCodegenSpecsPath = path.resolve( 'android/generated/java/com/facebook/fbreact/specs' ); -describe('patchCodegen', () => { +describe('patchCodegenAndroidPackage', () => { beforeEach(() => { mockfs({ [mockProjectPath]: { @@ -61,7 +61,11 @@ describe('patchCodegen', () => { }); it('moves the files to correct dir', async () => { - await patchCodegen(mockProjectPath, mockPackageJson, mockReport); + await patchCodegenAndroidPackage( + mockProjectPath, + mockPackageJson, + mockReport + ); const expectedDir = path.resolve( mockProjectPath, @@ -72,7 +76,11 @@ describe('patchCodegen', () => { }); it('replaces the package name in the files', async () => { - await patchCodegen(mockProjectPath, mockPackageJson, mockReport); + await patchCodegenAndroidPackage( + mockProjectPath, + mockPackageJson, + mockReport + ); const expectedDir = path.resolve( mockProjectPath, @@ -87,7 +95,11 @@ describe('patchCodegen', () => { }); it('removes the old package dir', async () => { - await patchCodegen(mockProjectPath, mockPackageJson, mockReport); + await patchCodegenAndroidPackage( + mockProjectPath, + mockPackageJson, + mockReport + ); expect(await fs.pathExists(mockCodegenSpecsPath)).toBe(false); }); diff --git a/packages/react-native-builder-bob/src/targets/codegen.ts b/packages/react-native-builder-bob/src/targets/codegen.ts index 31d59a81b..e98defe8d 100644 --- a/packages/react-native-builder-bob/src/targets/codegen.ts +++ b/packages/react-native-builder-bob/src/targets/codegen.ts @@ -1,6 +1,6 @@ import kleur from 'kleur'; import type { Input } from '../types'; -import { patchCodegen } from '../utils/patchCodegen'; +import { patchCodegenAndroidPackage } from '../utils/patchCodegenAndroidPackage'; import fs from 'fs-extra'; import path from 'path'; import del from 'del'; @@ -32,10 +32,21 @@ export default async function build({ root, report }: Options) { await del([codegenAndroidPath]); } + const codegenType = packageJson.codegenConfig?.type; + + if (codegenType === undefined) { + report.error( + "Couldn't find the 'type' value in 'codegenConfig'. Please check your package.json's 'codegenConfig' property and make sure 'type' is defined. https://reactnative.dev/docs/the-new-architecture/using-codegen#configuring-codegen" + ); + process.exit(1); + } + try { await runRNCCli(['codegen']); - await patchCodegen(root, packageJson, report); + if (codegenType === 'modules' || codegenType === 'all') { + await patchCodegenAndroidPackage(root, packageJson, report); + } report.success('Generated native code with codegen'); } catch (e: unknown) { diff --git a/packages/react-native-builder-bob/src/utils/patchCodegen.ts b/packages/react-native-builder-bob/src/utils/patchCodegenAndroidPackage.ts similarity index 98% rename from packages/react-native-builder-bob/src/utils/patchCodegen.ts rename to packages/react-native-builder-bob/src/utils/patchCodegenAndroidPackage.ts index 64aab82dd..9a0acb462 100644 --- a/packages/react-native-builder-bob/src/utils/patchCodegen.ts +++ b/packages/react-native-builder-bob/src/utils/patchCodegenAndroidPackage.ts @@ -13,7 +13,7 @@ const CODEGEN_DOCS = * @throws if codegenConfig.outputDir.android or codegenConfig.android.javaPackageName is not defined in package.json * @throws if the codegenAndroidPath does not exist */ -export async function patchCodegen( +export async function patchCodegenAndroidPackage( projectPath: string, // eslint-disable-next-line @typescript-eslint/no-explicit-any packageJson: any,