Skip to content

Commit

Permalink
fix: use proper codegen types (#697)
Browse files Browse the repository at this point in the history
### Summary

We were using `type: "all"` in codegenConfig for all types of libraries.
@cipolleschi recommended us to use proper types instead [in this
thread](facebook/react-native#46208 (comment)).
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
  • Loading branch information
atlj authored Nov 29, 2024
1 parent 09fc806 commit c143bab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -44,7 +44,7 @@ const mockCodegenSpecsPath = path.resolve(
'android/generated/java/com/facebook/fbreact/specs'
);

describe('patchCodegen', () => {
describe('patchCodegenAndroidPackage', () => {
beforeEach(() => {
mockfs({
[mockProjectPath]: {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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);
});
Expand Down
15 changes: 13 additions & 2 deletions packages/react-native-builder-bob/src/targets/codegen.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c143bab

Please sign in to comment.