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: Codemods and refactorings #30292

Merged
merged 27 commits into from
Jan 21, 2025
Merged

Conversation

yannbf
Copy link
Member

@yannbf yannbf commented Jan 17, 2025

Closes #30243
Closes #30239
Closes #30240
Closes #30316

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 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 18.7 kB 3.81 0%
initSize 131 MB 131 MB 20 kB -2.34 0%
diffSize 53.4 MB 53.4 MB 1.23 kB -2.38 0%
buildSize 7.19 MB 7.19 MB -397 B 0.41 0%
buildSbAddonsSize 1.85 MB 1.85 MB -397 B -1.9 0%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 1.87 MB 1.87 MB 0 B 2.95 0%
buildSbPreviewSize 0 B 0 B 0 B - -
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 3.91 MB 3.91 MB -397 B -1.65 0%
buildPreviewSize 3.28 MB 3.28 MB 0 B 0.42 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 6.9s 10.5s 3.5s -0.8 33.6%
generateTime 20.7s 26.9s 6.1s 3.34 🔺22.9%
initTime 13.7s 15.2s 1.4s 0.36 9.7%
buildTime 11.8s 9.1s -2s -702ms -0.39 -29.6%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 5.5s 5.7s 281ms 2.2 4.8%
devManagerResponsive 3.9s 4s 69ms 1.58 1.7%
devManagerHeaderVisible 609ms 701ms 92ms 2.08 🔺13.1%
devManagerIndexVisible 620ms 731ms 111ms 1.98 🔺15.2%
devStoryVisibleUncached 1.7s 2.1s 309ms 1.28 🔺14.7%
devStoryVisible 639ms 732ms 93ms 2.06 🔺12.7%
devAutodocsVisible 553ms 626ms 73ms 1.88 🔺11.7%
devMDXVisible 516ms 624ms 108ms 2.14 🔺17.3%
buildManagerHeaderVisible 547ms 645ms 98ms 1.41 🔺15.2%
buildManagerIndexVisible 551ms 648ms 97ms 1.41 🔺15%
buildStoryVisible 539ms 633ms 94ms 1.39 🔺14.8%
buildAutodocsVisible 500ms 564ms 64ms 1.87 🔺11.3%
buildMDXVisible 459ms 521ms 62ms 0.91 11.9%

Greptile Summary

This PR implements significant changes to Storybook's configuration system, introducing new type-safe helpers for defining configurations and improving addon synchronization across frameworks.

  • Added defineMain and definePreview type helpers across all framework packages to replace defineConfig
  • Implemented new CLI command for synchronizing addons between main.ts and preview configurations
  • Added codemod utilities in code/lib/cli-storybook/src/codemod/ for transforming stories to CSF4 format
  • Added getStorybookData helper in CLI tools to consolidate configuration loading
  • Removed old csf-3-to-4 transform from @storybook/codemod package in favor of new implementation

@yannbf yannbf added maintenance User-facing maintenance tasks ci:merged Run the CI jobs that normally run when merged. labels Jan 17, 2025
@yannbf yannbf self-assigned this Jan 17, 2025
Copy link

nx-cloud bot commented Jan 17, 2025

View your CI Pipeline Execution ↗ for commit 2ec88ee.

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

☁️ Nx Cloud last updated this comment at 2025-01-21 07:19:23 UTC

@storybook-pr-benchmarking
Copy link

storybook-pr-benchmarking bot commented Jan 17, 2025

Package Benchmarks

Commit: 2ec88ee, ran on 21 January 2025 at 07:23:55 UTC

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

@storybook/preact-vite

Before After Difference
Dependency count 13 13 0
Self size 6 KB 6 KB 🚨 +616 B 🚨
Dependency size 1.31 MB 1.31 MB 🎉 -12 B 🎉
Bundle Size Analyzer Link Link

@storybook/react-webpack5

Before After Difference
Dependency count 323 323 0
Self size 6 KB 7 KB 🚨 +662 B 🚨
Dependency size 42.87 MB 42.88 MB 🚨 +4 KB 🚨
Bundle Size Analyzer Link Link

@storybook/cli

Before After Difference
Dependency count 398 400 🚨 +2 🚨
Self size 503 KB 531 KB 🚨 +28 KB 🚨
Dependency size 75.57 MB 75.59 MB 🚨 +15 KB 🚨
Bundle Size Analyzer Link Link

@yannbf yannbf force-pushed the yann/csf-factories-base branch from f75b007 to 80c80ac Compare January 20, 2025 20:49
@yannbf yannbf marked this pull request as ready for review January 21, 2025 11:30
@yannbf yannbf merged commit 3a60679 into kasper/csf-factories Jan 21, 2025
67 of 69 checks passed
@yannbf yannbf deleted the yann/csf-factories-base branch January 21, 2025 11:30
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.

78 file(s) reviewed, 29 comment(s)
Edit PR Review Bot Settings | Greptile

@@ -1,12 +1,12 @@
import { join } from 'node:path';

import type { StorybookConfig } from '../frameworks/react-vite';
import { defineMain } from '../frameworks/react-vite';

const componentsPath = join(__dirname, '../core/src/components');
const managerApiPath = join(__dirname, '../core/src/manager-api');
const imageContextPath = join(__dirname, '..//frameworks/nextjs/src/image-context.ts');
Copy link
Contributor

Choose a reason for hiding this comment

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

syntax: extra forward slash in path '..//frameworks/nextjs/src/image-context.ts'

Comment on lines +18 to +22
const isCsfFactory =
t.isCallExpression(storyExport) &&
t.isMemberExpression(storyExport.callee) &&
t.isIdentifier(storyExport.callee.object) &&
storyExport.callee.object.name === 'meta';
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: This factory detection could miss edge cases where meta is imported under a different name. Consider making this more robust by checking the call signature rather than just the name.

Comment on lines +3 to +5
export function defineMain(config: StorybookConfig) {
return config;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

style: this identity function provides type checking but no runtime validation - consider adding runtime validation of the config object structure

Comment on lines +3 to +5
export function defineMain(config: StorybookConfig) {
return config;
}
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 marking this function as readonly to prevent accidental mutation of the config object

Comment on lines +3 to +5
export function defineMain(config: StorybookConfig) {
return config;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: this function provides no runtime validation or transformation of the config object, which could lead to invalid configurations being accepted silently

if (addon === '@storybook/addon-essentials') {
data.importPath = '@storybook/addon-essentials/entry-preview';
} else {
require.resolve(path.join(addon, 'preview'));
Copy link
Contributor

Choose a reason for hiding this comment

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

style: require.resolve result is unused. Consider storing the resolved path in data.importPath

* This goes through all mainConfig.addons, read their package.json and check whether they have an
* exports map called preview, if so add to the array
*/
addons.forEach(async (addon) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: forEach with async operations won't work as expected - use Promise.all with map instead

mainConfig: StorybookConfigRaw,
previewConfigPath: string
) {
const previewConfig = await readConfig(previewConfigPath!);
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: dangerous non-null assertion on previewConfigPath could cause runtime errors if undefined

];
programNode.body = cleanupTypeImports(programNode, disallowList);

// Remove unused type aliases e.g. `type Story = StoryObj<typeof meta>;`
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: removing type aliases during array iteration can cause index misalignment if multiple types need to be removed

hasArgument: true,
description: 'Run automigrations',
icon: '🤖',
options: createOptions({}),
Copy link
Contributor

Choose a reason for hiding this comment

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

style: empty options object suggests no configuration control - verify if automigrate should have any configurable options like dry-run or specific migration targets

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. maintenance User-facing maintenance tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants