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
fix: don't ship generated code with the library (#819)
### Summary
We currently have `includesGenerateCode: true` which ships codegen specs
with the library. The original idea was that the generated code is
consistent regardless of React Native version, which can lead to better
stability.
However, in a practical sense, a different React Native version may not
support this generated code anyway, so the library may get locked to
single React Native version anyway.
> The generated code will use the React Native version defined inside
your library. So if your library is shipping with React Native 0.76, the
generated code will be based on that version. This could mean that the
generated code is not compatible with apps using previous React Native
version used by the app (e.g. an App running on React Native 0.75). -
[Including Generated Code into
Libraries](https://reactnative.dev/docs/the-new-architecture/codegen-cli)
This has caused issues for us - breaking our template almost every React
Native release due to API changes in the codegen script, which adds a
maintenance burden without any concrete benefits.
So, this change removes this option from the default template. The
documentation has been updated as well with instructions on how to
enable this.
### Test plan
Generate the Turbo Module and Fabric templates and verify that the app
builds and runs on Android & iOS.
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
@@ -122,46 +122,6 @@ For more accurate testing, there are various other approaches:
122
122
123
123
You can find installation and usage instructions in the [Verdaccio documentation](https://verdaccio.org/docs/en/installation).
124
124
125
-
## How to opt out of shipping codegen generated code?
126
-
127
-
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.
128
-
129
-
If you have a reason to not ship Codegen generated scaffold code with your library, you need do the following steps:
130
-
131
-
1. Add `"includesGeneratedCode": false` to the `codegenConfig` field in your `package.json`:
132
-
133
-
```diff
134
-
"codegenConfig": {
135
-
// …
136
-
- "includesGeneratedCode": true
137
-
+ "includesGeneratedCode": false
138
-
}
139
-
```
140
-
141
-
2. Remove the [`codegen` target](#codegen) from the `react-native-builder-bob` field in your `package.json` or `bob.config.js`:
142
-
143
-
```diff
144
-
"source": "src",
145
-
"output": "lib",
146
-
"targets": [
147
-
// …
148
-
- "codegen"
149
-
]
150
-
```
151
-
152
-
3. If you have an `exports` field in your `package.json`, ensure that it contains `./package.json`:
153
-
154
-
```diff
155
-
"exports": {
156
-
".": {
157
-
// …
158
-
},
159
-
+ "./package.json": "./package.json"
160
-
},
161
-
```
162
-
163
-
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).
164
-
165
125
## Users get a warning when they install my library
166
126
167
127
If users are using Yarn 1, they may get a warning when installing your library:
0 commit comments