Skip to content

Commit c143bab

Browse files
authored
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](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
1 parent 09fc806 commit c143bab

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

packages/create-react-native-library/templates/common/$package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
},
199199
"codegenConfig": {
200200
"name": "RN<%- project.name -%><%- project.view ? 'View': '' -%>Spec",
201-
"type": "all",
201+
"type": "<%- project.view ? 'components': 'modules' -%>",
202202
"jsSrcsDir": "src",
203203
"outputDir": {
204204
"ios": "ios/generated",

packages/react-native-builder-bob/src/__tests__/patchCodegen.test.ts renamed to packages/react-native-builder-bob/src/__tests__/patchCodegenAndroidPackage.test.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect, it, describe, beforeEach, afterEach } from '@jest/globals';
22
import fs from 'fs-extra';
33
import path from 'node:path';
4-
import { patchCodegen } from '../utils/patchCodegen';
4+
import { patchCodegenAndroidPackage } from '../utils/patchCodegenAndroidPackage';
55
import mockfs from 'mock-fs';
66
import type { Report } from '../types';
77

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

47-
describe('patchCodegen', () => {
47+
describe('patchCodegenAndroidPackage', () => {
4848
beforeEach(() => {
4949
mockfs({
5050
[mockProjectPath]: {
@@ -61,7 +61,11 @@ describe('patchCodegen', () => {
6161
});
6262

6363
it('moves the files to correct dir', async () => {
64-
await patchCodegen(mockProjectPath, mockPackageJson, mockReport);
64+
await patchCodegenAndroidPackage(
65+
mockProjectPath,
66+
mockPackageJson,
67+
mockReport
68+
);
6569

6670
const expectedDir = path.resolve(
6771
mockProjectPath,
@@ -72,7 +76,11 @@ describe('patchCodegen', () => {
7276
});
7377

7478
it('replaces the package name in the files', async () => {
75-
await patchCodegen(mockProjectPath, mockPackageJson, mockReport);
79+
await patchCodegenAndroidPackage(
80+
mockProjectPath,
81+
mockPackageJson,
82+
mockReport
83+
);
7684

7785
const expectedDir = path.resolve(
7886
mockProjectPath,
@@ -87,7 +95,11 @@ describe('patchCodegen', () => {
8795
});
8896

8997
it('removes the old package dir', async () => {
90-
await patchCodegen(mockProjectPath, mockPackageJson, mockReport);
98+
await patchCodegenAndroidPackage(
99+
mockProjectPath,
100+
mockPackageJson,
101+
mockReport
102+
);
91103

92104
expect(await fs.pathExists(mockCodegenSpecsPath)).toBe(false);
93105
});

packages/react-native-builder-bob/src/targets/codegen.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import kleur from 'kleur';
22
import type { Input } from '../types';
3-
import { patchCodegen } from '../utils/patchCodegen';
3+
import { patchCodegenAndroidPackage } from '../utils/patchCodegenAndroidPackage';
44
import fs from 'fs-extra';
55
import path from 'path';
66
import del from 'del';
@@ -32,10 +32,21 @@ export default async function build({ root, report }: Options) {
3232
await del([codegenAndroidPath]);
3333
}
3434

35+
const codegenType = packageJson.codegenConfig?.type;
36+
37+
if (codegenType === undefined) {
38+
report.error(
39+
"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"
40+
);
41+
process.exit(1);
42+
}
43+
3544
try {
3645
await runRNCCli(['codegen']);
3746

38-
await patchCodegen(root, packageJson, report);
47+
if (codegenType === 'modules' || codegenType === 'all') {
48+
await patchCodegenAndroidPackage(root, packageJson, report);
49+
}
3950

4051
report.success('Generated native code with codegen');
4152
} catch (e: unknown) {

packages/react-native-builder-bob/src/utils/patchCodegen.ts renamed to packages/react-native-builder-bob/src/utils/patchCodegenAndroidPackage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const CODEGEN_DOCS =
1313
* @throws if codegenConfig.outputDir.android or codegenConfig.android.javaPackageName is not defined in package.json
1414
* @throws if the codegenAndroidPath does not exist
1515
*/
16-
export async function patchCodegen(
16+
export async function patchCodegenAndroidPackage(
1717
projectPath: string,
1818
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1919
packageJson: any,

0 commit comments

Comments
 (0)