-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support importing files from outside of the root folder #5563
Comments
My best experience is w/ using yarn workspaces. |
It was made intentionally to prevent users from importing something wrong. |
I personally am having one hell of a time getting CRA to work with yarn workspaces. With the following setup: packages/graphql - all my graphql fragments, queries, mutations, subscriptions
packages/controllers - shared JSX code between web/native
packages/web - cra 2.0
packages/native - react native CRA says, oh you want to do graphql just use CRA also doesn't support monorepo so I have to setup webpack, parcel, or something similar to bundle my JSX, but also to run the babel macros and have it all compiled so react can import it. It just seems to defeat the purpose of CRA, I want my imported code to run under the same config options that CRA has and not have to duplicate it all and have a separate bundler running in If I don't use a monorepo to share my code, then I am publishing a private npm package every time I make a change. |
@miraage I know it was intentional, and there were reasons. I'd like to see those reasons revisited given the inordinate amount of hacks it requires to work with multiple projects, which I presume was not fully appreciated at the time the decision was made. A config option to let projects opt out of this feature would be sufficient, where the majority of people still get the same behavior but power users do not have arbitrary barriers to good hygiene put in their way |
You don't have to publish the package. In your package.json just specify the local path to module |
so I currently have a |
@eddiemonge a few points about multiple repos: |
This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs. |
OK, here's some recent activity. This feature still makes sense and prevents clean usage of more than one CRA project per code repo. Any movement on this? |
This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs. |
OK, here's some recent activity. This feature still makes sense and prevents clean usage of more than one CRA project per code repo. Any movement on this? |
Any improvements or suggestions in here? |
This worked for me. Given this structure:
Add this entry to the
Now you can import common stuff in the import Thing from 'common/thing';
import OtherThing from 'other-folder'; It's still a bit of a work-around, but it's the cleanest solution I've managed so far. |
@humancatfood amazing, that works!! Thanks so much! |
What about just using npm link? I have an external library that i'd like to actively develop inside the consuming CRA, but I have to restart the app every time to see the changes. Angular CLI has had a flag when running in dev mode for a while... |
@humancatfood, @shaneosullivan How can you convert these outside components to ES5? |
@ianqueue I'm sure you can get something more scalable working with npm links, but I never managed to get my head around those and the @sezeregrek I don't know.. same as everything else? |
Hi again @ianqueue & @shaneosullivan, so I did get my head around this in the meantime (or at least I think I did..) You don't need to
This should create an entry in
You can now import it in your cra code: import bla from 'your-external-packages-name'; If you make changes to your external package it should propagate to the cra app and without you having to reload. |
Does the method described by @humancatfood require that code in the |
Nope, it'll all get compiled together by the CRA. What I suggested simply lets you import stuff from another location than before, it doesn't change anything about what happens to that stuff after that, ie compiling, babel-ing, etc. |
@humancatfood could you provide dummy repository example of your setup ? |
It's not like I don't have an actual job that needs doing, but ok, here you go: https://github.com/humancatfood/outside-imports The crucial step is in this commit: And here it is deployed: https://humancatfood.github.io/outside-imports/ After playing around with this for a bit longer, I think using npm link is actually better though |
@humancatfood right. Thanks for taking the time to show what you meant.
If you're importing e.g shared React components, then yes. @humancatfood 's example doesn't handle that, unfortunately. |
@waynebloss Apologies, I was thinking of minification, etc. If you want to import .jsx, then yes, you need to precompile it. Or eject and define your own loaders. Or write React in standard js of course: https://github.com/humancatfood/outside-imports/commit/9589f66b53fca9c3688376735aa6e561452a4976 |
This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs. |
This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue. |
Is this a bug report?
No
The current stance of CRA to disallow importing files from outside the root folder of an app forces the most involved users, those with multiple projects, to jump through hoops that should be unnecessary in order to share code, both components and utils.
Current options are
If we can allow CRA project to just act as another non-special folder in a larger mono-repo, and import files from outside it's root folder, it would greatly improve the experience of working with many CRA projects while keeping all the benefits of not ejecting.
The text was updated successfully, but these errors were encountered: