react-native-monorepo-config 0.1.5 → 0.1.7

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.
Files changed (2) hide show
  1. package/index.js +57 -28
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -34,33 +34,42 @@ export function withMetroConfig(baseConfig, { root, dirname }) {
34
34
 
35
35
  // Get the list of monorepo packages except current package
36
36
  // Yarn also supports workspaces as an object with a "packages" field
37
- const packages = (pkg.workspaces.packages || pkg.workspaces)
38
- .flatMap((pattern) =>
39
- glob.sync(pattern, {
40
- cwd: root,
41
- onlyDirectories: true,
42
- ignore: [`**/node_modules`, `**/.git`, `**/.yarn`],
37
+ const packages = Object.fromEntries(
38
+ (pkg.workspaces.packages || pkg.workspaces)
39
+ .flatMap((pattern) =>
40
+ glob.sync(pattern, {
41
+ cwd: root,
42
+ onlyDirectories: true,
43
+ ignore: [`**/node_modules`, `**/.git`, `**/.yarn`],
44
+ })
45
+ )
46
+ .map((p) => path.join(root, p))
47
+ .filter((dir) => {
48
+ // Exclude current package
49
+ if (path.relative(dir, dirname) === '') {
50
+ return false;
51
+ }
52
+
53
+ // Ignore folders that don't have a package.json
54
+ return fs.existsSync(path.join(dir, 'package.json'));
43
55
  })
44
- )
45
- .map((p) => path.join(root, p))
46
- .filter((dir) => {
47
- // Exclude current package
48
- if (path.relative(dir, dirname) === '') {
49
- return false;
50
- }
51
-
52
- // Ignore folders that don't have a package.json
53
- return fs.existsSync(path.join(dir, 'package.json'));
54
- });
56
+ .map((dir) => {
57
+ const pak = JSON.parse(
58
+ fs.readFileSync(path.join(dir, 'package.json'), 'utf8')
59
+ );
60
+
61
+ return [pak.name, dir];
62
+ })
63
+ );
55
64
 
56
65
  // If monorepo root contains a name, add it to the list of packages
57
66
  // Necessary if the root is a package itself
58
67
  if (pkg.name) {
59
- packages.push(root);
68
+ packages[pkg.name] = root;
60
69
  }
61
70
 
62
71
  // Get the list of peer dependencies for all packages in the monorepo
63
- const peers = packages
72
+ const peers = Object.values(packages)
64
73
  .flatMap((dir) => {
65
74
  const pak = JSON.parse(
66
75
  fs.readFileSync(path.join(dir, 'package.json'), 'utf8')
@@ -77,7 +86,7 @@ export function withMetroConfig(baseConfig, { root, dirname }) {
77
86
  // Otherwise duplicate versions of the same package will be loaded
78
87
  const blockList = new RegExp(
79
88
  '(' +
80
- packages
89
+ Object.values(packages)
81
90
  .flatMap((dir) =>
82
91
  peers.map(
83
92
  (m) => `^${escape(path.join(dir, 'node_modules', m))}\\/.*$`
@@ -103,6 +112,13 @@ export function withMetroConfig(baseConfig, { root, dirname }) {
103
112
  return acc;
104
113
  }, {});
105
114
 
115
+ // If monorepo root is a package, add it to extraNodeModules so metro can find it
116
+ // Normally monorepo packages are symlinked to node_modules, but the root is not
117
+ // So we need to add it manually
118
+ if (pkg.name) {
119
+ extraNodeModules[pkg.name] = root;
120
+ }
121
+
106
122
  /** @type {import('metro-config').MetroConfig} */
107
123
  return {
108
124
  ...baseConfig,
@@ -119,17 +135,30 @@ export function withMetroConfig(baseConfig, { root, dirname }) {
119
135
 
120
136
  blockList,
121
137
  extraNodeModules,
122
- resolveRequest: (context, realModuleName, platform) => {
138
+ resolveRequest: (originalContext, moduleName, platform) => {
139
+ let context = originalContext;
140
+
123
141
  // Prefer the source field for monorepo packages to consume source code
124
- if (packages.includes(realModuleName)) {
125
- context.mainFields = ['source', ...context.mainFields];
126
- context.unstable_conditionNames = [
127
- 'source',
128
- ...context.unstable_conditionNames,
129
- ];
142
+ if (packages[moduleName]) {
143
+ context = {
144
+ ...context,
145
+ mainFields: ['source', ...context.mainFields],
146
+ unstable_conditionNames: [
147
+ 'source',
148
+ ...context.unstable_conditionNames,
149
+ ],
150
+ };
130
151
  }
131
152
 
132
- return context.resolveRequest(context, realModuleName, platform);
153
+ if (baseConfig.resolver.resolveRequest) {
154
+ return baseConfig.resolver.resolveRequest(
155
+ context,
156
+ moduleName,
157
+ platform
158
+ );
159
+ } else {
160
+ return context.resolveRequest(context, moduleName, platform);
161
+ }
133
162
  },
134
163
  },
135
164
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-monorepo-config",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
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/)",