Skip to content

Commit da5df0b

Browse files
committed
refactor: move to react-native-monorepo-config
1 parent ed2a30f commit da5df0b

File tree

7 files changed

+10
-169
lines changed

7 files changed

+10
-169
lines changed

docs/pages/faq.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ There are 2 parts to this process.
7373

7474
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:
7575

76-
- [Babel](https://babeljs.io) is configured to use the alias in `example/babel.config.js` using [babel-plugin-module-resolver](https://github.com/tleunen/babel-plugin-module-resolver). This transforms the imports to point to the source code instead.
77-
- [Metro](https://facebook.github.io/metro/) is configured to allow importing from outside of the `example` directory by configuring `watchFolders`, and to use the appropriate peer dependencies. This configuration exists in the `example/metro.config.js` file.
78-
- [Webpack](https://webpack.js.org/) is configured to compile the library source code when running on the Web. This configuration exists in the `example/webpack.config.js` file.
76+
- [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.
7977
- [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.
8078

8179
2. **Linking the native code**

packages/create-react-native-library/templates/example-common/example/metro.config.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const path = require('path');
22
const { getDefaultConfig } = require('@react-native/metro-config');
33
const { getConfig } = require('react-native-builder-bob/metro-config');
4-
const pkg = require('../package.json');
54

65
const root = path.resolve(__dirname, '..');
76

@@ -13,6 +12,5 @@ const root = path.resolve(__dirname, '..');
1312
*/
1413
module.exports = getConfig(getDefaultConfig(__dirname), {
1514
root,
16-
pkg,
1715
project: __dirname,
1816
});

packages/create-react-native-library/templates/expo-library/example/metro.config.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const path = require('path');
22
const { getDefaultConfig } = require('@expo/metro-config');
33
const { getConfig } = require('react-native-builder-bob/metro-config');
4-
const pkg = require('../package.json');
54

65
const root = path.resolve(__dirname, '..');
76

@@ -13,6 +12,5 @@ const root = path.resolve(__dirname, '..');
1312
*/
1413
module.exports = getConfig(getDefaultConfig(__dirname), {
1514
root,
16-
pkg,
1715
project: __dirname,
1816
});

packages/react-native-builder-bob/babel-config.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const { loadConfig } = require('./lib/utils/loadConfig');
1414
* @param {object} options.pkg Content of package.json of the library
1515
* @returns {import('@babel/core').TransformOptions} Babel configuration
1616
*/
17-
const getConfig = (defaultConfig, { root, pkg }) => {
17+
const getConfig = (defaultConfig, { root }) => {
1818
const result = loadConfig(root);
1919

2020
if (result == null) {
@@ -33,20 +33,6 @@ const getConfig = (defaultConfig, { root, pkg }) => {
3333
...defaultConfig,
3434
overrides: [
3535
...(defaultConfig.overrides == null ? [] : defaultConfig.overrides),
36-
{
37-
exclude: /\/node_modules\//,
38-
plugins: [
39-
[
40-
require.resolve('babel-plugin-module-resolver'),
41-
{
42-
extensions: ['.tsx', '.ts', '.jsx', '.js', '.json'],
43-
alias: {
44-
[pkg.name]: path.join(root, pkg.source),
45-
},
46-
},
47-
],
48-
],
49-
},
5036
{
5137
include: path.join(root, source),
5238
presets: [
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,17 @@
11
/* eslint-disable @typescript-eslint/no-require-imports, import-x/no-commonjs, no-undef */
22

3-
const path = require('path');
4-
const escape = require('escape-string-regexp');
5-
const exclusionList = require('metro-config/src/defaults/exclusionList');
3+
const { withMetroConfig } = require('react-native-monorepo-config');
64

75
/**
86
* Get Metro configuration for the example project.
97
* This sets up appropriate root and watch folders for the library.
108
* It also excludes conflicting modules and aliases them to the correct place.
119
*
12-
* @param {import('metro-config').MetroConfig} defaultConfig Default Metro configuration
10+
* @param {import('metro-config').MetroConfig} baseConfig Base Metro configuration
1311
* @param {object} options Options to customize the configuration
1412
* @param {string} options.root Root directory of the monorepo
15-
* @param {object} options.pkg Content of package.json of the library
1613
* @param {string} options.project Directory containing the example project
1714
* @returns {import('metro-config').MetroConfig} Metro configuration
1815
*/
19-
const getConfig = (defaultConfig, { root, pkg, project }) => {
20-
const modules = [
21-
// AssetsRegistry is used internally by React Native to handle asset imports
22-
// This needs to be a singleton so all assets are registered to a single registry
23-
'@react-native/assets-registry',
24-
...Object.keys({ ...pkg.peerDependencies }),
25-
];
26-
27-
/**
28-
* Metro configuration
29-
* https://facebook.github.io/metro/docs/configuration
30-
*
31-
* @type {import('metro-config').MetroConfig}
32-
*/
33-
return {
34-
...defaultConfig,
35-
36-
projectRoot: project,
37-
watchFolders: [root],
38-
39-
// We need to make sure that only one version is loaded for peerDependencies
40-
// So we block them at the root, and alias them to the versions in example project's node_modules
41-
resolver: {
42-
...defaultConfig.resolver,
43-
44-
blacklistRE: exclusionList(
45-
modules.map(
46-
(m) =>
47-
new RegExp(`^${escape(path.join(root, 'node_modules', m))}\\/.*$`)
48-
)
49-
),
50-
51-
extraNodeModules: modules.reduce((acc, name) => {
52-
acc[name] = path.join(project, 'node_modules', name);
53-
return acc;
54-
}, {}),
55-
},
56-
};
57-
};
58-
59-
exports.getConfig = getConfig;
16+
exports.getConfig = (baseConfig, { root, project }) =>
17+
withMetroConfig(baseConfig, { root, dirname: project });

packages/react-native-builder-bob/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"@babel/preset-react": "^7.24.7",
4848
"@babel/preset-typescript": "^7.24.7",
4949
"arktype": "^2.1.15",
50-
"babel-plugin-module-resolver": "^5.0.2",
5150
"babel-plugin-syntax-hermes-parser": "^0.28.0",
5251
"browserslist": "^4.20.4",
5352
"cross-spawn": "^7.0.3",

yarn.lock

+4-100
Original file line numberDiff line numberDiff line change
@@ -4674,19 +4674,6 @@ __metadata:
46744674
languageName: node
46754675
linkType: hard
46764676

4677-
"babel-plugin-module-resolver@npm:^5.0.2":
4678-
version: 5.0.2
4679-
resolution: "babel-plugin-module-resolver@npm:5.0.2"
4680-
dependencies:
4681-
find-babel-config: ^2.1.1
4682-
glob: ^9.3.3
4683-
pkg-up: ^3.1.0
4684-
reselect: ^4.1.7
4685-
resolve: ^1.22.8
4686-
checksum: f1d198acbbbd0b76c9c0c4aacbf9f1ef90f8d36b3d5209d9e7a75cadee2113a73711550ebddeb9464d143b71df19adc75e165dff99ada2614d7ea333affe3b5a
4687-
languageName: node
4688-
linkType: hard
4689-
46904677
"babel-plugin-polyfill-corejs2@npm:^0.4.10":
46914678
version: 0.4.11
46924679
resolution: "babel-plugin-polyfill-corejs2@npm:0.4.11"
@@ -7465,16 +7452,6 @@ __metadata:
74657452
languageName: node
74667453
linkType: hard
74677454

7468-
"find-babel-config@npm:^2.1.1":
7469-
version: 2.1.1
7470-
resolution: "find-babel-config@npm:2.1.1"
7471-
dependencies:
7472-
json5: ^2.2.3
7473-
path-exists: ^4.0.0
7474-
checksum: 4be54397339520e0cd49870acb10366684ffc001fd0b7bffedd0fe9d3e1d82234692d3cb4e5ba95280a35887238ba6f82dc79569a13a3749ae3931c23e0b3a99
7475-
languageName: node
7476-
linkType: hard
7477-
74787455
"find-up@npm:^2.0.0":
74797456
version: 2.1.0
74807457
resolution: "find-up@npm:2.1.0"
@@ -7484,15 +7461,6 @@ __metadata:
74847461
languageName: node
74857462
linkType: hard
74867463

7487-
"find-up@npm:^3.0.0":
7488-
version: 3.0.0
7489-
resolution: "find-up@npm:3.0.0"
7490-
dependencies:
7491-
locate-path: ^3.0.0
7492-
checksum: 38eba3fe7a66e4bc7f0f5a1366dc25508b7cfc349f852640e3678d26ad9a6d7e2c43eff0a472287de4a9753ef58f066a0ea892a256fa3636ad51b3fe1e17fae9
7493-
languageName: node
7494-
linkType: hard
7495-
74967464
"find-up@npm:^4.0.0, find-up@npm:^4.1.0":
74977465
version: 4.1.0
74987466
resolution: "find-up@npm:4.1.0"
@@ -7874,18 +7842,6 @@ __metadata:
78747842
languageName: node
78757843
linkType: hard
78767844

7877-
"glob@npm:^9.3.3":
7878-
version: 9.3.5
7879-
resolution: "glob@npm:9.3.5"
7880-
dependencies:
7881-
fs.realpath: ^1.0.0
7882-
minimatch: ^8.0.2
7883-
minipass: ^4.2.4
7884-
path-scurry: ^1.6.1
7885-
checksum: 94b093adbc591bc36b582f77927d1fb0dbf3ccc231828512b017601408be98d1fe798fc8c0b19c6f2d1a7660339c3502ce698de475e9d938ccbb69b47b647c84
7886-
languageName: node
7887-
linkType: hard
7888-
78897845
"global-dirs@npm:^0.1.1":
78907846
version: 0.1.1
78917847
resolution: "global-dirs@npm:0.1.1"
@@ -9296,16 +9252,6 @@ __metadata:
92969252
languageName: node
92979253
linkType: hard
92989254

9299-
"locate-path@npm:^3.0.0":
9300-
version: 3.0.0
9301-
resolution: "locate-path@npm:3.0.0"
9302-
dependencies:
9303-
p-locate: ^3.0.0
9304-
path-exists: ^3.0.0
9305-
checksum: 53db3996672f21f8b0bf2a2c645ae2c13ffdae1eeecfcd399a583bce8516c0b88dcb4222ca6efbbbeb6949df7e46860895be2c02e8d3219abd373ace3bfb4e11
9306-
languageName: node
9307-
linkType: hard
9308-
93099255
"locate-path@npm:^5.0.0":
93109256
version: 5.0.0
93119257
resolution: "locate-path@npm:5.0.0"
@@ -10980,15 +10926,6 @@ __metadata:
1098010926
languageName: node
1098110927
linkType: hard
1098210928

10983-
"minimatch@npm:^8.0.2":
10984-
version: 8.0.4
10985-
resolution: "minimatch@npm:8.0.4"
10986-
dependencies:
10987-
brace-expansion: ^2.0.1
10988-
checksum: 2e46cffb86bacbc524ad45a6426f338920c529dd13f3a732cc2cf7618988ee1aae88df4ca28983285aca9e0f45222019ac2d14ebd17c1edadd2ee12221ab801a
10989-
languageName: node
10990-
linkType: hard
10991-
1099210929
"minimatch@npm:^9.0.0, minimatch@npm:^9.0.1":
1099310930
version: 9.0.3
1099410931
resolution: "minimatch@npm:9.0.3"
@@ -11095,13 +11032,6 @@ __metadata:
1109511032
languageName: node
1109611033
linkType: hard
1109711034

11098-
"minipass@npm:^4.2.4":
11099-
version: 4.2.8
11100-
resolution: "minipass@npm:4.2.8"
11101-
checksum: 7f4914d5295a9a30807cae5227a37a926e6d910c03f315930fde52332cf0575dfbc20295318f91f0baf0e6bb11a6f668e30cde8027dea7a11b9d159867a3c830
11102-
languageName: node
11103-
linkType: hard
11104-
1110511035
"minipass@npm:^5.0.0":
1110611036
version: 5.0.0
1110711037
resolution: "minipass@npm:5.0.0"
@@ -11754,7 +11684,7 @@ __metadata:
1175411684
languageName: node
1175511685
linkType: hard
1175611686

11757-
"p-limit@npm:^2.0.0, p-limit@npm:^2.2.0":
11687+
"p-limit@npm:^2.2.0":
1175811688
version: 2.3.0
1175911689
resolution: "p-limit@npm:2.3.0"
1176011690
dependencies:
@@ -11781,15 +11711,6 @@ __metadata:
1178111711
languageName: node
1178211712
linkType: hard
1178311713

11784-
"p-locate@npm:^3.0.0":
11785-
version: 3.0.0
11786-
resolution: "p-locate@npm:3.0.0"
11787-
dependencies:
11788-
p-limit: ^2.0.0
11789-
checksum: 83991734a9854a05fe9dbb29f707ea8a0599391f52daac32b86f08e21415e857ffa60f0e120bfe7ce0cc4faf9274a50239c7895fc0d0579d08411e513b83a4ae
11790-
languageName: node
11791-
linkType: hard
11792-
1179311714
"p-locate@npm:^4.1.0":
1179411715
version: 4.1.0
1179511716
resolution: "p-locate@npm:4.1.0"
@@ -12042,7 +11963,7 @@ __metadata:
1204211963
languageName: node
1204311964
linkType: hard
1204411965

12045-
"path-scurry@npm:^1.10.1, path-scurry@npm:^1.6.1":
11966+
"path-scurry@npm:^1.10.1":
1204611967
version: 1.11.1
1204711968
resolution: "path-scurry@npm:1.11.1"
1204811969
dependencies:
@@ -12168,15 +12089,6 @@ __metadata:
1216812089
languageName: node
1216912090
linkType: hard
1217012091

12171-
"pkg-up@npm:^3.1.0":
12172-
version: 3.1.0
12173-
resolution: "pkg-up@npm:3.1.0"
12174-
dependencies:
12175-
find-up: ^3.0.0
12176-
checksum: 5bac346b7c7c903613c057ae3ab722f320716199d753f4a7d053d38f2b5955460f3e6ab73b4762c62fd3e947f58e04f1343e92089e7bb6091c90877406fcd8c8
12177-
languageName: node
12178-
linkType: hard
12179-
1218012092
"postcss-selector-parser@npm:^6.0.10":
1218112093
version: 6.0.13
1218212094
resolution: "postcss-selector-parser@npm:6.0.13"
@@ -12425,7 +12337,6 @@ __metadata:
1242512337
"@types/which": ^2.0.1
1242612338
"@types/yargs": ^17.0.10
1242712339
arktype: ^2.1.15
12428-
babel-plugin-module-resolver: ^5.0.2
1242912340
babel-plugin-syntax-hermes-parser: ^0.28.0
1243012341
browserslist: ^4.20.4
1243112342
concurrently: ^7.2.2
@@ -12783,13 +12694,6 @@ __metadata:
1278312694
languageName: node
1278412695
linkType: hard
1278512696

12786-
"reselect@npm:^4.1.7":
12787-
version: 4.1.8
12788-
resolution: "reselect@npm:4.1.8"
12789-
checksum: a4ac87cedab198769a29be92bc221c32da76cfdad6911eda67b4d3e7136dca86208c3b210e31632eae31ebd2cded18596f0dd230d3ccc9e978df22f233b5583e
12790-
languageName: node
12791-
linkType: hard
12792-
1279312697
"resolve-cwd@npm:^3.0.0":
1279412698
version: 3.0.0
1279512699
resolution: "resolve-cwd@npm:3.0.0"
@@ -12836,7 +12740,7 @@ __metadata:
1283612740
languageName: node
1283712741
linkType: hard
1283812742

12839-
"resolve@npm:^1.10.0, resolve@npm:^1.14.2, resolve@npm:^1.22.4, resolve@npm:^1.22.8":
12743+
"resolve@npm:^1.10.0, resolve@npm:^1.14.2, resolve@npm:^1.22.4":
1284012744
version: 1.22.8
1284112745
resolution: "resolve@npm:1.22.8"
1284212746
dependencies:
@@ -12849,7 +12753,7 @@ __metadata:
1284912753
languageName: node
1285012754
linkType: hard
1285112755

12852-
"resolve@patch:resolve@^1.10.0#~builtin<compat/resolve>, resolve@patch:resolve@^1.14.2#~builtin<compat/resolve>, resolve@patch:resolve@^1.22.4#~builtin<compat/resolve>, resolve@patch:resolve@^1.22.8#~builtin<compat/resolve>":
12756+
"resolve@patch:resolve@^1.10.0#~builtin<compat/resolve>, resolve@patch:resolve@^1.14.2#~builtin<compat/resolve>, resolve@patch:resolve@^1.22.4#~builtin<compat/resolve>":
1285312757
version: 1.22.8
1285412758
resolution: "resolve@patch:resolve@npm%3A1.22.8#~builtin<compat/resolve>::version=1.22.8&hash=c3c19d"
1285512759
dependencies:

0 commit comments

Comments
 (0)