Skip to content

Commit 86ad9c3

Browse files
committed
docs: add info about package.json entrypoint
1 parent 19f5a45 commit 86ad9c3

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

docs/pages/build.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ To configure your project manually, follow these steps:
8080
"source": "./src/index.tsx",
8181
"main": "./lib/commonjs/index.js",
8282
"module": "./lib/module/index.js",
83-
"types": "./lib/typescript/commonjs/src/index.d.ts",
8483
"exports": {
8584
".": {
8685
"import": {
@@ -91,7 +90,8 @@ To configure your project manually, follow these steps:
9190
"types": "./lib/typescript/commonjs/src/index.d.ts",
9291
"default": "./lib/commonjs/index.js"
9392
}
94-
}
93+
},
94+
"./package.json": "./package.json"
9595
},
9696
"files": [
9797
"lib",
@@ -104,7 +104,6 @@ To configure your project manually, follow these steps:
104104
- `source`: The path to the source code. It is used by `react-native-builder-bob` to detect the correct output files and provide better error messages.
105105
- `main`: The entry point for the CommonJS build. This is used by Node - such as tests, SSR etc.
106106
- `module`: The entry point for the ES module build. This is used by bundlers such as webpack.
107-
- `types`: The entry point for the TypeScript definitions. This is used by TypeScript to typecheck the code using your library.
108107
- `files`: The files to include in the package when publishing with `npm`.
109108
- `exports`: The entry points for tools that support the `exports` field in `package.json` - such as Node.js 12+ & modern browsers. See [the ESM support guide](./esm.md) for more details.
110109

docs/pages/esm.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ To make use of the output files, ensure that your `package.json` file contains t
3636
```json
3737
"main": "./lib/commonjs/index.js",
3838
"module": "./lib/module/index.js",
39-
"types": "./lib/typescript/commonjs/src/index.d.ts",
4039
"exports": {
4140
".": {
4241
"import": {
@@ -47,23 +46,29 @@ To make use of the output files, ensure that your `package.json` file contains t
4746
"types": "./lib/typescript/commonjs/src/index.d.ts",
4847
"default": "./lib/commonjs/index.js"
4948
}
50-
}
49+
},
50+
"./package.json": "./package.json"
5151
},
5252
```
5353

5454
The `main`, `module` and `types` fields are for legacy setups that don't support the `exports` field. See the [Manual configuration](build.md#manual-configuration) guide for more information about those fields.
5555

56-
The `exports` field is used by modern tools and bundlers to determine the correct entry point. Here, we specify 2 conditions:
56+
The `exports` field is used by modern tools and bundlers to determine the correct entry point. The entrypoint is specified in the `.` key and will be used when the library is imported or required directly (e.g. `import 'my-library'` or `require('my-library')`).
57+
58+
Here, we specify 2 conditions:
5759

5860
- `import`: Used when the library is imported with an `import` statement or a dynamic `import()`. It should point to the ESM build.
5961
- `require`: Used when the library is required with a `require` call. It should point to the CommonJS build.
6062

61-
Each condition has a `types` field - necessary for TypeScript to provide the appropriate definitions for the module system. The type definitions have slightly different semantics for CommonJS and ESM, so it's important to specify them separately.
63+
Each condition has 2 fields:
6264

63-
The `default` field is the fallback entry point for both conditions. It's used for the actual JS code when the library is imported or required.
65+
- `types`: necessary for TypeScript to provide the appropriate definitions for the module system. The type definitions have slightly different semantics for CommonJS and ESM, so it's important to specify them separately.
66+
- `default`: fallback entry point for both conditions. It's used for the actual JS code when the library is imported or required.
6467

6568
You can also specify additional conditions for different scenarios, such as `react-native`, `browser`, `production`, `development` etc. Note that support for these conditions depends on the tooling you're using.
6669

70+
The `./package.json` field is used to point to the library's `package.json` file. It's necessary for tools that may need to read the `package.json` file directly (e.g. [React Native Codegen](https://reactnative.dev/docs/the-new-architecture/what-is-codegen)).
71+
6772
## Guidelines
6873

6974
There are still a few things to keep in mind if you want your library to be ESM-compatible:
@@ -96,6 +101,7 @@ There are still a few things to keep in mind if you want your library to be ESM-
96101
"react-native": "./lib/commonjs/index.native.js",
97102
"default": "./lib/commonjs/index.js"
98103
}
99-
}
104+
},
105+
"./package.json": "./package.json"
100106
}
101107
```

docs/pages/faq.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ If you have a reason to not ship Codegen generated scaffold code with your libra
135135
```diff
136136
"codegenConfig": {
137137
// …
138-
+ "includesGeneratedCode": false
138+
- "includesGeneratedCode": true
139+
+ "includesGeneratedCode": false
139140
}
140141
```
141142

@@ -146,16 +147,18 @@ If you have a reason to not ship Codegen generated scaffold code with your libra
146147
"output": "lib",
147148
"targets": [
148149
// …
149-
- "codegen"
150+
- "codegen"
150151
]
151152
```
152153

153154
3. If you have an `exports` field in your `package.json`, ensure that it contains `./package.json`:
154155

155156
```diff
156157
"exports": {
157-
// …
158-
+ "./package.json": "./package.json"
158+
".": {
159+
// …
160+
},
161+
+ "./package.json": "./package.json"
159162
},
160163
```
161164

0 commit comments

Comments
 (0)