create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Sharing components between apps, "create-react-component"

Open ericvicenti opened this issue 9 years ago • 83 comments

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?

ericvicenti avatar Feb 06 '17 23:02 ericvicenti

Ok, it seems that there is enough support to give this a try, so I'm going to implement parts one and two of my proposal as a PR to this repo.

That way we will have create-react component Baz, which will create a nice experience around developing a component.

ericvicenti avatar Feb 08 '17 16:02 ericvicenti

I heartily recommend including react-storybook in any component tool: you could have npm start launch the stories. We created our own create-react-component tool in-house that is basically CRA with a custom template and Babel config that we use to transpile and publish shared components to internal npm (originally Sinopia, now Artifactory) but it's limited. But because it's based on CRA we get all the other useful stuff like Jest, webpack, etc.

robcaldecott avatar Feb 08 '17 22:02 robcaldecott

I'm a bit late to the party but I think WebPack (and now the React Native packager too) support symlinks?

So with this setup:

/sharedJSComponents
/myWebApp
/myReactNativeApp

Can I just 'npm link' the sharedComponents into both myWebApp/node_modules and myReactNativeApp/node_modules? Then we don't need anything else, right?

mkonicek avatar Feb 10 '17 17:02 mkonicek

@mkonicek, yes but how will you configure the build for those shared components?

Also, npm link is non-ideal because it clobbers the global installation, in comparison to linklocal which lets you use relative dependencies and link them in for development.

ericvicenti avatar Feb 10 '17 18:02 ericvicenti

@ericvicenti since global installations are only for command line binaries, not requireable things, what global installation of a requireable thing (that is thus non-requireable, and thus useless) are you concerned with clobbering?

ljharb avatar Feb 10 '17 18:02 ljharb

We're working on something for this as well in a generic way over here if anyone's interested:

https://github.com/motion/repo-man

On Fri, Feb 10, 2017 at 10:38 AM Jordan Harband [email protected] wrote:

@ericvicenti https://github.com/ericvicenti since global installations are only for command line binaries, not requireable things, what global installation of a requireable thing (that is thus non-requireable, and thus useless) are you concerned with clobbering?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/facebookincubator/create-react-app/issues/1492#issuecomment-279027952, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAvRGVuIX91IcOCvgltXl5ZKEUZP_Lwks5rbK6cgaJpZM4L42Uw .

natew avatar Feb 10 '17 22:02 natew

@ljharb, Different projects on your computer could use the same module names, and they collide when linked globally.

ericvicenti avatar Feb 10 '17 23:02 ericvicenti

@ericvicenti that seems like a naming problem, not a tooling problem :-/ names should be unique.

ljharb avatar Feb 10 '17 23:02 ljharb

Yes but we need namespacing tools to avoid duplicate names. Like on my computer, I can have two files with the same name, just not in the same folder. I'm not going to tell people that they have to worry about namespace conflicts across the whole the computer.

ericvicenti avatar Feb 11 '17 00:02 ericvicenti

IIRC symlinks also confuse watchman (which we rely on through Jest). Not sure if I'm right though.

gaearon avatar Feb 11 '17 01:02 gaearon

@gaearon, yeah I think you're right. Pretty sure that is why the RN packager doesn't support symlinks. So I guess that would mostly affect the results of npm test --watch

ericvicenti avatar Feb 11 '17 01:02 ericvicenti

Symlinks also do not currently play well with packager: https://github.com/facebook/react-native/issues/637

AshCoolman avatar Feb 14 '17 07:02 AshCoolman

So I guess that would mostly affect the results of npm test --watch

We default to watch mode so it's essential that it works well.

gaearon avatar Feb 14 '17 20:02 gaearon

cc @insin on the off chance that he hasn't seen this issue

taion avatar Feb 16 '17 18:02 taion

I think if I could easily extend paths.appSrc (maybe via package.json properties?) to point to paths in my monorepo, I could share all the code I need in my lerna-based monorepo and have a happy development experience. At least with yarn start and yarn build, not sure how it'd work with tests.

arnihermann avatar Mar 02 '17 16:03 arnihermann

@ericvicenti What’s your thoughts on this? Do you plan to pursue a proof of concept?

gaearon avatar Mar 02 '17 17:03 gaearon

Yep, apologies for the lack of communication! I've hacked on this a bit with @skevy, and we have a partial prototype. It is mainly lacking storybook support. I'll get that done within the next day or two and put up a PR.

This is my planning doc for the scope of this new tool: https://gist.github.com/ericvicenti/b69ba80d22aa3f39f3d4cb5606ae8c9f

You can see the progress here: https://github.com/ericvicenti/create-react-app/tree/master/packages/react-component-scripts

ericvicenti avatar Mar 02 '17 20:03 ericvicenti

For my use-case, I'm not sure the component scripts would do since my projects are structured differently. Please correct me if I'm wrong though. For instance, I have multiple create-react-app's in my lerna monorepo. I might take a stab at my suggestion if I can get a thumbs up on it from a few others. Otherwise, I'll just eject and hack it. /cc @gaearon

arnihermann avatar Mar 02 '17 20:03 arnihermann

I don't know if it necessarily makes sense to duplicate so much logic/structure and fork into a new react-component-scripts. I feel like react-scripts could serve both purposes, simply introducing new component-build, component-start, and component-test commands.

Can you provide me with a [brief] reason for forking the scripts?

Timer avatar Mar 02 '17 21:03 Timer

@arnihermann, Im hesitant to add lerna-specific things at this point

@Timer, I was unsure how much code would actually need to be shared, but at this point I tend to agree with you. @gaearon, what do you think about adding the features of react-component-scripts into react-scripts?

ericvicenti avatar Mar 02 '17 22:03 ericvicenti