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 TypeScript support for react-scripts #4824

Merged
merged 1 commit into from Nov 21, 2018
Merged

Add TypeScript support for react-scripts #4824

merged 1 commit into from Nov 21, 2018

Conversation

mrmckeb
Copy link
Member

@mrmckeb mrmckeb commented Nov 20, 2018

Issue: #4587

What I did

Added support for TypeScript when using react-scripts@>=2.1.0.

How to test

Create a new Create React App project and pass in the --typescript flag.

npx create-react-app my-app --typescript

Install storybook (this build) and run storybook. You should not see any errors.

@codecov
Copy link

codecov bot commented Nov 20, 2018

Codecov Report

Merging #4824 into next will increase coverage by 0.04%.
The diff coverage is 33.33%.

Impacted file tree graph

@@            Coverage Diff            @@
##            next    #4824      +/-   ##
=========================================
+ Coverage   35.5%   35.55%   +0.04%     
=========================================
  Files        560      560              
  Lines       6818     6827       +9     
  Branches     913      915       +2     
=========================================
+ Hits        2421     2427       +6     
- Misses      3919     3920       +1     
- Partials     478      480       +2
Impacted Files Coverage Δ
app/react/src/server/options.js 0% <ø> (ø) ⬆️
app/react/src/server/framework-preset-cra-rules.js 0% <ø> (ø)
app/react/src/server/cra-config.js 28.2% <33.33%> (+11.53%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e5c925f...77dd97e. Read the comment docs.

Copy link
Member

@igor-dv igor-dv left a comment

Choose a reason for hiding this comment

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

Very cool! Thanks for taking it.
Do you want to add CLI tests for this in a separate PR?

app/react/src/server/cra-config.js Show resolved Hide resolved
app/react/src/server/cra-config.js Outdated Show resolved Hide resolved
@mrmckeb
Copy link
Member Author

mrmckeb commented Nov 21, 2018

@igor-dv Definitely, I can take a look at that over the weekend.

I've squashed and pushed those changes, thanks for the review.

@igor-dv igor-dv merged commit a9b787e into storybookjs:next Nov 21, 2018
@mrmckeb
Copy link
Member Author

mrmckeb commented Nov 26, 2018

Just a note that I didn't work on tests for this as @igor-dv put together and merged in a clever PR (#4836) to make the CRA-style config default for all React-based apps, which affected these files.

@mrmckeb mrmckeb deleted the feature/add-cra-ts-support branch November 26, 2018 08:28
@Vinnl
Copy link

Vinnl commented Dec 13, 2018

Thanks for this work! Do I understand it correctly that now, using Storybook 4.1, we can:

And follow-up in case the latter is false: is there anything that needs to be done to enable writing stories in TypeScript, other than renaming them from .jsx to .tsx?

@igor-dv
Copy link
Member

igor-dv commented Dec 13, 2018

If you are using CRA2 you can use TS for all also in storybook

@mrmckeb
Copy link
Member Author

mrmckeb commented Dec 13, 2018

Yes, all of my team's stories are .tsx and it works great.

@Vinnl
Copy link

Vinnl commented Dec 13, 2018

@mrmckeb Hmm, and you've removed your custom webpack.config.js and don't have a TypeScript loader in your own dependencies?

I'm asking because I'm running into

Module parse failed: Unexpected token (17:22) You may need an appropriate loader to handle this file type.

So it appears that for some reason, without my own Webpack config, Storybook's config does not look for TypeScript files.

@mrmckeb
Copy link
Member Author

mrmckeb commented Dec 13, 2018

Hi @Vinnl, can you let me know what version of react-scripts and @storybook/react you're using? Thanks!

@Vinnl
Copy link

Vinnl commented Dec 13, 2018

Of course @mrmckeb :) react-scripts is at 2.1.1, @storybook/react is at 4.1.1. According to yarn outdated, both are at their latest version.

@mrmckeb
Copy link
Member Author

mrmckeb commented Dec 13, 2018

Hmm, where are you stories located? In component directories?

Perhaps you could push up a quick test repo that I could play with? Something very rough.

@Vinnl
Copy link

Vinnl commented Dec 13, 2018

@mrmckeb The stories are in /stories, next to the components which are in /src. The project is open source, so it can be seen here. (Note that this work is done in the branch 57-use-svg-component-and-native-typescript-in-storybook, and that there are several projects in this repo - the relevant one is in /client.)

Update: With the following webpack.config.js it does work, although I think it's supposed to work without it?

module.exports = (baseConfig, env, config) => {
  config.module.rules.push({
    test: /\.(ts|tsx)$/,
    loader: require.resolve('babel-loader'),
    options: {
      presets: [['react-app', { flow: false, typescript: true }]],
    },
  });
  config.resolve.extensions.push('.ts', '.tsx');
  return config;
}

Second update: vstelmakh on Slack confirmed that it's not working without configuration for them either.

Final update: Moving the stories to be located within src/ (which also is a requirement for regular components with Create React App) made it work!

@mrmckeb
Copy link
Member Author

mrmckeb commented Dec 14, 2018

Sorry @Vinnl, I see what's wrong now.

Our config (at create-react-app) supports files inside of src, and you have your stories outside of src.

You can either add your stories folder to src, or use another approach. Personally, I keep stories alongside each component (Component.stories.tsx).

The custom webpack config you have provided is not necessary, if you really want to keep a stories folder outside of src, you can simply add that to the include array:
https://github.com/storybooks/storybook/pull/4902/files#diff-eda2a92e48b4df8907d95774b054a271R75

I hope that helps!

@Vinnl
Copy link

Vinnl commented Dec 14, 2018

Thanks for working on this at all @mrmckeb - you don't owe me anything, so that's very generous :)

But you were entirely correct! Moving the stories into the src/ folder solved the issue, and the stories get compiled just like my regular components now. Thanks a bunch!

@igor-dv
Copy link
Member

igor-dv commented Dec 14, 2018

@mrmckeb we probably need to add stories dir into the include as well 🤦‍♂️ SB cli creates this dir when you execute sb init...

@mrmckeb
Copy link
Member Author

mrmckeb commented Dec 14, 2018

I actually feel that this is not a good default. I much prefer stories to be alongside components - perhaps that should be the default? This results in less jumping between folders, and a smaller chance that people will forget to update stories after changing components (which I've seen a lot).

Regardless, this is a very easy fix if we want to add it ;)

@igor-dv
Copy link
Member

igor-dv commented Dec 14, 2018

Yeah, that is how most of the people work with stories, IMO. Though I am not sure it's easy to do this with the cli at least now...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants