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

React: Switch react-docgen-typescript-loader to react-docgen-typescript-plugin #11106

Merged
merged 3 commits into from
Jun 11, 2020
Merged

React: Switch react-docgen-typescript-loader to react-docgen-typescript-plugin #11106

merged 3 commits into from
Jun 11, 2020

Conversation

hipstersmoothie
Copy link
Contributor

@hipstersmoothie hipstersmoothie commented Jun 9, 2020

What I did

Typescript docgen for react was super slow!
This pull request switches from react-docgen-typescript-loader to react-docgen-typescript-plugin

Differences:

  • In a webpack plugin instead of loader
  • This allows for the typescript docgen to happen all at once and be non-blocking. (as a loader it synchronously executes for each file)
  • It also allows the plugin to only create the typescript compiler and docgen parser once instead of for every file

In my own project this resulted in a huge speed boost:

Before:

Screen Shot 2020-06-09 at 12 14 43 PM

After:

Screen Shot 2020-06-09 at 1 59 57 PM

How to test

  • Is this testable with Jest or Chromatic screenshots? no
  • Does this need a new example in the kitchen sink apps? no
  • Does this need an update to the documentation? yes

If your answer is yes to any of these, please make sure to include it in your PR.

@hipstersmoothie hipstersmoothie added the patch:yes Bugfix & documentation PR that need to be picked to main branch label Jun 9, 2020
@hipstersmoothie hipstersmoothie removed the patch:yes Bugfix & documentation PR that need to be picked to main branch label Jun 9, 2020
Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

This looks amazing, I can't wait to kick the tires.

This is is a pretty big change so expect some back and forth before we get it merged. First question: what about these:

https://github.com/storybookjs/storybook/blob/next/lib/core/src/server/config/defaults.js#L4-L7

@shilman
Copy link
Member

shilman commented Jun 9, 2020

cc @mrmckeb

@hipstersmoothie
Copy link
Contributor Author

hipstersmoothie commented Jun 9, 2020

what about these:

Both of those options will be respected.

https://www.npmjs.com/package/react-docgen-typescript-plugin

The only thing I changed with the options for the plugin are skipPropsWithName skipPropsWithoutDoc, and compilerOptions.

skipPropsWithName skipPropsWithoutDoc

Instead of creating and extra options we just pass propFilter to react-docgen-typescript. Those two options are handled there.

https://github.com/strothj/react-docgen-typescript-loader

compilerOptions

I can add this in if everyone wants it, but it seems way simpler to me to just point at a tsconfig in the repo.

@shilman shilman added this to the 6.0 typescript milestone Jun 9, 2020
@shilman shilman changed the title switch from react-docgen-typescript-loader to react-docgen-typescript-plugin React: Switch from react-docgen-typescript-loader to react-docgen-typescript-plugin Jun 9, 2020
@shilman shilman added react and removed core labels Jun 9, 2020
Copy link
Member

@mrmckeb mrmckeb left a comment

Choose a reason for hiding this comment

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

A few questions @hipstersmoothie, but looks good overall.

I can add this in if everyone wants it, but it seems way simpler to me to just point at a tsconfig in the repo.
You mean you would look for a root config?

@@ -43,8 +43,7 @@ export const convert = (type: PTType): SBType | any => {
case 'elementType':
default: {
if (name?.indexOf('|') > 0) {
// react-docgen-typescript-loader doesn't always produce proper
// enum types, possibly due to https://github.com/strothj/react-docgen-typescript-loader/issues/81
// react-docgen-typescript-plugin doesn't always produce proper
Copy link
Member

Choose a reason for hiding this comment

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

This comment seems to have only been partially removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This comment relates to the issue @shilman commented on. Will try to resolve this in the underlying library and remove this comment entirely

Copy link
Member

Choose a reason for hiding this comment

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

I understand, but one line was removed and now it doesn't make sense, right?

// react-docgen-typescript-plugin doesn't always produce proper
// this hack tries to parse out values from the string and should be
// removed when RDTL gets a little smarter about this

@@ -27,24 +30,13 @@ export function babel(config: TransformOptions, { typescriptOptions }: Storybook

export function webpackFinal(config: Configuration, { typescriptOptions }: StorybookOptions) {
const { reactDocgen, reactDocgenTypescriptOptions } = typescriptOptions;
if (reactDocgen !== 'react-docgen-typescript') return config;

if (reactDocgen !== 'react-docgen-typescript') {
Copy link
Member

Choose a reason for hiding this comment

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

Why was this changed?

Copy link
Member

Choose a reason for hiding this comment

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

I personally prefer the new code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can change it back but one line if statement throw me for a loop. the function above this one also had this style of if statement. Can change back if wanted.

Copy link
Member

Choose a reason for hiding this comment

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

I was more pointing out that this change isn't related to any of the work. I think it's good practice to try to only change things related to the task at hand, unless something is being improved. But if you think the change is meaningful, then I'm OK with that.

@mrmckeb
Copy link
Member

mrmckeb commented Jun 10, 2020

@shilman when this merges, we can make this change in CRA too.

@shilman
Copy link
Member

shilman commented Jun 10, 2020

The chromatic snapshots look pretty good. ❤️ @hipstersmoothie please take a look at the "enum"-related snapshot diffs. My enum handling code for RDTL is a hack, and it got "slightly worse" in your version. I'm not concerned about the regressions since it didn't work well in the first place. But I wanted to point it out because if you're going to do more hacking on react-docgen-typescript/(-plugin), that would be a really great area to give some attention to.

@hipstersmoothie
Copy link
Contributor Author

You mean you would look for a root config?

Previously with react-docgen-typescript-loader you could either use the default TS options, provide a path to a tsconfig, or provide complierOptions directly to the loader. I didn't implement directly providing compilerOptions in the plugin but it shouldn't be hard

@hipstersmoothie
Copy link
Contributor Author

@shilman you're talking about how react-docgen-typescript outputs unions like this right?

Screen Shot 2020-06-10 at 9 13 51 AM

You'd rather it output to be an enum with a values array?

@hipstersmoothie
Copy link
Contributor Author

@shilman Opened a PR on react-docgen-typescript to add an option that will expand complex unions.

styleguidist/react-docgen-typescript#261

Previously the following example would output

Screen Shot 2020-06-10 at 9 52 18 AM

With the new option the package will now expand the above to

Screen Shot 2020-06-10 at 9 51 31 AM

@hipstersmoothie
Copy link
Contributor Author

I've added the compilerOptions option now

hipstersmoothie/react-docgen-typescript-plugin#1

@shilman
Copy link
Member

shilman commented Jun 11, 2020

@hipstersmoothie that was fast! and awesome!! I'll try to kick the tires on this today and hopefully merge.

@shilman
Copy link
Member

shilman commented Jun 11, 2020

Looking fantastic!! 🔥 Here are the numbers I'm seeing for official-storybook on my machine:

  • react-docgen: 17 s for manager and 21 s for preview
  • react-docgen-typescript (before): 36 s for manager and 46 s for preview
  • react-docgen-typescript (after): 15 s for manager and 31 s for preview

@shilman shilman changed the title React: Switch from react-docgen-typescript-loader to react-docgen-typescript-plugin React: Switch react-docgen-typescript-loader to react-docgen-typescript-plugin Jun 11, 2020
@shilman shilman merged commit 62cd262 into storybookjs:next Jun 11, 2020
@hipstersmoothie hipstersmoothie deleted the docgen-typescript branch June 11, 2020 15:41
@mrmckeb
Copy link
Member

mrmckeb commented Jun 12, 2020

@hipstersmoothie It looks like this has introduced a regression, as react-docgen-typescript-plugin loads for all files (not just TypeScript), which means it is also injected for projects that aren't using TypeScript at all - and fails, as the typescript package is not found.

You can see here that we used to only target tsx? files. Is it possible to resolve this within the plugin?
https://github.com/storybookjs/storybook/pull/11106/files#diff-f65f40b676691502256f2800665754e1L38

@hipstersmoothie
Copy link
Contributor Author

I actually just released an update with this functionality. 0.3.0 should only include tsx files.

@mrmckeb
Copy link
Member

mrmckeb commented Jun 12, 2020

That's great news, and I'm guessing it only loads TypeScript as a dependency when ts and tsx files are found?

@hipstersmoothie
Copy link
Contributor Author

Are you asking if I can conditionally require typescript if it’s installed? I’m assuming the loader only ever got loaded if a typescript file was found

@mrmckeb
Copy link
Member

mrmckeb commented Jun 12, 2020

Yes, that was the previous behaviour. With this change, the plugin is always invoked - but we know that TypeScript may not be installed at all times. Do you conditionally require TypeScript now?

If not, we may need to revert this temporarily, as it will break in any projects that don't have a typescript dependency.

@hipstersmoothie
Copy link
Contributor Author

PR incoming https://github.com/hipstersmoothie/react-docgen-typescript-plugin/releases/tag/v0.4.0

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.

4 participants