Last active
July 10, 2023 01:53
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for pointing this out @steveluscher, now updated. Funnily enough I now work on Metro 😄