strothj / react-app-rewire-typescript-babel-preset

Add TypeScript support to Create React App using @babel/preset-typescript
https://react-app-rewire-typescript-babel-preset.netlify.com
MIT License
52 stars 6 forks source link

Correctly resolve entry point when inside monorepo #2

Closed detrohutt closed 6 years ago

detrohutt commented 6 years ago

I'm using this inside a lerna repo with yarn workspaces. I have hoisting enabled so the folder where my CRA app resides doesn't contain a node_modules folder. This breaks your current implementation because it's looking for src/index.tsx inside of the node_modules at the root of my lerna repo (because that's where react-scripts was hoisted to).

In other words, it's trying to resolve: <lernaRoot>/src/index.tsx The file is actually located at: <lernaRoot>/packages/<cra-app>/src/index.tsx

I've used require.resolve() because it allows to try resolving from multiple paths. I've put the original path first, so this shouldn't be a breaking change.

I've used process.cwd() as the fallback path because that is the directory where the react-app-rewired command was run, which in a standard setup would be the CRA app's root folder. This will also work in cases where there are multiple CRA apps in one monorepo (and hence potentially multiple react-scripts folders)

I tested this with my setup and it worked fine.

I'm working on a PR to CRA to remove the hard-coded '.js' extension on the entry point so that this whole part of the code would be unnecessary. At that point, just adding ".ts", ".tsx" to webpack's resolve.extensions (which you're already doing) would be sufficient.

I'll make another PR here if/when I'm able to get that merged at CRA. In the meantime, this patch should suffice for people using any type of monorepo structure.

Let me know if you have any questions or concerns.

strothj commented 6 years ago

Excellent work again, thank you!

strothj commented 6 years ago

Should be good to go on NPM

detrohutt commented 6 years ago

Thank you for the amazingly fast turnaround time! :smiley:

strothj commented 6 years ago

No problem :-) I work from home so I'm always around haha