Playing around with yarn workspaces, cra, crna, expo - based on Ben Awad's video tutorial and this Medium article.
!! Make sure you go over security warnings if basing a new project on this code !!
create-react-native-appis now completely replaced by expo - so I just use it.- I made
./packages/web/config-overrides.jsmore lean. - I make my own change to
App.jsin the web and app (because the template has changed withreact-scripts@2.0.0). - I change the "main" entery in
./packages/app/package.json. - I do not change
app.jsonat all.
- Create the packages directory (
mkdir packages). - In the directory, create the web and app packages:
cd packagesnpx create-react-app webyarn global add expo-cli && expo init- I chose to name the project app to fit with the tutorial video.
- Create the root
package.jsonwith the content{ "private": true, "workspaces": [ "packages/*" ] }. - Create a
commonfolder in packages. - Add to common a
package.jsonfile:{ "name": "@yarn-workspace-cra-crna-expo-tut/common", "version": "1.0.0", "main": "index.js", "license": "MIT" }. - Add to common a basic
index.jsfile containing an exported function (export const add = (a,b) => a + b;).
- Add to devDependencies to the web app:
cd packages/webyarn add --dev react-app-rewire-yarn-workspaces react-app-rewired
- In
./packges/web/packages.jsonchange the four scripts usingreact-scriptsto usereact-app-rewiredinstead.- - "start": "react-scripts start",
- + "start": "react-app-rewired start",
- - "build": "react-scripts build",
- + "build": "react-app-rewired build",
- - "test": "react-scripts test",
- + "test": "react-app-rewired test",
- - "eject": "react-scripts eject"
- + "eject": "react-app-rewired eject"
- Add a file named
./packges/web/config-overrides.jsfile, containing the following code:module.exports = require("react-app-rewire-yarn-workspaces"); - Add
"@yarn-workspace-cra-crna-expo-tut/common": "1.0.0"to the dependencies in./packges/web/packages.json. - Change
./packges/web/src/App.jsto use the function from thecommonpackage.- First, add
import { add } from '@yarn-workspace-cra-crna-expo-tut/common';to the file. - Then change
Learn ReacttoLearning React is like learning 1 + 1 = {add(1, 1)}.
- First, add
- Run
yarn startfrom within the web package to confirm this is working!
- In
./packages/app/package.jsonadd"name": "@yarn-workspace-cra-crna-expo-tut/app", "version": "1.0.0",. - In the
apppackage directory, runyarn add --dev crna-make-symlinks-for-yarn-workspaces. - Add a file called
./packages/app/link-workspaces.jswith this:require('crna-make-symlinks-for-yarn-workspaces')(__dirname);. - Add prestart script to
./packges/app/package.json:"prestart": "node link-workspaces.js",. - Add
"@yarn-workspace-cra-crna-expo-tut/common": "1.0.0"to the app package dependencies. - Change
App.jsin the app package to use the function from thecommonpackage.- First, add
import { add } from '@yarn-workspace-cra-crna-expo-tut/common';to the file. - Then add
<Text>TEST: 1 + 1 = {add(1,1)}</Text>to the App'sViewcomponent.
- First, add
- In the root project, create a file named
App.jswith the contentsimport App from './packages/app/App';export default App;
- Run
yarn startfrom within the web package to confirm this is working!