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

Set module resolution based on baseUrl in jsconfig/tsconfig.json #6116

Closed
wants to merge 34 commits into from
Closed

Set module resolution based on baseUrl in jsconfig/tsconfig.json #6116

wants to merge 34 commits into from

Conversation

robertvansteen
Copy link
Contributor

@robertvansteen robertvansteen commented Jan 3, 2019

Based on this thread on Twitter here is a PR to kickstart development/discussion on supporting absolute paths in Typescript via NODE_PATH so they work consistently between JS & TS.

Updated: Based on the discussion here: #5645 (comment) This attempts to unify the configuration by reading baseUrl from jsconfig.json / tsconfig.json.

🚧 This PR is still in progress

To do:

  • Add tests
  • Make sure paths are valid TypeScript
  • Add deprecation message to react-scripts start
  • Add documentation

@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. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@robertvansteen robertvansteen changed the title Set TS base url based on node_path Set TS baseUrl based on node_path Jan 3, 2019
@gaearon
Copy link
Contributor

gaearon commented Jan 3, 2019

@ is likely a larger chunk of work so it's better to do separately.

We'll need to test Windows.


While we're at it, I also have an alternative proposal: #5645 (comment). What do you think about it?

@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!

@robertvansteen robertvansteen changed the title Set TS baseUrl based on node_path Set baseUrl based on jsconfig/tsconfig.json Jan 3, 2019
@robertvansteen robertvansteen changed the title Set baseUrl based on jsconfig/tsconfig.json Set module resolution based on baseUrl in jsconfig/tsconfig.json Jan 3, 2019
@robertvansteen
Copy link
Contributor Author

I'm having some difficulties with the tests both locally and on the CI. Anyone that has some experience with the test suite in CRA that has some time to help me out?

@IIFE
Copy link

IIFE commented Jan 4, 2019

@rovansteen I've just added few comments to the changes (first time reviewing open source code :) ).

Hope they make sense.

@robertvansteen
Copy link
Contributor Author

robertvansteen commented Jan 4, 2019

@IIFE thanks for your feedback! 😄

@mrmckeb
Copy link
Contributor

mrmckeb commented Jan 4, 2019

@rovansteen, great work - thanks!

If we're deprecating NODE_PATH, perhaps we should look at adding some warning here - or migration steps?

@robertvansteen
Copy link
Contributor Author

@mrmckeb fair enough. Got any ideas about warnings or migration steps?

@mrmckeb
Copy link
Contributor

mrmckeb commented Jan 6, 2019

@rovansteen I'd just check if NODE_PATH was passed in, and if it was, add a warning to the console and mention that it's been deprecated in favour of using either a jsconfig or tsconfig file to achieve the same thing - and it would be great to link through to any related docs.

The concern here is that some people (including me) already rely on NODE_PATH, and might not read the release notes carefully enough when updating to a new react-scripts version.

@robertvansteen
Copy link
Contributor Author

So as @mrmckeb correctly pointed out the current implementation in this PR is not actually valid TypeScript and fails if you run tsc. A path (alias) should actually look like this: "@*": ["src/*"] rather than this: "@": ["src"].

I'm working on an implementation that supports this but @Timer is there any way we can run tsc on the test/fixtures/typescript folder, to make sure that even though it builds properly it's also completely valid TypeScript? The whole test setup seems a bit daunting to me so not sure where I should put that.

"compilerOptions": {
"baseUrl": "src",
"paths": {
"@": ["src"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps add a test with * along with @* for compatibility with the previous NODE_PATH approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For people that had their NODE_PATH set to . and were importing like this: src/components/button?

Copy link
Contributor

Choose a reason for hiding this comment

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

So, I think it should be the below as a default, as that matches the NODE_PATH behaviour. Assuming the user set NODE_PATH to src would make this a good default:

  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "*": ["src/*"],
    }
  },

Then, a user could do the following, if they chose.

  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@*": ["src/*"],
    }
  },

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I'm not sure if we should provide a default as the NODE_PATH is not set to . by default which would potentially confuse users thinking that's default node behavior.

Copy link
Contributor

@mrmckeb mrmckeb Jan 8, 2019

Choose a reason for hiding this comment

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

I understand, by default there's no NODE_PATH. However, I think most people that use it would use it with src.

As per most implementations that I've seen (based on Microsoft examples) you can leave "baseUrl" as ".", and the aliasing itself can happen in paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that makes sense and that's something we should definitely recommend in the documentation. Especially if you are migrating from NODE_PATH=src

@robertvansteen
Copy link
Contributor Author

@Timer @mrmckeb @ianschmitz given the vast amount of different tsconfig/jsconfig.json inputs we'd have to parse is there any chance we could add some unit tests for this? Right now CRA only contains integration/e2e tests but they are not practical to test lots of different configurations in this case.

I'm not really familiar with the whole testing setup in bash, so not sure where to add any unit tests. If you could give me a direction that would be really helpful!

@mrmckeb
Copy link
Contributor

mrmckeb commented Jan 15, 2019

Hi @rovansteen, I think @Timer would be better placed to give direction on this - as he's been with the project a lot longer than I. However, I'll try to answer as best I can :)

I think you would probably want to add a fixture here:
https://github.com/facebook/create-react-app/tree/master/test/fixtures

Essentially, you'd be testing that the app picks up the configs as expected.

@robertvansteen
Copy link
Contributor Author

@mrmckeb I have added that, however, there are so many possible configurations we should support it's not feasible to run an integration test for every possible configuration. I'd rather have an integration test to check if the config is picked up as expected (I already added these) and then a bunch of unit tests that just test if the values of the configuration are parsed correctly.

@eps1lon
Copy link
Contributor

eps1lon commented Feb 3, 2019

As far as I can tell this would change the behavior of paths explained in microsoft/TypeScript#10866. In depth comment: #5645 (comment)

@robertvansteen
Copy link
Contributor Author

@eps1lon good point, thanks for bringing that up.

@gaearon you've been an advocate for adding some sort of alias for absolute imports. Do you still want to add that to CRA even if TypeScript's support to this is limited to just compilation?

@robertvansteen
Copy link
Contributor Author

Since this PR became a bit too big and ambitious by trying to support both baseUrl and aliases I've created a new PR just for supporting baseUrl (#6656). Giving us more room to figure out how to add support for aliases in a stable way.

@lock lock bot locked and limited conversation to collaborators Mar 20, 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.

9 participants