create-react-app-typescript
create-react-app-typescript copied to clipboard
Support Lerna/Yarn Workspaces
Is this a bug report?
No.
As discussed in https://github.com/facebook/create-react-app/issues/1333 and merged in https://github.com/facebook/create-react-app/pull/3741, [email protected] has support for monorepos. I'd like to make use of that functionality in my create-react-app-typescript projects, want to make sure this doesn't get lost as time goes on :) Looks like 2.0.0 hasn't been released yet, so we can wait on that.
I've implemented a workaround using tsc's baseUrl which CRA-ts already supported.
https://github.com/untsamphan/cra-ts-monorepo-example
Need to eject (only) if you want tsc/webpack to grab .ts from the local packages.
Steps
-
Initial boilerplate from create-react-app-typescript in root/webapp commit.
-
Create yarn workspace commit.
-
Move CRA tsconfig.json to root to be shared with other local packages commit 1, commit 2.
-
Implement a trivial local packages
root/packages/mymaincommit. Run yarn now so it'll create symlink to mymain inroot/node_modules. Then we canimport from 'mymain'from anywhere. -
Make the CRA app use the new local packages commit. Now the CRA app will tsc compile correctly (because we have index.ts at mymain root). But when
yarn startit'll fail in browser because mymain isn't built. To fix this we can tsc in mymain to build the package and the app will run successfully. However, when wego to definitionto a symbol in mymain, it'll goto a .d.ts. -
To achieve goal 3 (go to definition -> .ts), we configure tsconfig.json
baseUrlto directly reference local packages. Since webpack won't bundle code outsidewebapp/src, and jest won't find the packages, we have to eject to configure them. commit -
Simple webpack config hack to allow it to bundle code outside
webpack/src. This to achieve goal 3. commit. Don't forget to deletebuildin local packages, because otherwise everyone will usebuild/index.*(per NPM spec) instead ofindex.tsat the local package root (a TS-specific behavior). -
Simple jest config hack to make jest inside webapp also run all tests in local packages. commit
@untsamphan Thank you for your great instructions. This is very much helpful for me. However I'd not like to eject and manage complicated configurations so I made another version using react-app-rewired.
- https://github.com/watiko/cra-ts-monorepo-example/tree/use-rewired
- https://github.com/watiko/cra-ts-monorepo-example/commit/3eeac750c357627279119f9e9ec1bc84ba5cf377
Thx again!
You can try to use https://www.npmjs.com/package/@hieunv/react-scripts. See sample https://github.com/react-chartjs/core
First, You create a project with
cd packages
npx create-react-app my-app --scripts-version=@hieunv/react-scripts