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

Preview API: Add csf factory utilities #30388

Merged
merged 1 commit into from
Jan 27, 2025

Conversation

yannbf
Copy link
Member

@yannbf yannbf commented Jan 27, 2025

Closes #30236

What I did

This PR introduces base utilities to use when it comes to dealing with stories, meta or preview annotations and either checking or extracting the proper anotations whether they're in csf factory format or not

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 77.9 MB 77.9 MB 17.5 kB 1.86 0%
initSize 132 MB 132 MB 28 kB -0.68 0%
diffSize 53.8 MB 53.8 MB 10.5 kB -0.72 0%
buildSize 7.19 MB 7.19 MB 385 B 1.66 0%
buildSbAddonsSize 1.85 MB 1.85 MB 234 B 0.04 0%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 1.87 MB 1.87 MB 0 B 1.36 0%
buildSbPreviewSize 0 B 0 B 0 B - -
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 3.91 MB 3.91 MB 234 B 1.59 0%
buildPreviewSize 3.28 MB 3.28 MB 151 B 1.78 0%
testBuildSize 0 B 0 B 0 B - -
testBuildSbAddonsSize 0 B 0 B 0 B - -
testBuildSbCommonSize 0 B 0 B 0 B - -
testBuildSbManagerSize 0 B 0 B 0 B - -
testBuildSbPreviewSize 0 B 0 B 0 B - -
testBuildStaticSize 0 B 0 B 0 B - -
testBuildPrebuildSize 0 B 0 B 0 B - -
testBuildPreviewSize 0 B 0 B 0 B - -
name before after diff z %
createTime 24.2s 18.4s -5s -787ms 0.07 -31.4%
generateTime 18.3s 21.4s 3.1s 0.07 14.5%
initTime 12.7s 16.5s 3.7s 1.47 🔺22.8%
buildTime 10.4s 12.2s 1.8s 2.44 🔺15.2%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 4.8s 5.1s 302ms 0.27 5.8%
devManagerResponsive 3.2s 3.5s 270ms -0.44 7.6%
devManagerHeaderVisible 645ms 835ms 190ms 3.74 🔺22.8%
devManagerIndexVisible 672ms 848ms 176ms 3.13 🔺20.8%
devStoryVisibleUncached 1.9s 1.8s -47ms -0.55 -2.5%
devStoryVisible 674ms 871ms 197ms 3.65 🔺22.6%
devAutodocsVisible 539ms 560ms 21ms 0.52 3.8%
devMDXVisible 508ms 583ms 75ms 0.72 12.9%
buildManagerHeaderVisible 521ms 609ms 88ms 0.46 14.4%
buildManagerIndexVisible 523ms 614ms 91ms 0.45 14.8%
buildStoryVisible 514ms 600ms 86ms 0.48 14.3%
buildAutodocsVisible 433ms 451ms 18ms -0.32 4%
buildMDXVisible 481ms 467ms -14ms -0.25 -3%

Greptile Summary

This PR introduces new CSF (Component Story Format) factory utilities to improve handling of story annotations and previews in Storybook's preview API.

  • Added new csf-factory-utils.ts with core functions getCsfFactoryPreview, isCsfFactory, and getCsfFactoryAnnotations for standardized CSF factory handling
  • Modified processCSFFile.ts to support CSF4 factory types by extracting metadata from firstStory.meta.input and firstStory.config.input
  • Updated DocsContext class to use isCsfFactory utility for proper CSF factory story handling
  • Refactored testStory function in test-utils.ts to use new getCsfFactoryAnnotations utility for improved type safety
  • TODO noted for proper CSF factory type definitions that need to be implemented

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

8 file(s) reviewed, 5 comment(s)
Edit PR Review Bot Settings | Greptile

'isCSFFactory' in story ? (story as any).input : story,
'isCSFFactory' in story ? (meta as any).input : meta,
annotations.story,
annotations.meta,
{ initialGlobals: (await getInitialGlobals?.()) ?? {}, tags: await getTags?.() },
Copy link
Contributor

Choose a reason for hiding this comment

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

style: optional chaining on getInitialGlobals could be moved to line 24 where the function is destructured for better error handling

@@ -8,3 +8,4 @@ export * from './getValuesFromArgTypes';
export * from './composeConfigs';
export * from './stepRunners';
export * from './portable-stories';
export * from './csf-factory-utils';
Copy link
Contributor

Choose a reason for hiding this comment

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

style: consider adding a newline at the end of file to match style of other exports

@@ -48,6 +48,7 @@ export function processCSFFile<TRenderer extends Renderer>(

const firstStory: any = Object.values(namedExports)[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

style: casting to any here loses type safety - consider using a more specific type or adding runtime type checks

@@ -48,6 +48,7 @@ export function processCSFFile<TRenderer extends Renderer>(

const firstStory: any = Object.values(namedExports)[0];
// CSF4
// TODO: @kasperpeulen will fix this once csf factory types are defined
if (!defaultExport && 'isCSFFactory' in firstStory) {
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: check for firstStory being undefined before accessing properties to prevent runtime errors

Comment on lines +284 to +285
const { story: storyAnnotations, meta: componentAnnotations } =
getCsfFactoryAnnotations(story);
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Destructuring could fail if getCsfFactoryAnnotations returns undefined. Consider adding a null check or default values.

Copy link

nx-cloud bot commented Jan 27, 2025

View your CI Pipeline Execution ↗ for commit 1cd8644.

Command Status Duration Result
nx run-many -t build --parallel=3 ✅ Succeeded 1m 46s View ↗

☁️ Nx Cloud last updated this comment at 2025-01-27 11:15:42 UTC

@storybook-pr-benchmarking
Copy link

Package Benchmarks

Commit: 1cd8644, ran on 27 January 2025 at 11:22:31 UTC

The following packages have significant changes to their size or dependencies:

@storybook/nextjs

Before After Difference
Dependency count 592 592 0
Self size 474 KB 474 KB 0 B
Dependency size 84.21 MB 84.23 MB 🚨 +17 KB 🚨
Bundle Size Analyzer Link Link

@storybook/react-webpack5

Before After Difference
Dependency count 323 323 0
Self size 8 KB 8 KB 0 B
Dependency size 43.26 MB 43.27 MB 🚨 +15 KB 🚨
Bundle Size Analyzer Link Link

@storybook/vue3-webpack5

Before After Difference
Dependency count 495 495 0
Self size 7 KB 7 KB 0 B
Dependency size 55.65 MB 55.67 MB 🚨 +17 KB 🚨
Bundle Size Analyzer Link Link

storybook

Before After Difference
Dependency count 55 55 0
Self size 22 KB 22 KB 0 B
Dependency size 33.54 MB 33.55 MB 🚨 +10 KB 🚨
Bundle Size Analyzer Link Link

sb

Before After Difference
Dependency count 56 56 0
Self size 1 KB 1 KB 0 B
Dependency size 33.56 MB 33.57 MB 🚨 +10 KB 🚨
Bundle Size Analyzer Link Link

@storybook/cli

Before After Difference
Dependency count 401 401 0
Self size 529 KB 529 KB 0 B
Dependency size 75.61 MB 75.62 MB 🚨 +19 KB 🚨
Bundle Size Analyzer Link Link

@storybook/codemod

Before After Difference
Dependency count 277 277 0
Self size 612 KB 612 KB 0 B
Dependency size 65.39 MB 65.41 MB 🚨 +19 KB 🚨
Bundle Size Analyzer Link Link

create-storybook

Before After Difference
Dependency count 113 113 0
Self size 1.11 MB 1.11 MB 0 B
Dependency size 42.67 MB 42.68 MB 🚨 +10 KB 🚨
Bundle Size Analyzer Link Link

@yannbf yannbf merged commit 5c6599e into kasper/csf-factories Jan 27, 2025
55 of 57 checks passed
@yannbf yannbf deleted the yann/csf-factory-utils-preview-api branch January 27, 2025 16:46
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.

1 participant