Skip to content

Document maintaining a fork of react-scripts as an alternative to ejecting #682

Description

@gaearon

It is not common knowledge that you can fork react-scripts, publish your fork, and then do create-react-app my-app --scripts-version my-react-scripts-fork.

This can be useful for customized setups, especially if you want to merge upstream updates once in a while. And with something like https://github.com/1egoman/backstroke, it might not even be painful!

We should document this. Help welcome!

Activity

  1. thangngoc89 commented on Sep 19, 2016

    @thangngoc89
    Contributor

    I tried this before. (0.2 maybe). But the global cli doesn't install the custom script. Was it fix?

  2. sloanelybutsurely commented on Sep 19, 2016

    @sloanelybutsurely

    We are going to be forking react-scripts and attempting to move our build process over to it @trunkclub. I'll talk to the team but I think we could write documentation as we go through the process.

  3. gaearon commented on Sep 19, 2016

    @gaearon
    ContributorAuthor

    But the global cli doesn't install the custom script. Was it fix?

    There was a bug but should be fine with 0.4.3.

  4. leftdevel commented on Sep 19, 2016

    @leftdevel

    This is supper awesome (...if I understood correctly), Thank you! I'm loving create-react-app, but in the current state it felt like you either embrace it as it is (minimal), or move away. And "Eject" makes you feel like you're being ungrateful with the maintainers.

    Now with this approach, and in my company, I can be the one maintaining the custom scripts while still keeping my teammates away from the struggle of battling configuration files.

    For instance, I'd just add less-css support, and still benefit from the core create-react-app features and updates.

  5. kirkaustin commented on Sep 19, 2016

    @kirkaustin

    I just run a short custom script after create-react-app does its thing so that I can support CSSModules. All it does is alter two lines in the webpack config scripts (css loader delacarations).

    Seemed like overkill to fork and publish, but maybe that's the way to go?

  6. thangngoc89 commented on Sep 19, 2016

    @thangngoc89
    Contributor

    @kirkaustin you don't need to publish it. You can install it directly from github

  7. kirkaustin commented on Sep 19, 2016

    @kirkaustin

    @thangngoc89 Not sure I understand. At my company we have an enterprise GitHub account. Are you suggesting that I can have my fork pulled from there?

  8. thangngoc89 commented on Sep 19, 2016

    @thangngoc89
    Contributor

    You're using npm to install dependencies right? You can do this npm install git+ssh://[email protected]/your-company/react-scripts-fork . You don't need to publish the scripts to npm and then install it.

  9. kirkaustin commented on Sep 19, 2016

    @kirkaustin

    Great, thanks!

  10. dpoineau commented on Sep 21, 2016

    @dpoineau

    Just curious if anyone has gotten a forked version of react-scripts via direct-from-github npm install working with the most recent create-react-app repository code? @thangngoc89

    With the switch by create-react-app to using lerna and as a result react-scripts moving into the packages/react-scripts subdirectory, it doesn't seem possible to install a forked react-scripts directly from github any longer (since npm cannot install from a github repo subdirectory), or am I missing something?

    Previously forking the entire repo (and referencing via github url) worked because react-scripts was the package specified in the repo's root package.json, but that's no longer the case it seems.

    So is publishing a custom npm package then the only way to take advantage of this approach?

  11. dpoineau commented on Sep 22, 2016

    @dpoineau

    So for now I went down the path of using a custom published npm package (like react-scripts-custom-whatever), but it still seems like there's at least one issue that prevents this from working which is that the react-scripts package name is hardcoded in at least one spot, which won't work if you're using a custom forked package: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/scripts/init.js#L17

    The end result for me is that create-react-app fails at executing the init script. Has anyone been successful with this approach with the most recent version of react-scripts (0.4.3), and I'm just doing something wrong?

    Edit: after looking at how e2e.sh runs its --scripts-version react-scripts-fork test successfully, it looks like the solution used in that fork was to just change the hardcoded package name in the line I referenced above in the fork to be the forked package name.

    Last edit: Here are all the steps I used to get a custom fork working in case anyone else gets as stuck as I did 😃

    1. Forked the create-react-app repo and cloned it locally
    2. Changed the package name in packages/react-scripts/package.json to be a new custom name
    3. Changed the 'react-scripts' reference in packages/react-scripts/scripts/init.js#L17 to match the new package name I chose
    4. Then I did a cd packages/react-scripts followed by an npm publish which published successfully
    5. Which then resulted in create-react-app test-app --scripts-version react-scripts-my-custom-package working as expected
  12. gaearon commented on Sep 22, 2016

    @gaearon
    ContributorAuthor

    it still seems like there's at least one issue that prevents this from working which is that the react-scripts package name is hardcoded in at least one spot, which won't work if you're using a custom forked package: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/scripts/init.js#L17

    PR to fix this would be welcome. 😉

  13. ForbesLindesay commented on Sep 22, 2016

    @ForbesLindesay
    Contributor

    I'm actually in favour of (maybe) just telling people to fork the entire monorepo and publish create-whatever-app and whatever-scripts. It would be good if you didn't have to change react-scripts and create-react-app strings in 100 places as your first change though. I would suggest putting both of these strings in some kind of names.js package in each project. That way there would be just two places it would need changing.

    I wonder if we should suggest people use code to generate their fork, rather than forking by hand. e.g. they could write code like overwrite webpack.config.js with this file. That way updating the fork would just mean re-running the code to generate the fork against the latest version of create-react-app.

    Alternatively, should we just encourage people to hard-fork once create-react-app is sufficiently stable?

  14. 65 remaining items

  15. cliff76 commented on Sep 13, 2018

    @cliff76

    Is this still a viable solution/alternative to eject? How would I add a fork of react-scripts to an existing project created with CRA? The command line doesn't seem to update/overwrite configs in an existing project. Also, when starting anew I'm also getting Error: Cannot find module 'react-dev-utils/browsersHelper

  16. jimthedev commented on Sep 13, 2018

    @jimthedev

    I got it working. I had to publish forks of each package and had to update all internal references to point at my packages. It’s a pain but I’m just maintaining it until 2.0 drops officially. Unfortunately it has enough custom parts I cannot publish publicly but could contribute to your fork if you have a place you want to work out of.

  17. cliff76 commented on Sep 13, 2018

    @cliff76

    Ouch! I see. Is there any way to update an existing project with a fork? We just started a project a couple of weeks ago via CRA and I need to make the smallest tweak to the webpack configs. I feel like I'm firing up a 747 just to visit my next door neighbor with this approach but I'm doing what I can to make things run.

  18. cliff76 commented on Sep 13, 2018

    @cliff76

    Also, does each fork need to have its package name updated to be different from the official repo?

  19. jimthedev commented on Sep 13, 2018

    @jimthedev
  20. cliff76 commented on Sep 13, 2018

    @cliff76

    I saw this earlier and lost the link! Does it actually work? I'll have to give it a shot. Thanks!

  21. cliff76 commented on Sep 14, 2018

    @cliff76

    By the way, that works like a charm, thanks!

  22. cweekly commented on Oct 25, 2018

    @cweekly

    What about https://github.com/timarney/react-app-rewired?

    I agree react-app-rewired was a great solution before CRA2. But CRA2 uses webpack 4, which breaks most plugins. Also its maintainer is apparently retiring[1], so this remains a thorny problem.

    1. 2.0 Discussion timarney/react-app-rewired#162
  23. ubbcou commented on Oct 27, 2018

    @ubbcou

    It seems that I have some trouble.
    I forked thie repo and now its address is https://github.com/ubbcou/create-react-app。The directory of react-scripts is /packages/react-scripts。
    I want to use customized react-scripts (without publish in npm), but how can I install /packages/react-scripts ?
    I only know that npm can install a repository like this: npm install --save https://github.com/ubbcou/create-react-app, and it can't do like this npm install --save https://github.com/ubbcou/create-react-app/packages/react-scripts.
    what should I do???

  24. uqee commented on Oct 27, 2018

    @uqee

    @ubbcou publish it as a scoped package, like @ubbcou/react-scripts

  25. ubbcou commented on Oct 27, 2018

    @ubbcou

    @ubbcou 将它作为一个范围包发布,就像@ubbcou/react-scripts

    @uqee
    Thank you :)
    So.... if I have two or more different configurations, I should publish multiple @ubbcou/react-scripts-whatever ?

  26. hisapy commented on Nov 21, 2018

    @hisapy

    I tried to use a fork of react-scripts with yarn link but yarn build outputs to node_modules/create-react-app/build instead of MY_CURRENT_PROJECT/build. Are there any known problems that prevent using react-scripts from yarn link?

  27. hisapy commented on Nov 25, 2018

    @hisapy

    Well, after diving into the code, I noticed that there paths.js module that exports different path values based on some assumptions, one of them being published, for which it tries to evaluate if the app's node_modules contains the react-scripts package, but apparently this doesn't work with symlinks created with yarn link.

    So what I've done to avoid publishing to npm each time I wanted to try something in my app, was to install my custom react-scripts from my file system with the following command:

    yarn add file:/Users/hisa/workspace/create-react-app/packages/react-scripts/

    This works, but the downside is that you have to run the command each time you make a modification of the your custom react-scripts. It would be awesome if we could use yarn link instead.

  28. locked and limited conversation to collaborators on Jan 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions