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

Support TypeScript out of the box #750

Closed
4 tasks done
sapegin opened this issue Dec 21, 2017 · 22 comments
Closed
4 tasks done

Support TypeScript out of the box #750

sapegin opened this issue Dec 21, 2017 · 22 comments

Comments

@sapegin
Copy link
Member

sapegin commented Dec 21, 2017

  • Upgrade react-docgen to 5.0.0 beta (Upgrade react-docgen to 5.0.0 beta #1354)
  • Tweak default components and ignore patterns to include .ts and .tsx.
  • Tweak default extensions in webpack config to include .ts and .tsx.
  • Update docs maybe (search for typescript and see if the information is still relevant).
sapegin added a commit that referenced this issue Jan 21, 2018
## New features

* New option `editorConfig` to change CodeMirror options (#648, [#662](#662) by @SaraVieira)
* Allow descriptions for sections ([#743](#743) by @SaraVieira)
* Add TypeScript files to default components and ignore patterns ([#749](#749), part of [#750](#750))

## Bug fixes

* Fix global access to all components in isolation mode ([#738](#738))
* Fix color issue in Safari ([#739](#739))
* Allow overriding of renderer-only components ([#710](#710))
* Do not overflow floated elements in examples (#772, [#773](#773) by @roblevintennis)
* HTML escaping in Add example block ([#741](#741))
* Fix infinite loop caused by markdown-to-jsx ([#742](#742))
sapegin added a commit that referenced this issue Jan 21, 2018
## New features

* New option `editorConfig` to change CodeMirror options (#648, [#662](#662) by @SaraVieira)
* Allow descriptions for sections ([#743](#743) by @SaraVieira)
* Add TypeScript files to default components and ignore patterns ([#749](#749), part of [#750](#750))

## Bug fixes

* Fix global access to all components in isolation mode ([#738](#738))
* Fix color issue in Safari ([#739](#739))
* Allow overriding of renderer-only components ([#710](#710))
* Do not overflow floated elements in examples (#772, [#773](#773) by @roblevintennis)
* HTML escaping in Add example block ([#741](#741))
* Fix infinite loop caused by markdown-to-jsx ([#742](#742))
sapegin added a commit that referenced this issue Jan 21, 2018
## New features

* New option `editorConfig` to change CodeMirror options (#648, [#662](#662) by @SaraVieira)
* Allow descriptions for sections ([#743](#743) by @SaraVieira)
* Add TypeScript files to default components and ignore patterns ([#749](#749), part of [#750](#750))

## Bug fixes

* Fix global access to all components in isolation mode ([#738](#738))
* Fix color issue in Safari ([#739](#739))
* Allow overriding of renderer-only components ([#710](#710))
* Do not overflow floated elements in examples (#772, [#773](#773) by @roblevintennis)
* HTML escaping in Add example block ([#741](#741))
* Fix infinite loop caused by markdown-to-jsx ([#742](#742))
@stereobooster
Copy link

stereobooster commented Feb 20, 2018

I do not want to open an separate issue, because I'm not sure if this an issue of styleguidist or my setup. I bootstrapped project with https://github.com/wmonk/create-react-app-typescript

My config is:

const path = require("path");
module.exports = {
  propsParser: require("react-docgen-typescript").withCustomConfig(
    "./tsconfig.json"
  ).parse
};

I get the following error

InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided ('static/media/Navbar.b192c68a.tsx

UPD: fixed by adding this to the config

webpackConfig: require("react-scripts-ts/config/webpack.config.dev.js")

@nwinger

This comment has been minimized.

@nwinger

This comment has been minimized.

@styleguidist styleguidist locked as off-topic and limited conversation to collaborators Feb 28, 2018
@okonet
Copy link
Member

okonet commented Apr 16, 2018

@sapegin I might need this and could work on this. I'm not sure about the second step, though. Any pointers?

@sapegin
Copy link
Member Author

sapegin commented Apr 17, 2018

@okonet Probably that's what withDefaultConfig is doing but the docs aren't super clear on that, and I haven't looked at the code. Similar to how we (and webpack) load webpack.config.js by default — load tsconfig.json by default like TypeScript does.

@styleguidist styleguidist unlocked this conversation Sep 19, 2018
@bebraw
Copy link

bebraw commented Sep 19, 2018

Make react-docgen-typescript support default export the way they work in Styleguidist for JavaScript.

I think that's done by now.

Make react-docgen-typescript read users’s tsconfig.json by default.

Seems to work.

Automatically enable react-docgen-typescript for TypeScript projects (check typescript in devDependencies?

I need a spec for this. How should we depend on react-docgen-typescript? What's the preferred approach? It doesn't come with any deps and it's controlled. I have a basic setup to adapt.

@sapegin
Copy link
Member Author

sapegin commented Sep 20, 2018

I think your approach with extension checking is the right one!

@sapegin
Copy link
Member Author

sapegin commented Sep 20, 2018

Make react-docgen-typescript read users’s tsconfig.json by default.

I'm not sure about that after looking at the code ;-/

@okonet
Copy link
Member

okonet commented Sep 20, 2018

Yep, we should resolve the path carefully.

@tsiq-jeremy
Copy link

I was doing a little bit of research on this and it looks like react-docgen is trying to support typescript, but it seems to be in early stages. If they did support TS there would not be a need for react-docgen-typescript anymore.

Automatically enable react-docgen-typescript for TypeScript projects (check typescript in devDependencies?

Seems doable, just update props-loader.js to check if TS is supported then use react-docgen-typescript if it is.

@s-h-a-d-o-w
Copy link

In case you are still considering this, I would recommend you run react-docgen-typescript against some slightly complex cases such as a formerly Flow-typed FC that destructures props, assigns default values to them and uses typed objects for some of the props - some of which are optional.

Unfortunately, none of that works well, as I have discovered today, after migrating our UI component library at work from Flow to TS. Default values are missing, the structure of typed objects is not passed to styleguidist and optional props are described as propType | undefined.
And so we are currently struggling with whether we can fix this in a reasonable amount of time or have to build our API docs some other way. But we've enjoyed using styleguidist with Flow-typed components very much, so we're hoping for the former.

But that's why I'd recommend waiting for that react-docgen TS support that @tsiq-jeremy mentioned.

@s-h-a-d-o-w
Copy link

s-h-a-d-o-w commented Apr 1, 2019

The beautiful thing is... possibly due to babel magic (gotta love it), it looks like people who need TS support right now can actually already have it. I've played around with the Button component in the "basic" example:
https://github.com/s-h-a-d-o-w/react-styleguidist/tree/basic_TS_example

Looks just wonderful to me. Not sure whether it has any flaws but... I'll definitely try this out on our library at work soon. 😉

UPDATE Yup, works great as long as one uses type and not interface for prop types...

button_ts_props

@devongovett
Copy link

FYI, I'm working on TypeScript support for react-docgen here. Feel free to test it out: reactjs/react-docgen#348

@sapegin sapegin added this to the 10.0.0: TypeScript milestone May 6, 2019
@J-Kallunki
Copy link
Contributor

Should this also include support for customizing rsg-components? Got typescript working fine for presented components, but creating custom rsg-components with styleguideComponents seems to be a bit harder when using TS.

@sapegin
Copy link
Member Author

sapegin commented May 21, 2019

@J-Kallunki which problems do you have? There shouldn't be any difference ;-/

@J-Kallunki
Copy link
Contributor

I'm using Webpack and awesome-typescript-loader.

ComponentsList.tsx:

import React, { Component } from 'react';
import { default as DefaultComponentsList } from 'react-styleguidist/lib/client/rsg-components/ComponentsList/ComponentsList';
export default class ComponentsList extends Component {
  render() {
    return <DefaultComponentsList {...this.props} />;
  }
}

Typings (minimal):

declare module 'react-styleguidist/lib/client/rsg-components/ComponentsList/ComponentsList' {
  interface ComponentsListProps {
    [key: string]: any | any[];
  }

  const ComponentsList: React.FunctionComponent<ComponentsListProps>;
  export default ComponentsList;
}

Result:

./node_modules/react-styleguidist/lib/client/rsg-components/ComponentsList/ComponentsList.js
Module not found: Can't resolve 'rsg-components/ComponentsList/ComponentsListRenderer'

So I would need to configure babel just for it and use only non-TS custom components?

@sapegin
Copy link
Member Author

sapegin commented May 21, 2019

I don't think you need to configure Babel, everything in lib is already compiled. But you need to configure TypeScript for your ComponentsList.tsx. Or you many need to define typings for rsg-components/ComponentsList/ComponentsListRenderer too, I don't know TypeScript enough unfortunately.

@J-Kallunki
Copy link
Contributor

I don't think you need to configure Babel, everything in lib is already compiled. But you need to configure TypeScript for your ComponentsList.tsx. Or you many need to define typings for rsg-components/ComponentsList/ComponentsListRenderer too, I don't know TypeScript enough unfortunately.

I got the earlier example working just by defining to moduleAliases:

'rsg-components/ComponentsList/ComponentsListRenderer': path.resolve(__dirname, 'node_modules/react-styleguidist/lib/client/rsg-components/ComponentsList/ComponentsListRenderer'),

Is it the best way to do it - it is the simplest - or should it be also documented how to use custom srg components with TS?

@sapegin
Copy link
Member Author

sapegin commented May 22, 2019

Have you tried this?

Or you many need to define typings for rsg-components/ComponentsList/ComponentsListRenderer too

@J-Kallunki
Copy link
Contributor

Have you tried this?

Or you many need to define typings for rsg-components/ComponentsList/ComponentsListRenderer too

Yeah tried that with the same way as it works for the ComponentsList but not helping for that.

It is clearly saying that cannot find ComponentsListRenderer when ComponentsListis called from the customwrapper ComponentsList-component:

Module not found: Can't resolve 'rsg-components/ComponentsList/ComponentsListRenderer'

@sapegin
Copy link
Member Author

sapegin commented Jun 5, 2019

@J-Kallunki I may have fixed that in 9e29c10, at least I had the same error ;-)

@plumpNation
Copy link

plumpNation commented Jun 12, 2019

The error:

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

I am not certain this is the same issue, apologies if not. I'm using create-react-app, I made a tsx styleguideComponents Wrapper, and setup propsParser: require('react-docgen-typescript').parse.

"react-docgen-typescript": "^1.12.4",
"react-styleguidist": "^9.1.6"

I created the smallest reproduction of this that I could and made a repo here.

https://github.com/plumpNation/cra-styleguidist-typescript

Thanks for the excellent tool, I will just switch the wrapper to jsx until I find out how to support tsx styleguideComponents.

EDIT: I switched to jsx and I still have the "missing loader" error so I may be doing something very wrong.

EDIT2: I had a break and it occurred to me that the styleguideComponents probably need to be in the standard CRA src folder. Now it works fine, repo updated with the "fix", sorry to bother you.

I'll let you delete or allow this comment to remain. Again, thanks for the library.

@sapegin sapegin closed this as completed Oct 21, 2020
artem0723 pushed a commit to artem0723/React-Styleguidist that referenced this issue Oct 28, 2021
## New features

* New option `editorConfig` to change CodeMirror options (#648, [#662](styleguidist/react-styleguidist#662) by @SaraVieira)
* Allow descriptions for sections ([#743](styleguidist/react-styleguidist#743) by @SaraVieira)
* Add TypeScript files to default components and ignore patterns ([#749](styleguidist/react-styleguidist#749), part of [#750](styleguidist/react-styleguidist#750))

## Bug fixes

* Fix global access to all components in isolation mode ([#738](styleguidist/react-styleguidist#738))
* Fix color issue in Safari ([#739](styleguidist/react-styleguidist#739))
* Allow overriding of renderer-only components ([#710](styleguidist/react-styleguidist#710))
* Do not overflow floated elements in examples (#772, [#773](styleguidist/react-styleguidist#773) by @roblevintennis)
* HTML escaping in Add example block ([#741](styleguidist/react-styleguidist#741))
* Fix infinite loop caused by markdown-to-jsx ([#742](styleguidist/react-styleguidist#742))
artem0723 pushed a commit to artem0723/React-Styleguidist that referenced this issue Oct 28, 2021
## New features

* New option `editorConfig` to change CodeMirror options (#648, [#662](styleguidist/react-styleguidist#662) by @SaraVieira)
* Allow descriptions for sections ([#743](styleguidist/react-styleguidist#743) by @SaraVieira)
* Add TypeScript files to default components and ignore patterns ([#749](styleguidist/react-styleguidist#749), part of [#750](styleguidist/react-styleguidist#750))

## Bug fixes

* Fix global access to all components in isolation mode ([#738](styleguidist/react-styleguidist#738))
* Fix color issue in Safari ([#739](styleguidist/react-styleguidist#739))
* Allow overriding of renderer-only components ([#710](styleguidist/react-styleguidist#710))
* Do not overflow floated elements in examples (#772, [#773](styleguidist/react-styleguidist#773) by @roblevintennis)
* HTML escaping in Add example block ([#741](styleguidist/react-styleguidist#741))
* Fix infinite loop caused by markdown-to-jsx ([#742](styleguidist/react-styleguidist#742))
artem0723 pushed a commit to artem0723/React-Styleguidist that referenced this issue Oct 28, 2021
## New features

* New option `editorConfig` to change CodeMirror options (#648, [#662](styleguidist/react-styleguidist#662) by @SaraVieira)
* Allow descriptions for sections ([#743](styleguidist/react-styleguidist#743) by @SaraVieira)
* Add TypeScript files to default components and ignore patterns ([#749](styleguidist/react-styleguidist#749), part of [#750](styleguidist/react-styleguidist#750))

## Bug fixes

* Fix global access to all components in isolation mode ([#738](styleguidist/react-styleguidist#738))
* Fix color issue in Safari ([#739](styleguidist/react-styleguidist#739))
* Allow overriding of renderer-only components ([#710](styleguidist/react-styleguidist#710))
* Do not overflow floated elements in examples (#772, [#773](styleguidist/react-styleguidist#773) by @roblevintennis)
* HTML escaping in Add example block ([#741](styleguidist/react-styleguidist#741))
* Fix infinite loop caused by markdown-to-jsx ([#742](styleguidist/react-styleguidist#742))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants