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

WIP: CSF factories #30197

Draft
wants to merge 40 commits into
base: next
Choose a base branch
from
Draft

WIP: CSF factories #30197

wants to merge 40 commits into from

Conversation

kasperpeulen
Copy link
Contributor

@kasperpeulen kasperpeulen commented Jan 6, 2025

Closes #

What I did

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 pull request has been released as version 0.0.0-pr-30197-sha-3753cde1. Try it out in a new sandbox by running npx [email protected] sandbox or in an existing project with npx [email protected] upgrade.

More information
Published version 0.0.0-pr-30197-sha-3753cde1
Triggered by @yannbf
Repository storybookjs/storybook
Branch kasper/csf-factories
Commit 3753cde1
Datetime Mon Jan 6 20:12:44 UTC 2025 (1736194364)
Workflow run 12639617924

To request a new release of this pull request, mention the @storybookjs/core team.

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

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 77.8 MB 77.8 MB 0 B -0.29 0%
initSize 131 MB 134 MB 2.81 MB 2.17 2.1%
diffSize 53 MB 55.8 MB 2.81 MB 2.17 🔺5%
buildSize 7.19 MB 7.19 MB 2.91 kB 42.46 0%
buildSbAddonsSize 1.85 MB 1.85 MB 2.06 kB Infinity 0.1%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 1.87 MB 1.87 MB 12 B 0.9 0%
buildSbPreviewSize 0 B 0 B 0 B - -
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 3.91 MB 3.91 MB 2.08 kB 346.47 0.1%
buildPreviewSize 3.28 MB 3.28 MB 837 B 12.72 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 7s 26s 18.9s 1.56 🔺72.8%
generateTime 20.3s 22.5s 2.2s 1.84 🔺10%
initTime 14.9s 14.6s -249ms 0.83 -1.7%
buildTime 9.9s 8.6s -1s -221ms -1.13 -14%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 4s 4.5s 425ms -1.13 9.4%
devManagerResponsive 3s 3.3s 320ms -1.17 9.6%
devManagerHeaderVisible 497ms 506ms 9ms -1.93 1.8%
devManagerIndexVisible 520ms 512ms -8ms -2.22 -1.6%
devStoryVisibleUncached 1.9s 1.7s -198ms -0.73 -11.2%
devStoryVisible 521ms 539ms 18ms -2.02 3.3%
devAutodocsVisible 434ms 446ms 12ms -1.4 2.7%
devMDXVisible 426ms 418ms -8ms -1.81 -1.9%
buildManagerHeaderVisible 508ms 461ms -47ms -1.76 🔰-10.2%
buildManagerIndexVisible 601ms 462ms -139ms -2.74 🔰-30.1%
buildStoryVisible 499ms 455ms -44ms -1.83 🔰-9.7%
buildAutodocsVisible 403ms 375ms -28ms -2.12 🔰-7.5%
buildMDXVisible 371ms 377ms 6ms -1.94 1.6%

Greptile Summary

Based on the provided files and changes, here's a concise summary of the CSF factories pull request:

Introduces CSF (Component Story Format) factories to provide a more structured and type-safe way to define Storybook configurations and stories.

  • Added defineConfig and meta.story() pattern for type-safe story definitions in /code/renderers/react/src/preview.tsx
  • Modified processCSFFile to detect and handle CSF4 factory format via isCSFFactoryPreview flag
  • Added support for per-CSF-file project annotations that can override global settings
  • Updated test infrastructure to support factory pattern in Vitest transformer and portable stories
  • Added comprehensive test coverage for both traditional CSF and new factory approaches

The changes provide better type safety and structure while maintaining backward compatibility with existing CSF formats. The implementation appears solid but requires careful testing around project annotation overrides and factory pattern adoption.

@kasperpeulen kasperpeulen changed the title CSF factories WIP: CSF factories Jan 6, 2025
@kasperpeulen kasperpeulen marked this pull request as draft January 6, 2025 13:50
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.

20 file(s) reviewed, 19 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines 17 to 19
const [previewFileUrl, ...previewAnnotationURLs] = [...previewAnnotations, previewOrConfigFile]
.filter(Boolean)
.map((path) => processPreviewAnnotation(path, projectRoot));
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 previewAnnotations and previewOrConfigFile are both empty arrays after filtering. Add a check to handle this case.

Comment on lines +26 to +29
const preview = await import('${previewFileUrl}');
const csfFactoryPreview = Object.values(preview).find(module => {
return 'isCSFFactoryPreview' in module
});
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Object.values() and find() could return undefined. Need type checking and error handling for when no CSF factory preview is found.

const configs = await Promise.all([${previewAnnotationURLs
.map(
(previewAnnotation, index) =>
// Prefer the updated module from an HMR update, otherwise import the original module
`hmrPreviewAnnotationModules[${index}] ?? import('${previewAnnotation}')`
)
.join(',\n')}])
return composeConfigs(configs);
return composeConfigs([...configs, preview]);
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: preview is used in composeConfigs even when csfFactoryPreview is found, which could cause conflicts. Should only include preview in non-factory case.

@@ -27,11 +27,12 @@ export const testStory = (
return async (context: TestContext & TaskContext & { story: ComposedStoryFn }) => {
const composedStory = composeStory(
story,
meta,
'isCSFFactory' in story ? (meta as any).annotations : meta,
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Type casting to 'any' should be avoided. Consider creating proper type definitions for CSF factories to maintain type safety.

{ initialGlobals: (await getInitialGlobals?.()) ?? {} },
undefined,
exportName
);

if (composedStory === undefined || skipTags?.some((tag) => composedStory.tags.includes(tag))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Optional chaining on skipTags is unnecessary since it's a required parameter

Comment on lines 182 to 190
const div = document.createElement('div');
document.body.appendChild(div);

await CSF3InputFieldFilled.run({ canvasElement: div });

const input = screen.getByTestId('input') as HTMLInputElement;
expect(input.value).toEqual('Hello world!');

document.body.removeChild(div);
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Test cleanup could fail if test errors before removeChild. Consider using try/finally

import { Button } from './Button';

// eslint-disable-next-line storybook/default-exports
const config = defineConfig({ args: { children: 'TODO: THIS IS NOT WORKING YET' } });
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: TODO comment indicates unfinished/broken functionality. This should be resolved before merging.

});

export const CSF2StoryWithParamsAndDecorator = meta.story({
render: (args: any) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Using any type here loses type safety. Consider using proper Button component prop types.

Comment on lines 31 to 33
readonly meta = <TComponent extends ComponentType<any>, TMetaArgs extends Args>(
meta: ComponentAnnotations<TRenderer, any> & { component: TComponent; args: TMetaArgs }
) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: The meta method accepts any for TRenderer annotations but then casts to TMetaArgs. This could lead to type safety issues. Consider constraining the any type.

public meta: Meta<TRenderer, TArgs>,
public config: PreviewConfig<TRenderer>
) {
Object.assign(this, annotations);
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Using Object.assign to copy all properties from annotations could lead to property collisions with the class's own methods/properties. Consider explicit property copying instead.

Copy link

nx-cloud bot commented Jan 6, 2025

View your CI Pipeline Execution ↗ for commit 30511e9.

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

☁️ Nx Cloud last updated this comment at 2025-01-08 15:41:16 UTC

@storybook-pr-benchmarking
Copy link

storybook-pr-benchmarking bot commented Jan 6, 2025

Package Benchmarks

Commit: 30511e9, ran on 8 January 2025 at 15:50:33 UTC

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

@storybook/addon-essentials

Before After Difference
Dependency count 36 36 0
Self size 17 KB 20 KB 🚨 +3 KB 🚨
Dependency size 13.83 MB 13.84 MB 🚨 +119 B 🚨
Bundle Size Analyzer Link Link

@storybook/core

Before After Difference
Dependency count 53 53 0
Self size 19.08 MB 21.46 MB 🚨 +2.38 MB 🚨
Dependency size 14.43 MB 14.43 MB 0 B
Bundle Size Analyzer Link Link

@storybook/experimental-nextjs-vite

Before After Difference
Dependency count 153 152 🎉 -1 🎉
Self size 232 KB 231 KB 🎉 -622 B 🎉
Dependency size 44.70 MB 28.88 MB 🎉 -15.82 MB 🎉
Bundle Size Analyzer Link Link

@storybook/nextjs

Before After Difference
Dependency count 592 592 0
Self size 472 KB 472 KB 🎉 -622 B 🎉
Dependency size 83.35 MB 83.78 MB 🚨 +422 KB 🚨
Bundle Size Analyzer Link Link

@storybook/react-native-web-vite

Before After Difference
Dependency count 114 105 🎉 -9 🎉
Self size 43 KB 42 KB 🎉 -1 KB 🎉
Dependency size 18.22 MB 18.46 MB 🚨 +238 KB 🚨
Bundle Size Analyzer Link Link

@storybook/react-vite

Before After Difference
Dependency count 87 87 0
Self size 13 KB 13 KB 0 B
Dependency size 16.03 MB 16.45 MB 🚨 +423 KB 🚨
Bundle Size Analyzer Link Link

@storybook/react-webpack5

Before After Difference
Dependency count 323 323 0
Self size 6 KB 6 KB 🎉 -27 B 🎉
Dependency size 42.41 MB 42.83 MB 🚨 +422 KB 🚨
Bundle Size Analyzer Link Link

storybook

Before After Difference
Dependency count 54 54 0
Self size 22 KB 22 KB 0 B
Dependency size 33.51 MB 35.89 MB 🚨 +2.38 MB 🚨
Bundle Size Analyzer Link Link

sb

Before After Difference
Dependency count 55 55 0
Self size 1 KB 1 KB 0 B
Dependency size 33.53 MB 35.91 MB 🚨 +2.38 MB 🚨
Bundle Size Analyzer Link Link

@storybook/cli

Before After Difference
Dependency count 396 396 0
Self size 496 KB 496 KB 🎉 -165 B 🎉
Dependency size 75.43 MB 77.81 MB 🚨 +2.38 MB 🚨
Bundle Size Analyzer Link Link

@storybook/codemod

Before After Difference
Dependency count 276 276 0
Self size 612 KB 612 KB 0 B
Dependency size 65.32 MB 67.70 MB 🚨 +2.38 MB 🚨
Bundle Size Analyzer Link Link

@storybook/core-webpack

Before After Difference
Dependency count 1 3 🚨 +2 🚨
Self size 16 KB 16 KB 0 B
Dependency size 28 KB 2.40 MB 🚨 +2.37 MB 🚨
Bundle Size Analyzer Link Link

create-storybook

Before After Difference
Dependency count 112 112 0
Self size 1.11 MB 1.11 MB 0 B
Dependency size 42.64 MB 45.03 MB 🚨 +2.38 MB 🚨
Bundle Size Analyzer Link Link

@storybook/preset-preact-webpack

Before After Difference
Dependency count 2 4 🚨 +2 🚨
Self size 5 KB 5 KB 0 B
Dependency size 44 KB 2.41 MB 🚨 +2.37 MB 🚨
Bundle Size Analyzer Link Link

@storybook/preset-react-webpack

Before After Difference
Dependency count 191 191 0
Self size 24 KB 24 KB 0 B
Dependency size 33.49 MB 33.91 MB 🚨 +422 KB 🚨
Bundle Size Analyzer Link Link

@storybook/preset-server-webpack

Before After Difference
Dependency count 18 20 🚨 +2 🚨
Self size 10 KB 10 KB 0 B
Dependency size 1.48 MB 3.85 MB 🚨 +2.37 MB 🚨
Bundle Size Analyzer Link Link

@storybook/preset-svelte-webpack

Before After Difference
Dependency count 101 103 🚨 +2 🚨
Self size 14 KB 14 KB 0 B
Dependency size 10.40 MB 12.77 MB 🚨 +2.37 MB 🚨
Bundle Size Analyzer Link Link

@storybook/react

Before After Difference
Dependency count 6 6 0
Self size 932 KB 1.35 MB 🚨 +422 KB 🚨
Dependency size 24 KB 24 KB 0 B
Bundle Size Analyzer Link Link

@yannbf yannbf added ci:merged Run the CI jobs that normally run when merged. and removed ci:normal labels Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci:merged Run the CI jobs that normally run when merged. feature request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants