Support absolute paths with packages/ folder
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:
- In addition to
src, you can create a special top-level folder calledpackages. - Inside
packages, you can create folders likeapp,stuff,lol, and they all “see” each other so you can import things fromapp/whatever.jsorlol/wow.js. You can also import any packages fromsrc(but not vice versa). The entry point is stillsrc/index.js. - We won’t add any magic handling that breaks Node resolution mechanism. Instead, whenever you
npm start,npm test, ornpm run build, we will run a utility that creates symlinks fromnode_modulesof 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.
package.json
public/
src/
index.js # can import app/banana.js or harry-potter/wand.js
packages/
app/
banana.js # can import harry-potter/wand.js
harry-potter/
wand.js # can import app/banana.js
node_modules/
app -> packages/app
harry-potter -> packages/harry-potter
other-deps
Am I missing why this would be a bad idea?
We've been using linklocal for this type of thing.
https://github.com/timoxley/linklocal
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 harry-potter to npm? So that I could use it another package as well? Is that the idea?
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:
import 'path/to/banana.js' instead of the brittle import '../../banana.js'.
Absolute paths to your app's files instead of relative paths.
The current workaround is to add an environment variable before running commands:
NODE_PATH=src/ && npm run start
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 node_modules/. Though honestly I can't help but wonder if that's an edge case.
@gaearon Why not just point to src/ instead of adding packages? I'm sure there is a great reason that's just not clear to me yet.
Why not just point to src/ instead of adding packages? I'm sure there is a great reason that's just not clear to me yet.
I was wondering this to, you could also symlink any directory in src. And then still keep src/index.js as entry point. No?
Why not just point to src/ instead of adding packages? I'm sure there is a great reason that's just not clear to me yet.
@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 packages/.
How does this help? Can anyone who will benefit from this comment?
Please refer to discussion in https://github.com/facebookincubator/create-react-app/issues/636.
Why not just point to src/ instead of adding packages? I'm sure there is a great reason that's just not clear to me yet.
It is too brittle in my experience. People won’t realize what is happening unless there is some explicit opt-in, and I think packages is a good way to signal the intention, as well as to make people think twice about how to name root folders (instead of accidentally aliasing something that already exists in node_modules).
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.
Is there a solution that would keep the same folder structure people have already built with create-react-app?
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 NODE_PATH. Or you could copy all top-level folders from src into packages and have src/index.js re-export whatever package you like, thus providing a quick migration path.
It is too brittle in my experience. People won’t realize what is happening unless there is some explicit opt-in, and I think packages is a good way to signal the intention, as well as to make people think twice about how to name root folders (instead of accidentally aliasing something that already exists in node_modules).
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 lib folder as a regular installed package where the name of the package is gotten from how you call the app in package.json. Say the app is called my-app then:
package.json # name: my-app
public/
src/
index.js # can import my-app/fruits/banana.js, my-app/harry-potter/wand.js or my-app/dom.js
lib/
dom.js # top level file, can be imported as `my-app/dom.js`
fruits/
banana.js # can import my-app/harry-potter/wand.js
harry-potter/
wand.js # can import my-app/fruits/banana.js
node_modules/
my-app/
dom.js -> lib/dom.js
fruits -> lib/fruits
harry-potter -> lib/harry-potter
other-deps
The adventage of this, is that you don't have to worry about how you would call the directory in the lib (or packagedirectory).
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.
How does this help? Can anyone who will benefit from this comment?
Please refer to discussion in #636.
That issue is where I came from. This solves the problem stated in the original request terribly at best.
Take the f8app, stop using @providesModule and apply this solution. Can you imagine ending up with a better structure?
terribly at best.
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 packages/. The problem is that we want to import almost everything using absolute paths. If I were to use this pattern, I would have to move my actions, components, helpers, 18n, theme.js, env.js and config.js to somewhere in packages. That's my whole app!
That's my whole app!
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 packages and it would work, but then src would be almost empty. What's next? We remove src?
Or are you expecting me to make every component a package? Am I supposed to have a thousand React components laid flat in packages?
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?
- A developer would and should expect a directory called
node_modulesto be managed by npm. - CRA targets beginners. What happens when a beginner faces with a problem and reads on SO that they should try removing
node_modulesand doing annpm i? The originalnode_modulesmight even be hidden by the editor. "Why do they ask me to remove my code?" - Most editors, libraries and
.gitignores do not follow the best practice and treat all directories namednode_modulesjust like the one at the root. Even React Native has this wrong. (It should've been/node_modules/and notnode_modules/.)
You should just treat node_modules as a reserved name.
@AlicanC
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.