-
-
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 absolute paths with packages/ folder #741
Comments
We've been using linklocal for this type of thing. |
What problem would this solve? I see lerna as being useful as for example now users can easily use the eslint config of create-react-app in any package they want. Could I publish this package |
How does this help? Can anyone who will benefit from this comment? |
This seems like an acceptable solution to me, if well documented. It sounds like under this proposal that it would make sense to put most domain models under packages/ and so in an OO system, one would not have much under src/, though tests/ should still go under src/ and import domain models under test from packages. That's a clear change from what we're used to thinking, that 'everything goes under source'. Am I holding it right? Also, it sounds like things under src/ would not be accessible via absolute paths, only things under packages/. This should be made really clear in the documentation... We have a folder called src/testHelpers. Implementing this solution would mean moving it to packages (in order for src/banana.tests.js to be able to import a testHelper file by absolute path). So I guess in the final analysis, if anyone has been using the NODE_PATH=src workaround, to implement this solution, one would have to reorganize all the folders and rewrite the imports. Is there a solution that would keep the same folder structure people have already built with create-react-app? |
The problem we're trying to solve is: The current workaround is to add an environment variable before running commands: But apparently some people have problems with that workaround, perhaps because of a conflict between the name of their file and the names of folders and files under |
@gaearon Why not just point to |
I was wondering this to, you could also symlink any directory in src. And then still keep |
@mandysimon88 that is what we are talking about. Every file in your project can refer to some other file. You need absolute paths in every file so this solution (in some sense) forces you to put everything in |
Please refer to discussion in #636.
It is too brittle in my experience. People won’t realize what is happening unless there is some explicit opt-in, and I think I also don’t want beginners to do this, which is why I want this feature to be opt-in. IMO it’s useful in larger projects, not from the first day.
You wouldn’t have to change anything. This feature would be strictly additive. You could migrate to it one helper at a time, if you’d like, or even keep using |
Just some reference how an other language solved this problem. Because what Dart does is something very similar as you are proposing @gaearon. It will see anything in the
The adventage of this, is that you don't have to worry about how you would call the directory in the |
Should you go ahead and do this, you should configure eslint-plugin-import to resolve files correctly. I see that the rules are currently disabled, but that's something that should be done at some point. |
That issue is where I came from. This solves the problem stated in the original request terribly at best. Take the f8app, stop using |
This is a pretty strong statement. Can you help me understand what is “terrible” about this proposal? |
To clarify, it is terrible for being a solution to the original request in #636. This solution basically suggests that we should move stuff we want to import using absolute paths to a directory in |
That's the whole point! Structure your app as packages instead of a monolith |
@Pajn it already is. If I had to use this pattern, I'd just move some directories to |
Or are you expecting me to make every component a package? Am I supposed to have a thousand React components laid flat in |
Could this be done without copying files by putting packages in src/node_modules (see #607) and checking for conflicts? |
@modernserf yes, but don't you think that's a bit ugly?
You should just treat |
Wouldn't having a single folder called "app" in packages satisfy your use case? You could even have src/index.js reexport app/index.js and then any module is addressable as app/whatever. Of course it's a bit more typing than if you give implicit namespaces but I think explicitness is favourable in this particular case. |
Then it would be mostly empty for you. I don't see a problem here. You choose a convention where everything lives in an absolutely addressable package. That's fine but then people new to your codebase need to know where the entry point is. So it makes sense to me that src stays at top level because that'll be the first place people look (and they can see what you reexport if you really decide to move everything into packages). |
I don't like it, but sure. Which official Facebook projects can we expect to see dogfooding this pattern? |
It's a common misunderstanding: |
As you probably know, Facebook is using Haste everywhere in its product code. You might have noticed it is not very popular in the open source community. Some things that work well for Facebook also require a lot of infrastructure that others don’t have. Create React App explicitly does not follow everything the way Facebook does it. In fact, it would not exist if it followed the usual dogfooding principle because, for example, Facebook doesn’t actively use Webpack and instead has super powerful development servers in the cloud that compile the code. The situation in the open source community is just different, and with this project we tried to break our usual principles and go where people are. I think the success of this project speaks that breaking the rules is a good idea sometimes. That said, project structure with |
I'm a big fan of this approach. It encourages encapsulation of like logic for a local package used in the For example I have a Further it also creates a structure such that if you wanted to open source components inside of Big fan of this approach. |
Coming from the rather large React codebase of wp-calypso where we heavily rely on absolute imports, I think this is a great idea. Although not necessary at all for a small project, when a project becomes large, absolute imports have a huge value and can save a lot of developer time. Having this be an option in CRA may help provide a way for new developers to encounter the concept in a friendly way with a suggested implementation rather than trying to discern how to do it themselves (or giving up since there is no "right way"). |
We’re going to keep supporting |
For those using
|
Using the above accepted solution I still can't achieve the following: My component structure is the following:
|
@jasan-s as support questions probably belong on StackOverflow, can you paste your question there and include the contents of your index.js file? |
@jasan-s I think that should work if the contents of your 'components/index' is correct ... Haven't tried it out with import, only with require, but if your index.js successfully imports the components from the subpaths and then exports them again using a "{}" then I don't see why it wouldn't work? Maybe it's the difference between import and require ... |
@leob I get an error using it with import.
I'll try stack overflow, see if i can get some help there as well. |
@jasan-s try
|
that works :) thanks @will-stone |
Wow, {default as ...} syntax that's new for me ... like I said I tried it with require only. |
Don't know about you guys but I much prefer using
In any component you can import like so:
The |
@Ehesp I didn't think it was possible to use babel plugins with an un-ejected CRA, if that's the case then this wouldn't be a solution users could add, but maybe something that could be considered by the CRA team to be put directly in to react-scripts? Is it cross platform compatible? |
@will-stone Ah yes sorry this is an ejected app! |
Having checked out VueJS for a while, I'm all in favour of using the "@" prefix to denote the |
I was able to use paths from
to |
@alexeyraspopov is there technically any benefit to using
over
? |
My bad, I apologize, it should be |
I think the question still remains: do we need to add |
A quick test shows that you can just set |
Was just about to comment that lol. It does break VSCode's cmd+click feature, but I'm fine with that for shorter imports. |
The issue with symlinking |
@Vanuan One alternative is to create a folder, say As a convention, I started all the symlinks with an underscore, which is otherwise forbidden in NPM so there would be no conflict with npm packages, and it made it clear to the developer that those were virtual folders, not dependencies that could be found in The advantage was that it was meant to test several architectures so I could swap various alternatives by changing the folders the symlinks pointed to, for example, my Since this folder with the symlinks is along the other source files, exclude it from linting, testing or any other utilities that my browse the folders, otherwise you might get in a loop. |
I still can't make this work properly. I have in This fails:
This works:
Folder structure:
|
@alexdevero setting |
@alexdevero setting |
That worked. Thank you very much @jasan-s. Is it somehow possible, without ejecting, to enable |
We’ve been using Lerna in Create React App, and I really like its approach. I think we should support a similar workflow (even without Lerna itself) for the “absolute paths” feature.
I imagine it working like this:
src
, you can create a special top-level folder calledpackages
.packages
, you can create folders likeapp
,stuff
,lol
, and they all “see” each other so you can import things fromapp/whatever.js
orlol/wow.js
. You can also import any packages fromsrc
(but not vice versa). The entry point is stillsrc/index.js
.npm start
,npm test
, ornpm run build
, we will run a utility that creates symlinks fromnode_modules
of the root project to every folder inpackages
. It reports a hard error if there is a conflict. This means the authors can add server rendering after ejecting without scratching their heads, and that all the tooling assuming Node resolution mechanism keeps working.Am I missing why this would be a bad idea?
The text was updated successfully, but these errors were encountered: