Skip to content

Instantly share code, notes, and snippets.

@huntie
Last active July 10, 2023 01:53
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huntie/85ea491763b444bfa1bdc8e997fc2765 to your computer and use it in GitHub Desktop.
Save huntie/85ea491763b444bfa1bdc8e997fc2765 to your computer and use it in GitHub Desktop.
Snippets accompanying my article "A concise guide to configuring React Native with Yarn Workspaces" on Medium
/**
* Metro Bundler configuration
* https://facebook.github.io/metro/docs/en/configuration
*
* eslint-env node, es6
*/
const exclusionList = require('metro-config/src/defaults/exclusionList');
const getWorkspaces = require('get-yarn-workspaces');
const path = require('path');
function getConfig(appDir, options = {}) {
const workspaces = getWorkspaces(appDir);
// Add additional Yarn workspace package roots to the module map
// https://bit.ly/2LHHTP0
const watchFolders = [
path.resolve(appDir, '..', 'node_modules'),
...workspaces.filter(
workspaceDir => !(workspaceDir === appDir),
),
];
return {
watchFolders,
resolver: {
blockList: exclusionList([
// Ignore other resolved react-native installations outside of
// myapp-native - this prevents a module naming collision when mapped.
/^((?!myapp-native).)+[\/\\]node_modules[/\\]react-native[/\\].*/,
// Ignore react-native-svg dependency in myapp-ui, mapped below.
// react-native-svg must only be included once due to a side-effect. It
// has not been hoisted as it requires native module linking here.
// http://bit.ly/2LJ7V4b
/myapp-ui[\/\\]node_modules[/\\]react-native-svg[/\\].*/,
]),
extraNodeModules: {
// Resolve all react-native module imports to the locally-installed version
'react-native': path.resolve(appDir, 'node_modules', 'react-native'),
// Resolve additional nohoist modules depended on by other packages
'react-native-svg': path.resolve(
appDir,
'node_modules',
'react-native-svg',
),
// Resolve core-js imports to the locally installed version
'core-js': path.resolve(appDir, 'node_modules', 'core-js'),
},
},
};
}
module.exports = getConfig(__dirname);
{
"name": "myapp-native",
"version": "1.0.0",
"private": true,
"scripts": {
"postinstall": "jetify",
"start": "react-native start",
"android": "react-native run-android",
"ios": "react-native run-ios",
"pods": "cd ios; pod install"
},
"workspaces": {
"nohoist": [
"@react-native-community/async-storage",
"react-native",
"react-native/**",
"react-native-dev-menu",
"react-native-svg",
"jetifier"
]
},
"dependencies": {
"@react-native-community/async-storage": "^1.6.1",
"myapp-settings": "1.0.0",
"myapp-ui": "1.0.0",
"react": "16.9.0",
"react-native": "0.61.5",
"react-native-dev-menu": "^4.0.0",
"react-native-svg": "^9.12.0"
},
"devDependencies": {
"get-yarn-workspaces": "^1.0.2",
"metro-config": "^0.56.0"
}
}
@steveluscher
Copy link

steveluscher commented Jun 29, 2022

This needs updating for the new metro-config/src/defaults/exclusionList module and the blocklist property in Metro's config.

@huntie
Copy link
Author

huntie commented Jun 30, 2022

Thanks for pointing this out @steveluscher, now updated. Funnily enough I now work on Metro 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment