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"
```
-`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.
88
96
-`main`: The entry point for the commonjs build. This is used by Node - such as tests, SSR etc.
89
97
-`module`: The entry point for the ES module build. This is used by bundlers such as webpack.
90
98
-`types`: The entry point for the TypeScript definitions. This is used by TypeScript to type check the code using your library.
91
-
-`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.
92
99
-`files`: The files to include in the package when publishing with `npm`.
100
+
-`exports`: The entry points for tools that support the `exports` field in `package.json` - such as Node.js 12+ & modern browsers.
93
101
94
102
Make sure to change specify correct files according to the targets you have enabled.
95
103
96
-
**NOTE**: If you're building TypeScript definition files, also make sure that the `types` field points to a correct path. Depending on the project configuration, the path can be different for you than the example snippet (e.g. `lib/typescript/index.d.ts` if you have only the `src` directory and `rootDir` is not set).
104
+
> The `exports` field also requires the `esm` option to be enabled for the [`commonjs`](#commonjs) and [`module`](#module) targets. In addition, the file extensions of the generated files will be `.js` instead of `.cjs` and `.mjs` if the `esm` option is not enabled.
105
+
106
+
> If you're building TypeScript definition files, also make sure that the `types` field points to a correct path. Depending on the project configuration, the path can be different for you than the example snippet (e.g. `lib/typescript/index.d.ts` if you have only the `src` directory and `rootDir` is not set).
97
107
98
108
1. Add the output directory to `.gitignore` and `.eslintignore`
99
109
@@ -148,12 +158,14 @@ Various targets to build for. The available targets are:
148
158
149
159
Enable compiling source files with Babel and use commonjs module system.
150
160
151
-
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`.
161
+
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` (when `esm: true`) field of `package.json`.
152
162
153
-
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).
163
+
By default, the code is compiled to support the last 2 versions of modern browsers. It also strips TypeScript and Flow annotations as well as compiles JSX. You can customize the environments to compile for by using a [browserslist config](https://github.com/browserslist/browserslist#config-file).
154
164
155
165
In addition, the following options are supported:
156
166
167
+
-`esm` (`boolean`): Enabling this option will output ES modules compatible code for Node.js 12+, modern browsers and other tools that support `package.json`'s `exports` field. This mainly adds file extensions to the imports and exports. Note that file extensions are not added when importing a file that may have platform-specific extensions (e.g. `.android.ts`). In addition, the generated files will have `.cjs` (commonjs) and `.mjs` (modules) extensions instead of `.js` - this is necessary to disambiguate between the two formats.
168
+
157
169
-`configFile` (`boolean` | `string`): To customize the babel config used, you can pass the [`configFile`](https://babeljs.io/docs/en/options#configfile) option as `true` if you have a `babel.config.js` or a path to a custom config file. This will override the default configuration. You can extend the default configuration by using the [`react-native-builder-bob/babel-preset`](https://github.com/callstack/react-native-builder-bob/blob/main/packages/react-native-builder-bob/babel-preset.js) preset.
158
170
159
171
-`babelrc` (`boolean`): You can set the [`babelrc`](https://babeljs.io/docs/en/options#babelrc) option to `true` to enable using `.babelrc` files.
@@ -165,19 +177,19 @@ In addition, the following options are supported:
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.
174
186
175
-
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`.
187
+
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` (when `esm: true`) field of `package.json`.
0 commit comments