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

chore: convert stories to CSF format for typescript stories #253

Merged
merged 18 commits into from Jan 31, 2020

Conversation

mbylstra
Copy link
Contributor

@mbylstra mbylstra commented Jan 30, 2020

branch preview: https://dev.cultureamp.design/dst/upgrade-to-storybook-csf-format/storybook/

Description

Component Story Format is the new format for writing stories. The previous storiesOf format still works, but is deprecated. The new format makes reading documentation much easier: all the docs use the new format and unlocks a lot of potential such as per-story configuration.

Implementation

Storybook provides a codemod for automatically upgrading stories to CSF format, but I found that it only works on .js files, not .tsx. I forked the codemod and fixed it to work on .tsx (https://github.com/cultureamp/kaizen-design-system/pull/253/files#diff-bf825ed2953e17e484e5afe43ad58c6a). This was used to automatically upgrade all of our stories. I've created an issue in the Storybook repo here: storybookjs/storybook#9669, and I hope to submit a PR so others can benefit.

Changes:

  • Storybook dependencies are now on 5.3.9
  • the configuration for finding stories is now in main.js as this is the new convention for 5.3.x (see caveats section)
  • The repo now contains a script for automatically converting stories from storiesOf that was forked from the offical Storybook codemod. The difference is that the fork works on Typescript instead of plain JS.
  • All the stories have been upgraded to CSF format

Caveats:

  • The new config for finding stories is far more specific than previously: (stories: ["../packages/component-library/stories/*.stories.tsx"],). I ran into trouble getting the previous regex or the glob syntax working with this new format. Previously it would find any story in the packages directoy, now it will only find stories in the /packages/component-library/stories direction, which is the only place they are currently found, so it's not a breaking change. I'd like to fix up the regex/glob in a future PR.
  • This only upgrades the typescript stories. There are Elm storiesOf stories that are dynamically generated by https://github.com/cultureamp/elm-storybook. Becuase of this, the console will spit out a lot of "Illegal mix of CSF default export and storiesOf calls in a single file" errors, because storiesOf is being deprecated. I think updating https://github.com/cultureamp/elm-storybook to use CSF would not be difficult (does not require a codemod), but I ran out of time. We might run into future problems if we want to actually use some of the new CSF features like per-story configuration, but I think it's feasible to work that into the elm package.

the addon.ts and config.ts file name convention from <=5.2 has been deprecated
The tsx parser AST the word "StringLiteral" instead of "Literal", which the
js parser AST uses. Because of this, the `storiesOf` stories were being filtered
out, and nothing was being modified. Now it works for tsx.
it seems necessary for CSF to work.
note that this path is much less flexible than the current one:
it only looks for stories in packages/component-library/
I couldn't get the regex or glob to work in the new format, so this will
fixed in a later release. Currently this is the only directory where stories are found.
@mbylstra mbylstra changed the title chore: convert stories to CSF format chore: convert stories to CSF format for typescript stories Jan 30, 2020
@mbylstra mbylstra merged commit 431ffb8 into master Jan 31, 2020
@mbylstra mbylstra deleted the dst/upgrade-to-storybook-csf-format branch January 31, 2020 05:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants