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
-`module`: The entry point for the ES module build. This is used by bundlers such as webpack.
103
103
-`types`: The entry point for the TypeScript definitions. This is used by TypeScript to typecheck the code using your library.
104
104
-`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.
106
106
107
107
Make sure to change specify correct files according to the targets you have enabled.
Copy file name to clipboardExpand all lines: docs/pages/esm.md
+33Lines changed: 33 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,39 @@ It's recommended to specify `"moduleResolution": "Bundler"` in your `tsconfig.js
30
30
31
31
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.
32
32
33
+
To make use of the output files, ensure that your `package.json` file contains the following fields:
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
+
33
66
## Guidelines
34
67
35
68
There are still a few things to keep in mind if you want your library to be ESM-compatible:
0 commit comments