A set of tools to help you build your React Native project in a Yarn Workspaces monorepo.
Check out the "Running React Native everywhere" tutorial and the react-native-universal-monorepo
to see them in action.
Install the package using npm or Yarn.
# Using npm
npm install -D react-native-monorepo-tools
# Using yarn
yarn add -D react-native-monorepo-tools
Return Metro tools to make it compatible with Yarn workspaces.
const { getMetroTools } = require("react-native-monorepo-tools");
const metroTools = getMetroTools();
console.log(metroTools.watchFolders);
// ↪ [
// "/Users/me/my-monorepo/node_modules/"
// "/Users/me/my-monorepo/packages/workspace-a/",
// "/Users/me/my-monorepo/packages/workspace-b",
// ]
console.log(metroTools.blockList);
// ↪ [
// /^((?!workspace-a).)*\/node_modules\/react\/.*$/,
// /^((?!workspace-a).)*\/node_modules\/react-native\/.*$/,
// ]
console.log(metroTools.extraNodeModules);
// ↪ {
// "react": "/Users/me/my-monorepo/packages/workspace-a/node_modules/react",
// "react-native": "/Users/me/my-monorepo/packages/workspace-a/node_modules/react-native"
// }
cwd
: Optional. Current working directory. Defaults toprocess.cwd()
.reactNativeAlias
: Optional. Alias for thereact-native
lib (e.g.,react-native-macos
).libNamesOnly
: Optional. Return the nothoist list as library names instead of paths? Defaults tofalse
.
watchFolders
: Directories watched by metro.blockList
: List of regexes resolving to paths ignored by metro.extraNodeModules
: List of required modules outside of the workspace directory.
Return Webpack tools to make it compatible with Yarn workspaces.
const { getWebpackTools } = require("react-native-monorepo-tools");
const webpackConfig = require('./webpack.config.js')
const webpackTools = getWebpackTools();
console.log(webpackTools.addNohoistAliases(webpackConfig));
// ↪ alias: {
// "react": "/Users/me/my-monorepo/packages/workspace-a/node_modules/react"
// "react-native": "/Users/me/my-monorepo/packages/workspace-a/node_modules/react-native-web"
// }
console.log(webpackTools.enableWorkspacesResolution(webpackConfig));
// ↪ adds to babel-loader each workspace
cwd
: Optional. Current working directory. Defaults toprocess.cwd()
.
enableWorkspacesResolution(webpackConfig)
: Updates a webpack config to allow importing from external workspaces.addNohoistAliases(webpackConfig)
: Updates a webpack config to ensure nohoisted libraries are resolved from this workspace.
Return the Metro Android assets resolution fix.
Metro's bundler has an issue with assets resolution on Android when importing assets from paths outside of the project's root directory.
To fix it, we can patch metro's publicPath
and enhanceMiddleware
to allow reading from n
depths below the project directory.
For more info, see this metro comment: facebook/metro#290 (comment)
const {
getMetroAndroidAssetsResolutionFix,
} = require("react-native-monorepo-tools");
const metroAndroidAssetsResolutionFix = getMetroAndroidAssetsResolutionFix();
console.log(metroAndroidAssetsResolutionFix.publicPath);
// ↪ "/assets/dir/dir/dir/dir"
console.log(metroAndroidAssetsResolutionFix.applyMiddleware);
// ↪ Function that applies the patch middleware to metro.
cwd
: Optional. Current working directory. Defaults toprocess.cwd()
.
publicPath
: Metro'spublicPath
.applyMiddleware
: Function that applies the patch middleware to metro.
Return the root path of this monorepo.
Usage:
const { getMonorepoRoot } = require("react-native-monorepo-tools");
const monorepoRoot = getMonorepoRoot();
console.log(monorepoRoot);
// ↪ /Users/me/my-monorepo',
Available params:
cwd
: Optional. Current working directory. Defaults toprocess.cwd()
.
Return the workspaces of this monorepo (as paths).
const { getWorkspaces } = require("react-native-monorepo-tools");
const workspaces = getWorkspaces();
console.log(workspaces);
// ↪ [
// "/Users/me/my-monorepo/packages/workspace-a",
// "/Users/me/my-monorepo/packages/workspace-b",
// ]
cwd
: Optional. Current working directory. Defaults toprocess.cwd()
.
Return the current workspace path.
const { getCurrentWorkspace } = require("react-native-monorepo-tools");
const currentWorkspace = getCurrentWorkspace();
console.log(currentWorkspace);
// ↪ "/Users/me/my-monorepo/packages/workspace-a"
Return the nothoist libraries of this monorepo (as paths or names).
const { getNohoist } = require("react-native-monorepo-tools");
const nohoist = getNohoist();
console.log(nohoist);
// ↪ [
// "/Users/me/my-monorepo/packages/workspace-a/react",
// "/Users/me/my-monorepo/packages/workspace-a/react-native",
// "/Users/me/my-monorepo/packages/workspace-b/react",
// "/Users/me/my-monorepo/packages/workspace-b/react-native",
// ]
cwd
: Optional. Current working directory. Defaults toprocess.cwd()
.currentWorkspaceOnly
: Optional. Return the nothoist list for the current workspace only? Defaults tofalse
.libNamesOnly
: Optional. Return the nothoist list as library names instead of paths? Defaults tofalse
.
Each contribution is welcome!
Please, check the contributing guidelines.
React Native Monorepo Tools is released under AGPL-3.0 - GNU Affero General Public License v3.0.
- Modification and redistribution allowed for both private and commercial use.
- You must grant patent rigth to the owner and to all the contributors.
- You must keep it open source and distribute under the same license.
- Changes must be documented.
- Include a limitation of liability and it does not provide any warranty.
THIS TOOL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. For the full warranty check the LICENSE.