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: use a unique name for the exports condition for source (#948)
This is a breaking change for libraries that use
`react-native-builder-bob/vite-config`
They now must add the custom condition to their vite config. To preserve
existing behavior, add `conditions: ['source']` under `resolve`:
```diff
export default defineConfig((env) =>
mergeConfig(config(env), {
resolve: {
alias: {
[pack.name]: new URL('..', import.meta.url),
},
+ conditions: ['source'],
dedupe: Object.keys(pack.peerDependencies),
},
})
);
```
Copy file name to clipboardExpand all lines: docs/pages/esm.md
+50-5Lines changed: 50 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ To make use of the output files, ensure that your `package.json` file contains t
42
42
"types": "./lib/typescript/src/index.d.ts",
43
43
"exports": {
44
44
".": {
45
-
"source": "./src/index.tsx",
45
+
"my-library-source": "./src/index.tsx",
46
46
"types": "./lib/typescript/src/index.d.ts",
47
47
"default": "./lib/module/index.js"
48
48
},
@@ -56,10 +56,57 @@ The `exports` field is used by Node.js 12+, modern browsers and tools to determi
56
56
57
57
Here, we specify 3 conditions:
58
58
59
-
-`source`: A custom condition used by `react-native-builder-bob`to determine the source file for the library.
59
+
-`my-library-source`: A custom condition used to resolve the source file for the library in development.
60
60
-`types`: Used for the TypeScript definitions.
61
61
-`default`: Used for the actual JS code when the library is imported or required.
62
62
63
+
When creating a project, a custom condition name pointing to the source code is automatically set. You can also change it to something else if you want by updating any occurrences in your project.
64
+
65
+
TypeScript can resolve the source condition by adding it to [`customConditions`](https://www.typescriptlang.org/tsconfig/#customConditions):
66
+
67
+
```json
68
+
{
69
+
"compilerOptions": {
70
+
"customConditions": ["my-library-source"]
71
+
}
72
+
}
73
+
```
74
+
75
+
When using [`react-native-monorepo-config`](https://github.com/satya164/react-native-monorepo-config), pass the same condition to Metro so the example app resolves the library source:
If you use [Jest](https://jestjs.io), add the source condition to [`testEnvironmentOptions.customExportConditions`](https://jestjs.io/docs/configuration#testenvironmentoptions-object). With the React Native Jest preset, keep React Native's default conditions as well:
If you use [Vite](https://vitejs.dev), add the source condition to [`resolve.conditions`](https://vitejs.dev/config/#resolve-conditions):
99
+
100
+
```ts
101
+
import { defineConfig } from'vite';
102
+
103
+
exportdefaultdefineConfig({
104
+
resolve: {
105
+
conditions: ['my-library-source'],
106
+
},
107
+
});
108
+
```
109
+
63
110
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.
64
111
65
112
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)).
@@ -74,7 +121,7 @@ Using the `exports` field has a few benefits, such as:
74
121
```diff
75
122
"exports": {
76
123
".": {
77
-
"source": "./src/index.tsx",
124
+
"my-library-source": "./src/index.tsx",
78
125
"types": "./lib/typescript/src/index.d.ts",
79
126
"default": "./lib/module/index.js"
80
127
},
@@ -135,7 +182,6 @@ To configure a dual package setup, you can follow these steps:
135
182
```
136
183
137
184
Here, we specify 2 conditions:
138
-
139
185
-`import`: Used when the library is imported with an `import` statement or a dynamic `import()`. It will point to the ESM build.
140
186
-`require`: Used when the library is required with a `require` call. It will point to the CommonJS build.
141
187
@@ -292,7 +338,6 @@ There are still a few things to keep in mind if you want your library to be ESM-
292
338
```
293
339
294
340
Alternatively, if you want to be able to use the library in Node.js with `import` syntax, there are a few options:
295
-
296
341
- Use `Platform.select` instead of platform-specific extensions:
Copy file name to clipboardExpand all lines: docs/pages/faq.md
+2-5Lines changed: 2 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,6 @@ If your library depends on another react-native library containing native code,
32
32
-**Add the native library to `peerDependencies`**
33
33
34
34
This means that the consumer of the library will need to install the native library and add it to the `dependencies` section of their `package.json`. It makes sure that:
35
-
36
35
- There are no version conflicts if another package also happens to use the same library, or if the user wants to use the library in their app. While there can be multiple versions of a JavaScript-only library, there can only be one version of a native library - so avoiding version conflicts is important.
37
36
- The package manager installs it in correct location so that autolinking can work properly.
38
37
@@ -51,7 +50,6 @@ If your library depends on another react-native library containing native code,
51
50
Since this is a library, the `react-native` version specified in the `package.json` is not relevant for the consumers. It's only used for developing and testing the library. If you'd like to upgrade the `react-native` version to test with it, you'd need to:
52
51
53
52
1.**Bump versions of the following packages under `devDependencies` in the `package.json`:**
54
-
55
53
-`react-native`
56
54
-`react`
57
55
-`@types/react`
@@ -74,9 +72,8 @@ There are 2 parts to this process.
74
72
1.**Aliasing the JavaScript code**
75
73
76
74
The JavaScript (or TypeScript) source code is aliased to be used by the example app. This makes it so that when you import from `'your-library-name'`, it imports the source code directly and avoids having to rebuild the library for JavaScript only changes. We configure several tools to make this work:
77
-
78
-
-[Metro](https://facebook.github.io/metro/) is configured to allow importing from outside of the `example` directory by configuring `watchFolders`, to use the appropriate peer dependencies, and to import source code of the library in the example. This configuration exists in the `example/metro.config.js` file.
79
-
-[TypeScript](https://www.typescriptlang.org/) is configured to use the source code for type checking by using the `paths` property under `compilerOptions`. This configuration exists in the `tsconfig.json` file at the root.
75
+
-[Metro](https://facebook.github.io/metro/) is configured to allow importing from outside of the `example` directory by configuring `watchFolders`, to use the appropriate peer dependencies, and to resolve the library's custom source condition. This configuration exists in the `example/metro.config.js` file.
76
+
-[TypeScript](https://www.typescriptlang.org/) is configured to use the source code for type checking with the custom source condition and the `paths` property under `compilerOptions`. This configuration exists in the `tsconfig.json` file at the root.
0 commit comments