Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Typescript: Simplified typescript steps not working #5541

Closed
sebasmurphy opened this issue Oct 23, 2018 · 18 comments
Closed

Typescript: Simplified typescript steps not working #5541

sebasmurphy opened this issue Oct 23, 2018 · 18 comments

Comments

@sebasmurphy
Copy link

Is this a bug report?

Yes

Did you try recovering your dependencies?

Yes

Environment

Environment:
OS: macOS 10.14
Node: 10.12.0
Yarn: 1.10.1
npm: 6.4.1

Packages: (wanted => installed)
react: ^16.5.2 => 16.5.2
react-dom: ^16.5.2 => 16.5.2
react-scripts: 2.0.5 => 2.0.5

Steps to Reproduce

I noticed that typescript was added to the project officially. I went ahead and created a new project and followed in the instructions detailed in 3c11efe but they did not work. I've walked back through the commit history and tried some of the previously detailed steps to no avail. Neither creating a tsconfig.json or a tsconfig.json with {} in the file worked for me either.

Followed instructions listed at https://github.com/facebook/create-react-app/blob/master/docusaurus/docs/adding-typescript.md

  1. npx create-react-app
  2. yarn add typescript @types/react @types/react-dom @types/jest
  3. Rename src/index.js to src/index.tsx

Expected Behavior

I expected the app to start correctly

Actual Behavior

Received an error .

Could not find a required file.
  Name: index.js

Reproducible Demo

https://github.com/sebasmurphy/prototype-react-typescript

@miafoo
Copy link

miafoo commented Oct 23, 2018

I believe the issue is the fact that - while the PR was merged in - they have not yet published a new version of react-scripts which includes these changes.

Last publish was 9 days ago: https://www.npmjs.com/package/react-scripts

@sebasmurphy
Copy link
Author

sebasmurphy commented Oct 23, 2018

Ahh totally forgot to check that.

I assumed it had already been done given all of the recent commits to master in /packages/react-scripts

@tomaszwylezekperform
Copy link

Is known when the new version will be published?

@miafoo
Copy link

miafoo commented Oct 23, 2018

Not sure, but would be awesome if it was at least published to canary or a different tag.

@brunolemos
Copy link
Contributor

brunolemos commented Oct 23, 2018

react-scripts: 2.0.5 => 2.0.5

Not a bug, it's just that v2.1 wasn't published yet. It should be really soon!

You can use the yarn link method for now.

- npx create-react-app appname --scripts-version=@brunolemos/react-scripts
- cd appname
- yarn add --dev typescript @types/react @types/react-dom @types/jest
- git mv src/index.js src/index.tsx
- yarn start

Note: It's possible the steps above don't work 100% because I only published react-scripts, but the other packages from the monorepo should be published as well.

@nielsdB97
Copy link

@brunolemos I'm currently using react-scripts-ts which I think was also recommended in the CRA readme. Do you think it would be a good idea to add a migration guide for people coming from react-scripts-ts to switch to react-scripts?

@brunolemos
Copy link
Contributor

brunolemos commented Oct 23, 2018

@nielsdB97 Not sure, because it's very simple. The user just needs to remove any mention of react-scripts-ts, install the latest react-scripts and the dependencies from the docs.

Anything different than this only makes sense to be on react-scripts-ts docs, not on create-react-app, for example removing the tsconfig.prod.json file and running tslint on a pre-commit hook.

@nielsdB97
Copy link

@brunolemos if that's all there is to it then I would agree it's not necessary to have a migration guide. However I figured there might be some differences in the configs, for example how react-scripts-ts requires you to have separate tsconfig.json, tsconfig.prod.json and tsconfig.test.json.

I actually just tried replacing react-scripts-ts with @brunolemos/react-scripts on the latest version, but it fails to compile src/index.tsx on the first TS syntax it encounters. Is there something I'm doing wrong or is this expected because I'm maybe missing something?

PS: Thanks a lot for your efforts, this is what was keeping me from using CRA 😄

@brunolemos
Copy link
Contributor

brunolemos commented Oct 23, 2018

@nielsdB97 It's because I didn't publish the other required packages, sorry.

Please wait for the official release or use this method instead:

npx create-react-app@next app-name --scripts-version=2.0.6-next.c662dfb0 --typescript


Older method using yarn link:

  • Clone
    • git clone git@github.com:facebook/create-react-app.git
    • cd create-react-app
  • Compile
    • yarn
  • Create link
    • cd packages/react-scripts/
    • yarn link
  • Create new project / Go to your existing project
    • cd ~/your/projects/folder
    • npx create-react-app app-name
    • cd app-name
  • Use Link
    • yarn link react-scripts
  • Setup TypeScript
  • Finish
    • yarn start

@Paul-Colucci
Copy link

@brunolemos is there an estimated time frame on when the new package will be available?

@brunolemos
Copy link
Contributor

brunolemos commented Oct 23, 2018

@Paul-Colucci I don't have control over when it will be released, so I don't know. But if 2.1 will only contain typescript, it could be any time now.

@nielsdB97
Copy link

nielsdB97 commented Oct 24, 2018

@brunolemos I just followed your instructions and linked my local react-scripts checkout to my project. It fails to compile however, since it doesn't recognize my absolute imports based on compileroptions.baseUrl config in tsconfig.json (which works fine under react-scripts-ts).

Is this because it uses Babel and not the standard TS compiler? If so, do you think we can/want to support this functionality by default?

@gaearon
Copy link
Contributor

gaearon commented Oct 24, 2018

I assumed it had already been done given all of the recent commits to master in /packages/react-scripts

Things that land in master might not get released for weeks or even months (in the worst case). When things get released, they appear in the changelog and on the releases page. 😉

@xiaoxiangmoe
Copy link
Contributor

@gaearon Can we put @types/react into the repo of react?

@sebasmurphy
Copy link
Author

@gaearon yeah that's my bad, I jumped the gun a bit. I saw some tweets and discussion on hnews and assumed a new version had been released with the changes. However the aforementioned posts were just referencing the fact that the code had been merged. 😞

@hinsxd
Copy link

hinsxd commented Oct 25, 2018

@nielsdB97 It's because I didn't publish the other required packages, sorry.

Please wait for the official release or use this method instead:

  • Clone

    • git clone git@github.com:facebook/create-react-app.git
    • cd create-react-app
  • Compile

    • yarn
  • Create link

    • cd packages/react-scripts/
    • yarn link
  • Create new project / Go to your existing project

    • cd ~/your/projects/folder
    • npx create-react-app app-name
    • cd app-name
  • Use Link

    • yarn link react-scripts
  • Setup TypeScript

  • Finish

    • yarn start

However when I tried to import scss files, the following error appears:

./src/App.scss (/Users/hinsxd/projects/create-react-app/node_modules/css-loader??ref--6-oneOf-5-1!/Users/hinsxd/projects/create-react-app/node_modules/postcss-loader/src??postcss!/Users/hinsxd/projects/create-react-app/node_modules/sass-loader/lib/loader.js!./src/App.scss)
To import Sass files, you first need to install node-sass.
Run `npm install node-sass` or `yarn add node-sass` inside your workspace.

The create-react-app folder is the root folder of create-react-app clone. It seems there are some issues in resolving symbolic links that react-scripts does not know that root path of the project.
I added console.log(__dirname) at the very first line of the script, found out that it wasn't inside node_modules, but the cloned directory.
Perhaps (strothj/react-app-rewire-typescript-babel-preset#30) would be a safer method.

@brunolemos
Copy link
Contributor

Please use this new method instead of yarn link:

npx create-react-app@next app-name --scripts-version=2.0.6-next.c662dfb0 --typescript

For existing projects, run yarn unlink react-scripts and install yarn add react-scripts@2.0.6-next.c662dfb0.

Let me know if that works for you!

@sebasmurphy you can close this :)

@sebasmurphy
Copy link
Author

Closing...

@lock lock bot locked and limited conversation to collaborators Jan 9, 2019
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants