react-native-monorepo-config 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -3
- package/index.js +16 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,6 +12,8 @@ Helper to configure Metro for a React Native app in a monorepo.
|
|
|
12
12
|
npm install --save-dev react-native-monorepo-config
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
Also make sure that your're using recent version of Node.js, i.e. v20.19.0 and higher (LTS), v22.12.0 and higher (LTS) or v23.4.0 and higher.
|
|
16
|
+
|
|
15
17
|
## Usage
|
|
16
18
|
|
|
17
19
|
Let's consider the following monorepo structure:
|
|
@@ -71,7 +73,43 @@ module.exports = {
|
|
|
71
73
|
|
|
72
74
|
This configuration will setup a few things:
|
|
73
75
|
|
|
74
|
-
- Configure Metro to watch for changes in
|
|
75
|
-
|
|
76
|
+
- Configure Metro to watch for changes in the monorepo root instead of only the current package.
|
|
77
|
+
|
|
78
|
+
This may slow down the bundling process in large monorepos. In that case, you can override `watchFolders` to add specific folders to watch instead:
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
module.exports = {
|
|
82
|
+
watchFolders: [
|
|
83
|
+
path.resolve(__dirname),
|
|
84
|
+
// Path to packages that the app depends on
|
|
85
|
+
path.resolve(__dirname, '../packages/a'),
|
|
86
|
+
path.resolve(__dirname, '../packages/b'),
|
|
87
|
+
],
|
|
88
|
+
|
|
89
|
+
...monoRepoConfig,
|
|
90
|
+
};
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
- Block packages defined in `peerDependencies` of other packages in the monorepo to avoid duplicate versions from being loaded. They must be added under `dependencies` or `devDependencies` in the app's `package.json`.
|
|
94
|
+
|
|
95
|
+
Loading duplicate versions of some packages such as `react` can cause issues. So this way multiple versions are not loaded. Make sure to specify `peerDependencies` for your packages appropriately.
|
|
96
|
+
|
|
76
97
|
- If the packages defined in `peerDependencies` have been hoisted to the monorepo root, point Metro to them so they can be found.
|
|
77
|
-
- Configure Metro's resolve to prioritize `package.json#source` or the `source` condition in `package.json#exports` so that the app can import source code directly from other packages in the monorepo.
|
|
98
|
+
- Configure Metro's resolve to prioritize `package.json#source` or the `source` condition in `package.json#exports` so that the app can import source code directly from other packages in the monorepo.
|
|
99
|
+
|
|
100
|
+
To utilize this, make sure to add the `source` field to the `package.json` of the packages you want to import from, e.g.:
|
|
101
|
+
|
|
102
|
+
```json
|
|
103
|
+
"source": "src/index.ts"
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Or in the `exports` field if you're using `exports` field and Metro 0.82.0+:
|
|
107
|
+
|
|
108
|
+
```json
|
|
109
|
+
"exports": {
|
|
110
|
+
".": {
|
|
111
|
+
"source": "./src/index.ts",
|
|
112
|
+
// other conditions...
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
```
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import escape from 'escape-string-regexp';
|
|
2
2
|
import glob from 'fast-glob';
|
|
3
3
|
import fs from 'node:fs';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
4
5
|
import path from 'node:path';
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -99,11 +100,21 @@ export function withMetroConfig(baseConfig, { root, dirname }) {
|
|
|
99
100
|
// When we import a package from the monorepo, metro may not be able to find the deps in blockList
|
|
100
101
|
// We need to specify them in `extraNodeModules` to tell metro where to find them
|
|
101
102
|
const extraNodeModules = peers.reduce((acc, name) => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
.
|
|
103
|
+
let dir;
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
// Try to resolve the package relative to the current package using node resolution
|
|
107
|
+
// This may fail if the module's `package.json` is not in its `exports`
|
|
108
|
+
const require = createRequire(path.join(dirname, 'package.json'));
|
|
109
|
+
|
|
110
|
+
dir = path.dirname(require.resolve(`${name}/package.json`));
|
|
111
|
+
} catch (e) {
|
|
112
|
+
// First, try to find the package in the current package's node_modules
|
|
113
|
+
// As a fallback, try to find it in the monorepo root
|
|
114
|
+
dir = [dirname, root]
|
|
115
|
+
.map((d) => path.join(d, 'node_modules', name))
|
|
116
|
+
.find((d) => fs.existsSync(d));
|
|
117
|
+
}
|
|
107
118
|
|
|
108
119
|
if (dir) {
|
|
109
120
|
acc[name] = dir;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-monorepo-config",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "Configure Metro for a React Native app in a monorepo",
|
|
5
5
|
"repository": "https://github.com/satya164/react-native-monorepo-config",
|
|
6
6
|
"author": "Satyajit Sahoo <satyajit.happy@gmail.com> (https://github.com/satya164/)",
|