Skip to content
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

add support for installing react-scripts using npm link #1122

Closed
wants to merge 3 commits into from
Closed

add support for installing react-scripts using npm link #1122

wants to merge 3 commits into from

Conversation

tabrezm
Copy link

@tabrezm tabrezm commented Nov 30, 2016

When using npm link to depend on a forked version of react-scripts, the paths are set up incorrectly such that the source files in the react-scripts template get used instead of the actual source files from the app.

Repro steps:

  1. Create a package using the public version of create-react-app.
  2. Clone create-react-app from GitHub and convert packages/react-scripts to a scoped package. For example, change this:
  "name": "react-scripts",
  "version": "0.7.0",

to this:

  "name": "@tabrezm/react-scripts",
  "version": "0.0.0",
  1. Run npm link from packages/react-scripts.
  2. Run npm link @tabrezm/react-scripts from the folder for the package created in step 1. Also, change the following lines in package.json from this:
  "devDependencies": {
    "react-scripts": "0.7.0"
  },

to this

  "devDependencies": {
    "@tabrezm/react-scripts": "0.0.0"
  },
  1. Change something in App.js and run npm start.

Expected: see new content
Actual: see content from template in react-scripts instead

Full discussion: #682 (comment)

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks!

If you are contributing on behalf of someone else (eg your employer): the individual CLA is not sufficient - use https://developers.facebook.com/opensource/cla?type=company instead. Contact cla@fb.com if you have any questions.

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@tabrezm
Copy link
Author

tabrezm commented Dec 1, 2016

Hmm, ignore for now? I can't get this build failure to repro locally :(

EDIT: Fixed build issues. Apologies for the spam.

add support for installing react-scripts via npm link
@tabrezm tabrezm changed the title use npm_lifecycle_event to override paths for publishing add support for installing react-scripts using npm link Dec 1, 2016
@gaearon gaearon added this to the 0.8.0 milestone Dec 1, 2016
nit: grammar fix in comment
@raycohen
Copy link

raycohen commented Dec 3, 2016

with this change, are the scoped package steps still needed if you're not making a long running fork?

I want to be able to take my existing app, pull someone's branch for an open PR, run npm link react-scripts, and see how those changes work in my app. And then npm unlink react-scripts once I've gotten to test. This simple flow works with most other dependencies... can it work with react-scripts?

@tabrezm
Copy link
Author

tabrezm commented Dec 3, 2016

Yep, should work. Running npm link <react-script-module-name> from your app should change the path under node_modules to pick up the local dependency. The original issue (and my particular set up) were based on longer term forks.

@fson fson modified the milestones: 0.9.0, 0.8.0 Dec 3, 2016
// `publish`) or running the smoke test.
var isRunningFromOwn = __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1;
var isSmokeTest = process.argv.some(arg => arg.indexOf('--smoke-test') > -1);
var isRunningFromAppUsingLink = (process.env.npm_lifecycle_event === 'start') && isRunningFromOwn && !isSmokeTest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is special about start? I don't understand this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without including the check for start, (i.e. only checking isRunningFromOwn && !isSmokeTest), the tests fail because those conditions are both true for other events like build. At first glance, it may be assuming too much, as it's entirely possible that someone will write an app that has other start-like events, but are named something else. But they should be able to handle their own custom cases since they'll be forking react-scripts in the first place.

@gaearon
Copy link
Contributor

gaearon commented Dec 12, 2016

Could you write up another explanation about why this works and solves that use case?
I read the comments but I still don’t get it. (Sorry!)

@tabrezm
Copy link
Author

tabrezm commented Dec 15, 2016

When you try to use npm link to reference a forked version of react-scripts, the original logic of paths.js uses the template from the react-scripts module (i.e. packages/react-scripts/template) instead of the app itself (e.g. src). You can see this by following the repro steps in the initial comment. After a ton of trial and error, I came up with a set of conditions for when to use the template, and when to use local source files of the app referencing react-scripts. There are effectively three cases to look for, so I'll walk through each one.

  1. isRunningFromOwn: this is actually preserving the case noted by this comment.
  2. isSmokeTest: this is a special case that I wasn't able to work around in a more elegant way. I'll talk about this one in the next case.
  3. isRunningFromAppUsingLink: when using npm link, the current dir contains packages/react-scripts/template, which when using the current implementation, resolves the paths to point to the template in react-scripts instead of the source files in the app referencing react-scripts. When trying to separate this case out, I ran into two additional conditions I need to check for where it is valid to keep using the template:
    a. process.env.npm_lifecycle_event === 'start': by checking for this, we ensure that we don't clobber the existing cases for build, publish, etc. where we don't want to look at an invalid path for the template. In other words, it's still correct to look for the template at packages/react-scripts/template.
    b. !isSmokeTest: without checking for this case, the tests would break since they'd look at the wrong path for the template. I borrowed the special check for a smoke test from here.

With the special logic to track isRunningFromAppUsingLink, I refactored the existing usages of resolveApp and resolveOwn to handle this special case.

@gaearon gaearon modified the milestones: 0.10.0, 0.9.0 Jan 23, 2017
@gaearon gaearon modified the milestones: 0.9.1, 0.10.0 Feb 12, 2017
@gaearon
Copy link
Contributor

gaearon commented Feb 12, 2017

Tagging as 0.9.1 as I'd like us to understand what's the problem here, and maybe merge this in some form.

@Timer
Copy link
Contributor

Timer commented Feb 23, 2017

I believe that this is trying to accomplish what #1356 does, but in a much more convoluted way.

I appreciate all your hard work and explanations @tabrezm, but I'm going to close this in favor of #1356 since it is a more straightforward modification.

Please do not hesitate to reach out to us if this does not resolve your use case.
It will be available when 0.9.1 is released, or you can test it manually sooner. 😄

@Timer Timer closed this Feb 23, 2017
@lock lock bot locked and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants