Sharing components between apps, "create-react-component"
Note from Maintainers: see https://github.com/facebookincubator/create-react-app/issues/1492#issuecomment-358170489 for the latest thinking on this.
The Problem
Say I have two sibling CRA apps, Foo and Bar. What is the best practice for sharing a common component, Baz between the two apps?
Scope
It would make sense to have a sibling folder Baz with it's own package.json, and to use npm link to access it from Foo and Bar.
But it seems that npm link has bigger issues, so maybe we should explore alternatives. One example is wml, which simply watches and copies files.
Building
Even when npm link works fine, the JSX in Baz has not been compiled.
Per this discussion, I think we should compile separate modules like Baz independently, but that will require boilerplate such as babel config. CRA helps avoid this for full apps, but we don't yet have create-react-component to help manage the boilerplate of shared components.
Optimizing for Developer Experience
There are a few ways to clean this up, but I'm curious what people think is the current best practice. What is the ideal developer experience?
Prior art worth considering: insin/nwb, specifically Developing React Components and Libraries with nwb
Ah, I hadn't seen nwb. It looks like a smooth experience!
Does it make more sense to add integrations and compatibility with nwb new react-component? Or should we make a create-react-component that stays in sync with CRA? It could live in the same repo and share configs like babel.
There are a few things that I would have done differently for CRC, in comparison to nwb new react-component:
- Use jest instead of mocha, for consistency with CRA
- No questions at init time (convention over configuration)
A few past issues:
https://github.com/facebookincubator/create-react-app/issues/423 https://github.com/facebookincubator/create-react-app/issues/796 https://github.com/facebookincubator/create-react-app/issues/907 https://github.com/facebookincubator/create-react-app/issues/1242
I can't stress how much I need this feature. My core problem is I have 3 apps: marketing-site, 'customer-app', and 'employee-app'. I am keeping them separate to make them as performant as possible, but there are many shared components, for instance the logo.
My current solution has been having a fourth app that gets imported into each, and then using yarn link, but as mentioned above this is painful, and has issues.
With each layer of separation, my development experience goes down. Having this built into create-react-app would significantly simplify my life.
What artifacts would such a feature produce? CommonJS, Harmony Modules, UMD, AMD?
Something I've been wanting/thinking about for a while now. There are actually a few levels you can look at this:
- I want a CLI that links together all my local node projects I'm working on (like system-wide lerna)
- I want something that shares my common scripts/settings between my projects (like CRA)
- I want something that manages multiple projects (shows their status, pulls them all down initially)
There are some pretty complex considerations I think in here. We have a big stack right now with about 25 packages inside it using lerna. But I'd love to release the 8 or so that are more ready for public consumption.
Problem is, I don't want to sacrifice all the niceness of using Lerna now: shared configs, always in sync, can edit them locally easily, automatically linked together.
So, I've just started on repoman which honestly I'm not sure how will solve all this stuff.
My initial idea is to let your packages define a repoScripts key that points to a github repo, which then has some shared dotfiles/scripts that can be run. Will check into nwb more. If it only handled auto-linking things together + easy standardized/ejectable configurations would be nice.
@izakfilmalter It's not ideal but here's my solution https://github.com/facebookincubator/create-react-app/issues/1084#issuecomment-267725239
Ok, @gaearon, based on your previous comments, it seems that this issue is pretty ripe for fixing.
RFC of a cohesive solution
Please poke this proposal full of holes 😄
Part I: The new meta Creator
We may want to give people more tools like CRA in the future, but it isn't desirable to force people to install a new global dependency for each tool.
Instead, I'd like to propose one tool to rule them all: create-react:
npm install -g create-react
# This is equivalent to `create-react-app MyApp`
create-react app MyApp
# Now we can support:
create-react component Foo
Behind the scenes, this tool will look up the latest npm package to do the heavy lifting. For example, create-react app actually is implemented by the react-creator-app package. The react-creator-component package would define the behavior of create-react component.
This opens up more innovation in the space, because a random company could publish react-creator-mycompany-project, and everybody can easily try out their environment by running create-react mycompany-project.
Also, this could replace react-native init, which could become create-react mobile, create-react native or something.
Part II: The Component Dev Setup
One of the new creators would be the 'component' creator, which creates a new React component package.
Like a CRA app, it includes scripts as devDependencies, and has local commands like npm run build, which builds using the standard Babel configuration. It would output whatever module standard is expected by standard CRA apps.
One major difference is that npm start will build the component and watch for changes, rather than launching a server.
At this point, developers could use npm start alongside linklocal to hack on their components from a neighboring react app.
One huge argument for doing this is to improve tooling for React Native components. Currently, most RN libraries publish uncompiled code, which has become a nightmare when attempting to share libraries or components in other environments that expect node_modules to be compiled. By introducing a common set of library scripts, we can build a comfortable path forward to compiled modules.
Part III: Improve experience within an app
The only bad part about the above experience is the manual usage of linklocal and the npm start for each component you may want to hack on.
In the future, the react-scripts of CRA could be upgraded to automatically run linklocal, so that changes made in neighboring modules are immediately reflected in the development app.
Then, react-scripts can detect when an app's linked component is supported by the same scripts, and can automatically run the watch-and-build behavior for each of the compatible components.
So, if I had Foo and Bar as independent apps that each depended on a sibling Baz component, I could run either app and still see the updates as I make changes to Baz.
Thoughts?
Let me ping a few more smart people who have a lot of context on this: @cpojer @thejameskyle @skevy @taion @ljharb
I'm excited to work on this because I've felt this pain a lot- especially when trying to share code between React Native and web/CRA.
Please let me know if you think this makes sense! I'm happy to put together a prototype, if people like this approach.
I'm not sure I understand - isn't the solution to sharing Baz between two apps, "publish it to npm"? npm link is fine for developing, but something that works across two apps deserves to be a separate package - if not published publicly, then to an internal npm registry (sinopia is free, for example, and using an npm registry gives you semver benefits that git installs don't).
Can you help me understand what the problem is here that needs solving?
@ljharb, I don't think most people who use CRA know about sinopia. If that is the recommended solution, then we can at least do a better job on education. Lots of people seem to like the lerna approach, and/or linklocal.
Aside from the reported issues about npm link, it can be a pain to start up a dev server for each package that you want to edit. If you're codebase is nicely decoupled, you usually have a ton of separate modules. Ideally we could get CRA apps to automatically watch and re-build these other modules when they change.
And yes, publishing is another aim. If we have a CRA-like thing for react native components, we can fix that problem you always rightfully complain about: the RN ecosystem is publishing tons of uncompiled modules that is wreaking havoc on people who try to share code between universes. If we encourage them to use our devDependencies and scripts, their published library/component/nativeModule can easily evolve with best practices.
For the rest of the React ecosystem, it may be handy to publish a component without any build configuration.
So I would (personally) summarise the problem domain as the same one as create-react-app, just with respect to painlessly creating a component without any of the build concerns etc as @ericvicenti has stated. Having previously used sinopia (not picking on it) - but its now not even in maintenance mode (last commit was over a year ago) https://github.com/rlidwka/sinopia/issues/376 and it is a solution that adds another cog in the toolchain rather than making it easier to develop imho. @ericvicenti that approach outlined in the RFC, looks good to me, by the way.
Why not just:
Projects
Components
SharedComponent.js
App1
app1.js
package.json (3rd-party app1's dependencies)
App2
app.js
package.json (3rd-party app2's dependencies)
Run the packager from the root (Projects), when bundling it will only include files that are used. E.g. if SharedComponent.js is only imported / required by app2.js, it will only be included in app2's bundle.
If SharedComponent uses a 3rd-party dependency, all apps using ShareComponent should include that dep in their package.json.
@mkonicek, there is no npm start that is available outside of App1 and App2, so there is no way to "run the packager from the root". How can developers set that up?
Also, what if App1 is React Native and uses RN packager, while App2 is made with CRA and uses webpack?
@admmasters, yes exactly. My aim is to deliver a good development experience for standalone components, just like CRA delivers a great dev experience for apps.
Also semi-related: #741 #1065
@gaearon, does the src/node_modules proposal aim to fix the same problem? I'm curious what the DX would look like in that world, as a developer shares a component between two apps.
I like the solution discussed in #741:
A packages/ (node_modules?) folder inside a repo created by create-react-app.
I think it's a great approach to co-locate components with the app mostly driving them. In early dev stage, this allows juggling code between app and component as things evolve in a seamless manner - without nasty versioning/publish/download overhead.
When ready, packages can be published by create-react-app.
If there's only components and no app, just have a wrapper app in /src and the components in packages.
No need for a separate create-react-component app in this case.
Remains just the problem of using it in another app. Some node/npm related solutions mentioned above (publish, sinopia, link local, NODE_PATH) could be just fine.
@ericvicenti It's related but it's solving a slightly different problem: avoiding relative imports inside the project rather than sharing components between projects. Since people don't use Haste in open source, they have this issue of ../../../../components/abc which is often obnoxious. Whatever solution we settle on, I think we should consider both problems since they're similar.
@bebbi, I'm still confused. What would my directory structure look like? I need two independent apps "Foo" and "Bar", which each share a component "Baz". Baz may be an independent repo, but I would like to easily edit it while developing my Foo and Bar apps.
@gaearon, Gotcha, I agree that the problems are related and should be thought about together. I'm spoiled by using Haste at FB. Its not perfect but it means we never worry about this sort of thing.
It's a partial solution for your scenario. But with the assumption that often one project is driving any given components dev, it might still be a nice one.
If Foo's the driver and we call the components folder packages for now:
Projects
cra_Foo
src
packages
Baz
cra_Bar
src
and Bar accesses Baz through some less direct way, e.g. via NODE_PATH or publish etc.
CRA could publish Foo as an app including Baz, but there would be a script building Baz and anything else in packages as components.
I use git submodules to share components placed in src/Baz of every app. In this way you can edit Baz in Foo, push it and then just git submodule update in Bar.