Skip to content

Commit 7f80b75

Browse files
committed
refactor: drop commonjs build from default setup
1 parent a8fd434 commit 7f80b75

File tree

5 files changed

+102
-62
lines changed

5 files changed

+102
-62
lines changed

docs/pages/build.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,11 @@ To configure your project manually, follow these steps:
7878

7979
```json
8080
"source": "./src/index.tsx",
81-
"main": "./lib/commonjs/index.js",
82-
"module": "./lib/module/index.js",
81+
"main": "./lib/module/index.js",
8382
"exports": {
8483
".": {
85-
"import": {
86-
"types": "./lib/typescript/module/src/index.d.ts",
87-
"default": "./lib/module/index.js"
88-
},
89-
"require": {
90-
"types": "./lib/typescript/commonjs/src/index.d.ts",
91-
"default": "./lib/commonjs/index.js"
92-
}
84+
"types": "./lib/typescript/src/index.d.ts",
85+
"default": "./lib/module/index.js"
9386
},
9487
"./package.json": "./package.json"
9588
},
@@ -248,7 +241,7 @@ Example:
248241

249242
Enable compiling source files with Babel and use CommonJS module system. This is essentially the same as the `module` target and accepts the same options, but transforms the `import`/`export` statements in your code to `require`/`module.exports`.
250243

251-
This is useful for supporting usage of this module with `require` in Node versions older than 20 (it can still be used with `import` for Node.js 12+ if `module` target with `esm` is enabled), and some tools such a [Jest](https://jestjs.io). The output file should be referenced in the `main` field. If you have a dual module setup with both ESM and CommonJS builds, it needs to be specified in `exports['.'].require` field of `package.json`.
244+
This is useful for supporting usage of this module with `require` in Node versions older than 20 (it can still be used with `import` for Node.js 12+ if `module` target with `esm` is enabled), and some tools such as [Jest](https://jestjs.io). The output file should be referenced in the `main` field. If you have a [dual module setup](esm.md#dual-module-setup) with both ESM and CommonJS builds, it needs to be specified in `exports['.'].require` field of `package.json`.
252245

253246
Example:
254247

docs/pages/esm.md

Lines changed: 91 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@ You can verify whether ESM support is enabled by checking the configuration for
1616
}
1717
```
1818

19+
<<<<<<< HEAD
20+
The `"esm": true` option enables ESM-compatible output by adding the `.js` extension to the import statements in the generated files. This is necessary if you want to be able to import the library on Node.js or in a bundler that supports ESM, with some caveats. See the [Guidelines](#guidelines) section for more information.
21+
||||||| parent of 0330463 (refactor: drop commonjs build from default setup)
22+
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
23+
=======
24+
1925
The `"esm": true` option enables ESM-compatible output by adding the `.js` extension to the import statements in the generated files. This is necessary if you want to be able to import the library on Node.js or in a bundler that supports ESM, with some caveats. See the [Guidelines](#guidelines) section for more information.
2026

27+
For TypeScript, it also generates 2 sets of type definitions if the [`commonjs`](build.md#commonjs) target is also enabled: one for the CommonJS build and one for the ES module build.
28+
>>>>>>> 0330463 (refactor: drop commonjs build from default setup)
29+
2130
For TypeScript, it also generates 2 sets of type definitions if the [`commonjs`](build.md#commonjs) target is also enabled: one for the CommonJS build and one for the ES module build.
2231

2332
It's recommended to specify `"moduleResolution": "bundler"` in your `tsconfig.json` file to match Metro's behavior:
@@ -35,42 +44,94 @@ Specifying `"moduleResolution": "bundler"` means that you don't need to use file
3544
To make use of the output files, ensure that your `package.json` file contains the following fields:
3645

3746
```json
38-
"main": "./lib/commonjs/index.js",
39-
"module": "./lib/module/index.js",
47+
"main": "./lib/module/index.js",
4048
"exports": {
4149
".": {
42-
"import": {
43-
"types": "./lib/typescript/module/src/index.d.ts",
44-
"default": "./lib/module/index.js"
45-
},
46-
"require": {
47-
"types": "./lib/typescript/commonjs/src/index.d.ts",
48-
"default": "./lib/commonjs/index.js"
49-
}
50+
"types": "./lib/typescript/src/index.d.ts",
51+
"default": "./lib/module/index.js"
5052
},
5153
"./package.json": "./package.json"
5254
},
5355
```
5456

55-
The `main` field is for tools that don't support the `exports` field (e.g. [Jest](https://jestjs.io)). The `module` field is a non-standard field that some tools use to determine the ESM entry point.
57+
The `main` field is for tools that don't support the `exports` field (e.g. [Metro](https://metrobundler.dev/)).
5658

5759
The `exports` field is used by Node.js 12+, modern browsers and tools 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')`).
5860

5961
Here, we specify 2 conditions:
6062

61-
- `import`: Used when the library is imported with an `import` statement or a dynamic `import()`. It should point to the ESM build.
62-
- `require`: Used when the library is required with a `require` call. It should point to the CommonJS build.
63-
64-
Each condition has 2 fields:
65-
6663
- `types`: Used for the TypeScript definitions.
6764
- `default`: Used for the actual JS code when the library is imported or required.
6865

6966
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.
7067

7168
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)).
7269

73-
> Note: Metro enables support for `package.json` exports by default from version [0.82.0](https://github.com/facebook/metro/releases/tag/v0.82.0). In previous versions, experimental support can be enabled by setting the `unstable_enablePackageExports` option to `true` in the [Metro configuration](https://metrobundler.dev/docs/configuration/). If this is not enabled, Metro will use the entrypoint specified in the `main` field.
70+
## Dual module setup
71+
72+
The previously mentioned setup only works with tools that support ES modules. If you want to support tools that don't support ESM and use the CommonJS module system, you can set up a dual module setup.
73+
74+
A dual module setup means that you have 2 builds of your library: one for ESM and one for CommonJS. The ESM build is used by tools that support ES modules, while the CommonJS build is used by tools that don't support ES modules.
75+
76+
To set up a dual module setup, you can follow these steps:
77+
78+
1. Add the `commonjs` target to the `react-native-builder-bob` field in your `package.json` or `bob.config.js`:
79+
80+
```diff
81+
"react-native-builder-bob": {
82+
"source": "src",
83+
"output": "lib",
84+
"targets": [
85+
["module", { "esm": true }],
86+
+ ["commonjs", { "esm": true }]
87+
"typescript",
88+
]
89+
}
90+
```
91+
92+
2. Change the `main` field in your `package.json` to point to the CommonJS build:
93+
94+
```diff
95+
- "main": "./lib/module/index.js",
96+
+ "main": "./lib/commonjs/index.js",
97+
```
98+
99+
3. Optionally add a `module` field in your `package.json` to point to the ESM build:
100+
101+
```diff
102+
"main": "./lib/commonjs/index.js",
103+
+ "module": "./lib/module/index.js",
104+
```
105+
106+
The `module` field is a non-standard field that some tools use to determine the ESM entry point.
107+
108+
4. Change the `exports` field in your `package.json` to include 2 conditions:
109+
110+
```diff
111+
"exports": {
112+
".": {
113+
- "types": "./lib/typescript/src/index.d.ts",
114+
- "default": "./lib/module/index.js"
115+
+ "import": {
116+
+ "types": "./lib/typescript/module/src/index.d.ts",
117+
+ "default": "./lib/module/index.js"
118+
+ },
119+
+ "require": {
120+
+ "types": "./lib/typescript/commonjs/src/index.d.ts",
121+
+ "default": "./lib/commonjs/index.js"
122+
+ }
123+
}
124+
},
125+
```
126+
127+
Here, we specify 2 conditions:
128+
129+
- `import`: Used when the library is imported with an `import` statement or a dynamic `import()`. It will point to the ESM build.
130+
- `require`: Used when the library is required with a `require` call. It will point to the CommonJS build.
131+
132+
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.
133+
134+
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.
74135

75136
## Guidelines
76137

@@ -100,7 +161,19 @@ There are still a few things to keep in mind if you want your library to be ESM-
100161

101162
- 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.
102163
- 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.
103-
- If you specify a `react-native` condition in `exports`, make sure that it comes before the `default` condition. The conditions should be ordered from the most specific to the least specific:
164+
- If you specify a `react-native` condition in `exports`, make sure that it comes before other conditions. The conditions should be ordered from the most specific to the least specific:
165+
166+
```json
167+
"exports": {
168+
".": {
169+
"types": "./lib/typescript/src/index.d.ts",
170+
"react-native": "./lib/modules/index.native.js",
171+
"default": "./lib/module/index.js"
172+
}
173+
}
174+
```
175+
176+
Or for a dual module setup:
104177

105178
```json
106179
"exports": {

packages/create-react-native-library/templates/common/$package.json

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@
33
"version": "0.1.0",
44
"description": "<%- project.description %>",
55
"source": "./src/index.tsx",
6-
"main": "./lib/commonjs/index.js",
7-
"module": "./lib/module/index.js",
6+
"main": "./lib/module/index.js",
87
"exports": {
98
".": {
10-
"import": {
11-
"types": "./lib/typescript/module/src/index.d.ts",
12-
"default": "./lib/module/index.js"
13-
},
14-
"require": {
15-
"types": "./lib/typescript/commonjs/src/index.d.ts",
16-
"default": "./lib/commonjs/index.js"
17-
}
9+
"types": "./lib/typescript/src/index.d.ts",
10+
"default": "./lib/module/index.js"
1811
},
1912
"./package.json": "./package.json"
2013
},
@@ -186,12 +179,6 @@
186179
"esm": true
187180
}
188181
],
189-
[
190-
"commonjs",
191-
{
192-
"esm": true
193-
}
194-
],
195182
[
196183
"typescript",
197184
{

packages/react-native-builder-bob/src/__tests__/__snapshots__/init.test.ts.snap

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,13 @@ exports[`initializes the configuration 1`] = `
99
},
1010
"exports": {
1111
".": {
12-
"import": {
13-
"types": "./lib/typescript/module/src/index.d.ts",
14-
"default": "./lib/module/index.js"
15-
},
16-
"require": {
17-
"types": "./lib/typescript/commonjs/src/index.d.ts",
18-
"default": "./lib/commonjs/index.js"
19-
}
12+
"types": "./lib/typescript/src/index.d.ts",
13+
"default": "./lib/module/index.js"
2014
},
2115
"./package.json": "./package.json"
2216
},
2317
"source": "./src/index.ts",
24-
"main": "./lib/commonjs/index.js",
25-
"module": "./lib/module/index.js",
18+
"main": "./lib/module/index.js",
2619
"scripts": {
2720
"prepare": "bob build"
2821
},
@@ -43,12 +36,6 @@ exports[`initializes the configuration 1`] = `
4336
"esm": true
4437
}
4538
],
46-
[
47-
"commonjs",
48-
{
49-
"esm": true
50-
}
51-
],
5239
"typescript"
5340
]
5441
},

packages/react-native-builder-bob/src/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export async function init() {
101101
{
102102
title: 'commonjs - for legacy setups (Node.js < 20)',
103103
value: 'commonjs',
104-
selected: true,
104+
selected: false,
105105
},
106106
{
107107
title: 'typescript - declaration files for typechecking',

0 commit comments

Comments
 (0)