Commit f11b666
authored
refactor: drop commonjs build from default setup (#800)
There's a possibility of [dual package
hazard](https://nodejs.org/docs/latest-v19.x/api/packages.html#dual-package-hazard)
with the current setup. Metro enables `exports` support from
[0.82.0](https://github.com/facebook/metro/releases/tag/v0.82.0), so
this can become a problem for some packages.
So this drops the dual package setup in favor of an ESM-only setup. To
make a similar change in your package, apply the following change to
your entrypoints:
```diff
- "main": "./lib/commonjs/index.js",
+ "main": "./lib/module/index.js",a
- "types": "./lib/typescript/commonjs/src/index.d.ts",
"exports": {
".": {
- "import": {
- "types": "./lib/typescript/module/src/index.d.ts",
- "default": "./lib/module/index.js"
- },
- "require": {
- "types": "./lib/typescript/commonjs/src/index.d.ts",
- "default": "./lib/commonjs/index.js"
- }
+ "types": "./lib/typescript/src/index.d.ts",
+ "default": "./lib/module/index.js"
}
},
```
Also, remove the `commonjs` target from the `react-native-builder-bob`
field in your `package.json` or `bob.config.js` or `bob.config.mjs`:
```diff
"react-native-builder-bob": {
"source": "src",
"output": "lib",
"targets": [
["module", { "esm": true }],
- ["commonjs", { "esm": true }]
"typescript",
]
}
```
With this change, [Jest](https://jestjs.io/) will break for the
consumers of your libraries due to the usage of ESM syntax. So they may
need to update their Jest configuration to transform your library:
```js
module.exports = {
preset: 'react-native',
+ transform: {
+ 'node_modules/(your-library|another-library)': 'babel-jest',
+ },
};
```
If consumers of your library are using it in NodeJS in a CommonJS
environment, they'll need to use at least NodeJS v20.19.0 to be able to
synchronously `require` your library.
You can read more at our [ESM support
docs](https://callstack.github.io/react-native-builder-bob/esm#dual-package-setup).1 parent a8fd434 commit f11b666
File tree
6 files changed
+106
-75
lines changed- docs/pages
- packages
- create-react-native-library/templates/common
- react-native-builder-bob/src
- __tests__/__snapshots__
- targets
6 files changed
+106
-75
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
82 | | - | |
| 81 | + | |
83 | 82 | | |
84 | 83 | | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
| 84 | + | |
| 85 | + | |
93 | 86 | | |
94 | 87 | | |
95 | 88 | | |
| |||
248 | 241 | | |
249 | 242 | | |
250 | 243 | | |
251 | | - | |
| 244 | + | |
252 | 245 | | |
253 | 246 | | |
254 | 247 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
39 | | - | |
| 38 | + | |
40 | 39 | | |
41 | 40 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
| 41 | + | |
| 42 | + | |
50 | 43 | | |
51 | 44 | | |
52 | 45 | | |
53 | 46 | | |
54 | 47 | | |
55 | | - | |
| 48 | + | |
56 | 49 | | |
57 | 50 | | |
58 | 51 | | |
59 | 52 | | |
60 | 53 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | 54 | | |
67 | 55 | | |
68 | 56 | | |
| |||
72 | 60 | | |
73 | 61 | | |
74 | 62 | | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
75 | 133 | | |
76 | 134 | | |
77 | 135 | | |
| |||
100 | 158 | | |
101 | 159 | | |
102 | 160 | | |
103 | | - | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
104 | 174 | | |
105 | 175 | | |
106 | 176 | | |
| |||
Lines changed: 3 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
| 6 | + | |
8 | 7 | | |
9 | 8 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
| 9 | + | |
| 10 | + | |
18 | 11 | | |
19 | 12 | | |
20 | 13 | | |
| |||
186 | 179 | | |
187 | 180 | | |
188 | 181 | | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | 182 | | |
196 | 183 | | |
197 | 184 | | |
| |||
Lines changed: 3 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 12 | + | |
| 13 | + | |
20 | 14 | | |
21 | 15 | | |
22 | 16 | | |
23 | 17 | | |
24 | | - | |
25 | | - | |
| 18 | + | |
26 | 19 | | |
27 | 20 | | |
28 | 21 | | |
| |||
43 | 36 | | |
44 | 37 | | |
45 | 38 | | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | 39 | | |
53 | 40 | | |
54 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
104 | | - | |
| 104 | + | |
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
| |||
299 | 299 | | |
300 | 300 | | |
301 | 301 | | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | 302 | | |
311 | 303 | | |
312 | 304 | | |
| |||
Lines changed: 8 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
257 | 257 | | |
258 | 258 | | |
259 | 259 | | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
266 | 268 | | |
267 | 269 | | |
268 | 270 | | |
| |||
0 commit comments