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
If you created a project with [`create-react-native-library`](./create.md), `react-native-builder-bob` is **already pre-configured to build your project**. You don't need to configure it again.
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 yet. You can either add it manually or create a new library with `create-react-native-library`.
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
28
29
29
You can find details on what exactly it adds in the [Manual configuration](#manual-configuration) section.
30
30
@@ -34,28 +34,28 @@ To configure your project manually, follow these steps:
34
34
35
35
1. First, install `react-native-builder-bob` in your project. Open a Terminal in your project, and run:
36
36
37
-
```sh
38
-
yarn add --dev react-native-builder-bob
39
-
```
37
+
```sh
38
+
yarn add --dev react-native-builder-bob
39
+
```
40
40
41
-
1. In your `package.json`, specify the targets to build for:
41
+
2. In your `package.json`, specify the targets to build for:
42
42
43
43
```json
44
44
"react-native-builder-bob": {
45
45
"source": "src",
46
46
"output": "lib",
47
47
"targets": [
48
-
"codegen",
49
48
["commonjs", { "esm": true }],
50
49
["module", { "esm": true }],
51
-
["typescript", { "esm": true }]
50
+
"typescript",
51
+
"codegen",
52
52
]
53
53
}
54
54
```
55
55
56
56
See the [Options](#options) section for more details.
If you are not sure which one to use, we recommend going with `prepare` as it works during both publishing and installing from GIT with more package managers.
> If you're building TypeScript definition files, also make sure that the `types` field points to a correct path. Depending on the project configuration, the path can be different for you than the example snippet (e.g. `lib/typescript/index.d.ts` if you have only the `src` directory and `rootDir` is not set).
114
114
115
-
1. Add the output directory to `.gitignore` and `.eslintignore`
115
+
5. Add the output directory to `.gitignore` and `.eslintignore`
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.
135
135
@@ -262,6 +262,8 @@ Example:
262
262
263
263
Enable generating type definitions with `tsc` if your source code is written in [TypeScript](http://www.typescriptlang.org/).
264
264
265
+
When both `module` and `commonjs` targets are enabled, and `esm` is set to `true` for the `module` target, this will output 2 sets of type definitions: one for the CommonJS build and one for the ES module build.
Copy file name to clipboardExpand all lines: docs/pages/esm.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,25 +11,25 @@ You can verify whether ESM support is enabled by checking the configuration for
11
11
"targets": [
12
12
["commonjs", { "esm": true }],
13
13
["module", { "esm": true }],
14
-
["typescript", { "esm": true }]
14
+
"typescript"
15
15
]
16
16
}
17
17
```
18
18
19
19
The `"esm": true` option enables ESM-compatible output by adding the `.js` extension to the import statements in the generated files. For TypeScript, it also generates 2 sets of type definitions: one for the CommonJS build and one for the ES module build.
20
20
21
-
It's recommended to specify `"moduleResolution": "Bundler"` and `"resolvePackageJsonImports": false` in your `tsconfig.json` file to match [Metro's behavior](https://reactnative.dev/blog/2023/06/21/package-exports-support#enabling-package-exports-beta):
21
+
It's recommended to specify `"moduleResolution": "bundler"` and `"resolvePackageJsonImports": false` in your `tsconfig.json` file to match [Metro's behavior](https://reactnative.dev/blog/2023/06/21/package-exports-support#enabling-package-exports-beta):
22
22
23
23
```json
24
24
{
25
25
"compilerOptions": {
26
-
"moduleResolution": "Bundler",
26
+
"moduleResolution": "bundler",
27
27
"resolvePackageJsonImports": false
28
28
}
29
29
}
30
30
```
31
31
32
-
Specifying `"moduleResolution": "Bundler"` means that you don't need to use file extensions in the import statements. Bob automatically adds them when possible during the build process.
32
+
Specifying `"moduleResolution": "bundler"` means that you don't need to use file extensions in the import statements. Bob automatically adds them when possible during the build process.
33
33
34
34
To make use of the output files, ensure that your `package.json` file contains the following fields:
35
35
@@ -80,7 +80,7 @@ There are still a few things to keep in mind if you want your library to be ESM-
80
80
```
81
81
82
82
- Avoid using `.cjs`, `.mjs`, `.cts` or `.mts` extensions. Metro always requires file extensions in import statements when using `.cjs` or `.mjs` which breaks platform-specific extension resolution.
83
-
- Avoid using `"moduleResolution": "Node16"` or `"moduleResolution": "NodeNext"` in your `tsconfig.json` file. They require file extensions in import statements which breaks platform-specific extension resolution.
83
+
- Avoid using `"moduleResolution": "node16"` or `"moduleResolution": "nodenext"` in your `tsconfig.json` file. They require file extensions in import statements which breaks platform-specific extension resolution.
84
84
- If you specify a `react-native` condition in `exports`, make sure that it comes before `import` or `require`. The conditions should be ordered from the most specific to the least specific:
0 commit comments