Skip to content

Commit 0d915f4

Browse files
committed
docs: update ESM support docs
1 parent 1eac18f commit 0d915f4

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

docs/pages/build.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ yarn add --dev react-native-builder-bob
7979
"types": "./lib/typescript/commonjs/src/index.d.ts",
8080
"exports": {
8181
".": {
82-
"import": {
83-
"types": "./lib/typescript/module/src/index.d.ts",
84-
"default": "./lib/module/index.js"
85-
},
86-
"require": {
87-
"types": "./lib/typescript/commonjs/src/index.d.ts",
88-
"default": "./lib/commonjs/index.js"
89-
}
82+
"import": {
83+
"types": "./lib/typescript/module/src/index.d.ts",
84+
"default": "./lib/module/index.js"
85+
},
86+
"require": {
87+
"types": "./lib/typescript/commonjs/src/index.d.ts",
88+
"default": "./lib/commonjs/index.js"
89+
}
9090
}
9191
},
9292
"files": [
@@ -102,7 +102,7 @@ yarn add --dev react-native-builder-bob
102102
- `module`: The entry point for the ES module build. This is used by bundlers such as webpack.
103103
- `types`: The entry point for the TypeScript definitions. This is used by TypeScript to typecheck the code using your library.
104104
- `files`: The files to include in the package when publishing with `npm`.
105-
- `exports`: The entry points for tools that support the `exports` field in `package.json` - such as Node.js 12+ & modern browsers.
105+
- `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.
106106

107107
Make sure to change specify correct files according to the targets you have enabled.
108108

docs/pages/esm.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,39 @@ It's recommended to specify `"moduleResolution": "Bundler"` in your `tsconfig.js
3030

3131
This means that you don't need to specify the file extension in the import statements. They'll be automatically added when possible during the build process.
3232

33+
To make use of the output files, ensure that your `package.json` file contains the following fields:
34+
35+
```json
36+
"main": "./lib/commonjs/index.js",
37+
"module": "./lib/module/index.js",
38+
"types": "./lib/typescript/commonjs/src/index.d.ts",
39+
"exports": {
40+
".": {
41+
"import": {
42+
"types": "./lib/typescript/module/src/index.d.ts",
43+
"default": "./lib/module/index.js"
44+
},
45+
"require": {
46+
"types": "./lib/typescript/commonjs/src/index.d.ts",
47+
"default": "./lib/commonjs/index.js"
48+
}
49+
}
50+
},
51+
```
52+
53+
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.
54+
55+
The `exports` field is used by modern tools and bundlers to determine the correct entry point. Here, we specify 2 conditions:
56+
57+
- `import`: Used when the library is imported with an `import` statement or a dynamic `import()`. It should point to the ESM build.
58+
- `require`: Used when the library is required with a `require` call. It should point to the CommonJS build.
59+
60+
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.
61+
62+
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.
63+
64+
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.
65+
3366
## Guidelines
3467

3568
There are still a few things to keep in mind if you want your library to be ESM-compatible:

0 commit comments

Comments
 (0)