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
feat: add ESM support for generated project (#583)
This adds ESM support to the generated project. To do this:
- Use `.cjs` and `.mjs` file extensions for the generated files
- Add file extensions to imports in the compiled code
- Add the `exports` field in `package.json`
- Update the `moduleResolution` config to `Bundler` in `tsconfig.json`
In addition:
- Enable the new JSX runtime option for React
- Recommend removing the `react-native` field from `package.json`
This is a breaking change for library authors. After upgrading, it's
necessary to update the configuration by running the following command:
```sh
yarn bob init
```
Alternatively, they can follow the [manual configuration
guide](https://callstack.github.io/react-native-builder-bob/build#manual-configuration).
In addition, typescript consumers would need to change the following
fields in `tsconfig.json`:
```json
"jsx": "react-jsx",
"moduleResolution": "Bundler",
```
If using ESLint, it may also be necessary to disable the
"react/react-in-jsx-scope" rule:
```json
"react/react-in-jsx-scope": "off"
```
-`main`: The entry point for the commonjs build. This is used by Node - such as tests, SSR etc.
90
96
-`module`: The entry point for the ES module build. This is used by bundlers such as webpack.
91
-
-`react-native`: The entry point for the React Native apps. This is used by Metro. It's common to point to the source code here as it can make debugging easier.
92
97
-`types`: The entry point for the TypeScript definitions. This is used by TypeScript to type check the code using your library.
93
98
-`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.
94
99
-`files`: The files to include in the package when publishing with `npm`.
@@ -150,7 +155,7 @@ Various targets to build for. The available targets are:
150
155
151
156
Enable compiling source files with Babel and use commonjs module system.
152
157
153
-
This is useful for running the code in Node (SSR, tests etc.). The output file should be referenced in the `main` field of `package.json`.
158
+
This is useful for running the code in Node (SSR, tests etc.). The output file should be referenced in the `main` field and `exports['.'].require` field of `package.json`.
154
159
155
160
By default, the code is compiled to support last 2 versions of modern browsers. It also strips TypeScript and Flow annotations, and compiles JSX. You can customize the environments to compile for by using a [browserslist config](https://github.com/browserslist/browserslist#config-file).
156
161
@@ -174,7 +179,7 @@ Example:
174
179
175
180
Enable compiling source files with Babel and use ES module system. This is essentially same as the `commonjs` target and accepts the same options, but leaves the `import`/`export` statements in your code.
176
181
177
-
This is useful for bundlers which understand ES modules and can tree-shake. The output file should be referenced in the `module` field of `package.json`.
182
+
This is useful for bundlers which understand ES modules and can tree-shake. The output file should be referenced in the `module` field and `exports['.'].import` field of `package.json`.
message: `Your package.json has 'exports' field set.\n Do you want to replace it?`,
263
+
initial: true,
264
+
})
265
+
).replace;
266
+
}else{
267
+
replace=true;
268
+
}
269
+
270
+
if(replace){
271
+
pkg.exports=exports;
272
+
}
273
+
}
274
+
275
+
if(
276
+
pkg['react-native']&&
277
+
(pkg['react-native'].startsWith(source)||
278
+
pkg['react-native'].startsWith(`./${source}`))
279
+
){
280
+
const{ remove }=awaitprompts({
281
+
type: 'confirm',
282
+
name: 'remove',
283
+
message: `Your package.json has the 'react-native' field pointing to source code.\n This can cause problems when customizing babel configuration.\n Do you want to remove it?`,
0 commit comments