0
# Code Generation
1
2
Core code generation functionality that takes schemas and produces native platform code for React Native components and TurboModules.
3
4
## Capabilities
5
6
### Main Generator Function
7
8
Generates native code from a schema for specified platforms and outputs.
9
10
```javascript { .api }
11
/**
12
* Generate native code from a React Native schema
13
* @param options - Configuration options for code generation
14
* @param config - Generator configuration specifying what to generate
15
* @returns true if generation succeeded, false otherwise
16
*/
17
function generate(
18
options: LibraryOptions,
19
config: LibraryConfig
20
): boolean;
21
22
interface LibraryOptions {
23
/** Name of the library being generated */
24
libraryName: string;
25
/** Schema describing the components and modules */
26
schema: SchemaType;
27
/** Directory where generated files will be written */
28
outputDirectory: string;
29
/** Package name for platform-specific code (e.g., com.example.mylibrary) */
30
packageName?: string;
31
/** Whether to assume nonnull annotations */
32
assumeNonnull: boolean;
33
/** Whether to use local include paths instead of full paths */
34
useLocalIncludePaths?: boolean;
35
/** Custom generator functions (uses built-in generators if not provided) */
36
libraryGenerators?: LibraryGeneratorsFunctions;
37
}
38
39
interface LibraryConfig {
40
/** Array of generator types to run */
41
generators: Array<LibraryGenerators>;
42
/** If true, check files for changes instead of writing them */
43
test?: boolean;
44
}
45
```
46
47
**Usage Example:**
48
49
```javascript
50
const RNCodegen = require('@react-native/codegen/lib/generators/RNCodegen');
51
const fs = require('fs');
52
53
const schema = JSON.parse(fs.readFileSync('MyComponent.json', 'utf-8'));
54
55
const success = RNCodegen.generate(
56
{
57
libraryName: 'MyLibrary',
58
schema: schema,
59
outputDirectory: './ios/Generated',
60
packageName: 'com.example.mylibrary',
61
assumeNonnull: false,
62
},
63
{
64
generators: ['componentsIOS', 'modulesIOS'],
65
test: false,
66
}
67
);
68
```
69
70
### Multi-Schema Generation
71
72
Generate code from multiple schemas at once, useful for combining multiple libraries.
73
74
```javascript { .api }
75
/**
76
* Generate code from multiple schemas
77
* @param options - Configuration for multi-schema generation
78
* @param config - Generator configuration
79
* @returns true if generation succeeded, false otherwise
80
*/
81
function generateFromSchemas(
82
options: SchemasOptions,
83
config: SchemasConfig
84
): boolean;
85
86
interface SchemasOptions {
87
/** Map of library name to schema */
88
schemas: { [libraryName: string]: SchemaType };
89
/** Directory where generated files will be written */
90
outputDirectory: string;
91
/** Platform support configuration for Apple platforms */
92
supportedApplePlatforms?: { [platform: string]: { [key: string]: boolean } };
93
}
94
95
interface SchemasConfig {
96
/** Array of schema generator types to run */
97
generators: Array<SchemasGenerators>;
98
/** If true, check files for changes instead of writing them */
99
test?: boolean;
100
}
101
102
type SchemasGenerators = 'providerIOS';
103
```
104
105
### View Configuration Generation
106
107
Generate JavaScript view configuration for React Native components.
108
109
```javascript { .api }
110
/**
111
* Generate view configuration JavaScript for a component
112
* @param options - Library name and schema
113
* @returns Generated view configuration as a string
114
*/
115
function generateViewConfig(options: {
116
libraryName: string;
117
schema: SchemaType;
118
}): string;
119
```
120
121
**Usage Example:**
122
123
```javascript
124
const RNCodegen = require('@react-native/codegen/lib/generators/RNCodegen');
125
126
const viewConfig = RNCodegen.generateViewConfig({
127
libraryName: 'MyComponent',
128
schema: componentSchema,
129
});
130
131
console.log(viewConfig); // JavaScript view configuration code
132
```
133
134
### Available Generators
135
136
Generator types available for the `generators` array:
137
138
```javascript { .api }
139
type LibraryGenerators =
140
| 'componentsAndroid' // Android component code (C++/Java)
141
| 'componentsIOS' // iOS component code (Objective-C++/C++)
142
| 'descriptors' // Component descriptor files
143
| 'events' // Event emitter code
144
| 'props' // Props interface code
145
| 'states' // Component state code
146
| 'tests' // Test code generation
147
| 'shadow-nodes' // Shadow node implementations
148
| 'modulesAndroid' // Android TurboModule code (JNI/Java)
149
| 'modulesCxx' // C++ TurboModule code
150
| 'modulesIOS'; // iOS TurboModule code (Objective-C++)
151
```
152
153
### Access to Individual Generators
154
155
Access individual generator functions for custom workflows.
156
157
```javascript { .api }
158
/** Object containing all available generator functions */
159
const allGenerators: {
160
[generatorName: string]: GenerateFunction;
161
};
162
163
type GenerateFunction = (
164
libraryName: string,
165
schema: SchemaType,
166
packageName?: string,
167
assumeNonnull: boolean,
168
headerPrefix?: string
169
) => FilesOutput;
170
171
type FilesOutput = Map<string, string>;
172
```
173
174
**Usage Example:**
175
176
```javascript
177
const RNCodegen = require('@react-native/codegen/lib/generators/RNCodegen');
178
179
// Use a specific generator directly
180
const files = RNCodegen.allGenerators.generateComponentDescriptorH(
181
'MyLibrary',
182
schema,
183
'com.example.mylibrary',
184
false,
185
'react/renderer/components/MyLibrary/'
186
);
187
188
// files is a Map<string, string> of filename -> file content
189
files.forEach((content, filename) => {
190
console.log(`Generated ${filename}`);
191
});
192
```
193
194
## Generator Output Structure
195
196
Generated files are organized by platform and type:
197
198
- **iOS Component Files**: C++ headers (.h), C++ implementation (.cpp), Objective-C++ (.mm)
199
- **Android Component Files**: C++ headers (.h), C++ implementation (.cpp), Java interfaces (.java)
200
- **Module Files**: Platform-specific TurboModule implementations
201
- **Descriptor Files**: Component registration and metadata
202
- **Test Files**: Generated test cases for components and modules
203
204
The exact file structure depends on the generator types specified and the schema content.