You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This will ask you a few questions and add the required configuration and scripts for building the code. The code will be compiled automatically when the package is published.
26
26
27
-
> Note: the `init` command doesn't add the [`codegen` target](#codegen) yet. You can either add it manually or create a new library with `create-react-native-library`.
28
-
29
27
You can find details on what exactly it adds in the [Manual configuration](#manual-configuration) section.
30
28
31
29
## Manual configuration
@@ -46,9 +44,7 @@ To configure your project manually, follow these steps:
46
44
"output": "lib",
47
45
"targets": [
48
46
["module", { "esm": true }],
49
-
["commonjs", { "esm": true }],
50
47
"typescript",
51
-
"codegen"
52
48
]
53
49
}
54
50
```
@@ -120,18 +116,6 @@ To configure your project manually, follow these steps:
120
116
121
117
This makes sure that Jest doesn't try to run the tests in the generated files.
If your library supports the [New React Native Architecture](https://reactnative.dev/architecture/landing-page), you should also configure Codegen. This is not required for libraries that only support the old architecture.
126
-
127
-
You can follow the [Official Codegen Setup Guide](https://reactnative.dev/docs/the-new-architecture/using-codegen) to enable Codegen.
128
-
129
-
It's also recommended to ship your Codegen generated scaffold code with your library since it has numerous benefits. To see the benefits and implement this behavior, you can see the [Official Codegen Shipping Guide](https://reactnative.dev/docs/the-new-architecture/codegen-cli#including-generated-code-into-libraries).
130
-
131
-
See [How to opt-out of shipping the Codegen generated code](./faq.md#how-to-opt-out-of-shipping-codegen-generated-scaffold-code) if you don't want to ship the Codegen generated scaffold code.
132
-
133
-
> Note: If you enable Codegen generated code shipping, React Native won't build the scaffold code automatically when you build your test app. You need to rebuild the codegen scaffold code manually each time you make changes to your spec. If you want to automate this process, you can create a new project with `create-react-native-library` and inspect the example app.
134
-
135
119
And we're done 🎉
136
120
137
121
## Options
@@ -281,9 +265,120 @@ If you need to support legacy setups that use `moduleResolution: node10` or `mod
281
265
282
266
#### `codegen`
283
267
284
-
Enable generating the [React Native Codegen](https://reactnative.dev/docs/the-new-architecture/what-is-codegen) scaffold code, which is used with the New React Native Architecture.
268
+
Enable generating the [React Native Codegen](https://reactnative.dev/docs/the-new-architecture/what-is-codegen) scaffold code when building the library.
269
+
270
+
If you use this `target`, you'll also want to use `"includesGeneratedCode": true` to ship the generated code with your library. Before you do so, make sure to [read the official docs](https://reactnative.dev/docs/the-new-architecture/codegen-cli#including-generated-code-into-libraries) to understand the advantages and tradeoffs of this approach.
271
+
272
+
If you want to ship codegen generated code with your library, you can do the following steps to integrate it with the library's workflow:
273
+
274
+
1. Add the `codegen` target to the `react-native-builder-bob` field in your `package.json` or `bob.config.js`:
275
+
276
+
```diff
277
+
"source": "src",
278
+
"output": "lib",
279
+
"targets": [
280
+
// …
281
+
+ "codegen"
282
+
]
283
+
```
284
+
285
+
This will enable the codegen script to run when you publish the library (if `bob build` is configured to be run on publish).
286
+
287
+
2. Add `@react-native-community/cli` as a `devDependency` in your `package.json`:
288
+
289
+
```diff
290
+
"devDependencies": {
291
+
// …
292
+
+ "@react-native-community/cli": "^x.x.x"
293
+
}
294
+
```
295
+
296
+
For the `@react-native-community/cli` version, refer to the `example/package.json` file. The version should be the same as the one used in the `example` app.
297
+
298
+
3. Add `"includesGeneratedCode": true` and `"outputDir"` to the `codegenConfig` field in your `package.json`:
299
+
300
+
```diff
301
+
"codegenConfig": {
302
+
// …
303
+
+ "outputDir": {
304
+
+ "ios": "ios/generated",
305
+
+ "android": "android/generated"
306
+
+ },
307
+
+ "includesGeneratedCode": true
308
+
}
309
+
```
310
+
311
+
4. Update imports in your ios code to use the new paths for the generated code:
312
+
313
+
- If you have a Turbo Module, replace `YourProjectNameSpec.h` with `YourProjectName/YourProjectNameSpec.h`:
commandLine 'cmd', '/c', 'npx bob build --target codegen'
363
+
} else {
364
+
commandLine 'sh', '-c', 'npx bob build --target codegen'
365
+
}
366
+
}
367
+
368
+
preBuild.dependsOn invokeLibraryCodegen
369
+
```
370
+
371
+
7. Add a `pre_install` hook to `example/ios/Podfile` to automatically run the codegen script when installing pods:
372
+
373
+
```ruby
374
+
pre_install do |installer|
375
+
system("cd ../../ && npx bob build --target codegen")
376
+
end
377
+
```
378
+
379
+
This will likely be inside the `target 'YourAppName' do` block.
285
380
286
-
You can ensure your Codegen generated scaffold code is stable through different React Native versions by shipping it with your library. You can find more in the [React Native Official Docs](https://reactnative.dev/docs/the-new-architecture/codegen-cli#including-generated-code-into-libraries).
381
+
And you're done! Make sure to run `pod install` in the `example/ios` folder and then run the example app to make sure everything works.
Copy file name to clipboardExpand all lines: docs/pages/faq.md
-40
Original file line number
Diff line number
Diff line change
@@ -124,46 +124,6 @@ For more accurate testing, there are various other approaches:
124
124
125
125
You can find installation and usage instructions in the [Verdaccio documentation](https://verdaccio.org/docs/en/installation).
126
126
127
-
## How to opt out of shipping codegen generated code?
128
-
129
-
We recommend shipping the generated scaffold code with your library due to [the benefits mentioned in React Native docs](https://reactnative.dev/docs/the-new-architecture/codegen-cli#including-generated-code-into-libraries). The new architecture libraries generated by `create-react-native-library` include the generated scaffold code by default.
130
-
131
-
If you have a reason to not ship Codegen generated scaffold code with your library, you need do the following steps:
132
-
133
-
1. Add `"includesGeneratedCode": false` to the `codegenConfig` field in your `package.json`:
134
-
135
-
```diff
136
-
"codegenConfig": {
137
-
// …
138
-
- "includesGeneratedCode": true
139
-
+ "includesGeneratedCode": false
140
-
}
141
-
```
142
-
143
-
2. Remove the [`codegen` target](#codegen) from the `react-native-builder-bob` field in your `package.json` or `bob.config.js`:
144
-
145
-
```diff
146
-
"source": "src",
147
-
"output": "lib",
148
-
"targets": [
149
-
// …
150
-
- "codegen"
151
-
]
152
-
```
153
-
154
-
3. If you have an `exports` field in your `package.json`, ensure that it contains `./package.json`:
155
-
156
-
```diff
157
-
"exports": {
158
-
".": {
159
-
// …
160
-
},
161
-
+ "./package.json": "./package.json"
162
-
},
163
-
```
164
-
165
-
This is required for React Native Codegen to read the `codegenConfig` field from your library's `package.json`. You can find the related issue [here](https://github.com/callstack/react-native-builder-bob/issues/637).
166
-
167
127
## Users get a warning when they install my library
168
128
169
129
If users are using Yarn 1, they may get a warning when installing your library:
0 commit comments