Have you ever faced troubles when configuring a fresh react-native app to test your framework locally? These utility functions will help you to configure metro and babel to run your monorepo-structured framework and test it's modules without a need to publish them.
Simply copy and paste the files below into your project root and follow the guides explained in both of them.
- Install both framework and application dependencies with
npm i && ( cd ./path-to/framework && npm i )
- Install
babel-plugin-module-resolver
withnpm i -D babel-plugin-module-resolver
- Copy and paste babel.resolver.js and metro.resolver.js to your project root
const monorepoResolver = require('./babel.resolver');
const { root, alias } = monorepoResolver({
path: '../path-to/monorepo-project',
modules: [
'subpath-to/module-1',
'subpath-to/module-2',
// ...etc
],
});
const moduleResolverConfig = {
root,
alias: {
...alias,
// additional aliases
}
};
module.exports = function (api) {
api.cache(true);
const presets = [
'module:metro-react-native-babel-preset',
];
const plugins = [
['module-resolver', moduleResolverConfig],
];
return { presets, plugins };
};
const monorepoResolver = require('./metro.resolver');
const { extraNodeModules, watchFolders } = monorepoResolver(structure);
module.exports = {
resolver: {
extraNodeModules: {
...extraNodeModules,
// any other node modules if needed
},
},
watchFolders: [
...watchFolders,
// any other watch folders if needed
],
};