... in favor of https://github.com/bradfordlemley/cra-monorepo-examples
This repo demonstrates a proposal (3436) for a source sharing feature in create-react-app.
See How to use in your own app section for instructions.
monorepo |--cra-app1 |--package.json |--src |--cra-app2 |--package.json |--src |--shared |--comp1 |--comp2
monorepo |--cra-app1 |--package.json: srcPaths: [“../shared”] |--src |--file1.js: import comp1 from ‘comp1’ ← ok, shared/comp1 |--file2.js: import comp1 from ‘../../shared/comp1’ ← naughty, fails |--cra-app2 |--package.json: srcPaths: [“../shared”, "./packages"] |--src |--file1.js: import comp1 from ‘comp1’ ← ok, shared/comp1 |--file2.js: import comp3 from ‘comp3’ ← ok, packages/comp3 |--packages |--comp3 |--shared |--comp1 |--comp2
- srcPaths are specified in the CRA app's package.json.
- srcPaths are treated the same as /src
- Transpiled using same config (.babelrc under srcPath is not honored)
- Do not have their own dependencies (ie. do not have their own node_modules)
- All dependencies must be included in app’s package.json / node_modules
- All jest tests are included
- Modules under srcPaths are included in CRA app via absolute imports.
- Including via relative import fails, same as standard CRA
- Overlapping srcPaths are not allowed, e.g. [“../shared”, “../shared/comp1”]
- This is to avoid confusion, to keep resolution something easy to reason about.
- This is not yet enforced, but it will be in the future, so don't do it.
- srcPaths are paths relative to app root (where package.json is) ... they can be absolute paths, too, but relative paths are the obvious choice for monorepos.
- Resolve order is same as order of srcPaths.
- https://github.com/bradfordlemley/create-react-app/tree/feature-srcPaths
- https://github.com/bradfordlemley/create-react-app/commit/709dc407e2855f5dbdd982bde19dc0141e0d8e7c
- git clone https://github.com/bradfordlemley/create-react-app
- cd create-react-app
- git checkout feature-srcPaths
- cd packages/react-scripts
- npm install <-- install react-scripts dependencies
- npm link <-- tell your npm that you might want to use this version of react-scripts somewhere
- Install react-scripts with srcPaths support as described above
- git clone https://github.com/bradfordlemley/cra-share
- Run app 1
- cd cra-share/cra-app1
- npm install
- npm link react-scripts <-- tell npm you want to use the react-scripts that was installed above
- npm start <-- run app 1
- npm test|build|anything else that you can normally do with a create-react-app
- Run app 2
- cd cra-share/cra-app2
- npm install
- npm link react-scripts <-- tell npm you want to use the react-scripts that was installed above
- npm start <-- run app 2
- npm start|test|build|anything else that you can normally do with a create-react-app
Note: your app needs to be compatible with react-scripts v1.0.17, because this react-scripts w/ srcPaths support is forked from v1.0.17.
-
Install react-scripts with srcPaths support as described above
-
Open console at your app (create new app using standard create-react-app tool or use your existing app)
-
npm link react-scripts <-- tell npm you want to use the modified react-scripts that was installed above
If you run
npm install
again in your app, you need to runnpm link react-scripts
again asnpm install
will wipe out the link -
Add srcPaths to your package.json
-
Put your shared modules in one of the srcPaths
-
Import your shared modules from your app
-
To revert to your app to use previous react-scripts version, run
npm install
in your app, that will wipe out the link to the react-scripts with srcPaths.
- Update ModuleScopePlugin to handle srcPaths in same way it handles appSrc (fail if relative import outside srcPath)
- Check for overlapping srcPaths