Addon-Docs: Resolve CSF4 module exports without a default export#34834
Conversation
When a bundler splits a CSF4 story module (no `export default meta`)
into a separate chunk, the namespace object passed to `<Meta of={...} />`
differs by object identity from the one Storybook registered. The
existing default-export fallback in `DocsContext.resolveModuleExport`
cannot handle this case because CSF4 modules have no `default` key.
Fall back to identifying the CSF file via any of its story exports.
Reject the lookup when story exports span multiple CSF files. Guard
against individual CSF4 Story objects so `<Canvas of={Stories.Primary} />`
keeps resolving to a story.
Fixes storybookjs#34159
Fixes storybookjs#34373
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughDocsContext resolution now handles CSF4 modules without default exports. The test utility is enhanced to support optional default exports, the core logic conditionally guards default registration and adds fallback resolution scanning module values to their CSF files, and test coverage validates both non-default module resolution and Story object identity handling. ChangesCSF4 modules without default exports in DocsContext
Possibly related PRs
🎯 3 (Moderate) | ⏱️ ~20 minutes Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
kasperpeulen
left a comment
There was a problem hiding this comment.
Reviewed the DocsContext fallback for CSF4 modules without a default export. The change is narrowly scoped, rejects ambiguous multi-CSF namespace matches, preserves individual Story-object resolution, and matches the linked repro/root cause. Local focused tests/typecheck passed and CI is green.
Closes #34159
Closes #34373
What I did
DocsContext.resolveModuleExportlooks up CSF files by object identity, with a fallback through thedefaultexport to handle bundlers like Rolldown that don't preserve namespace identity. CSF4 stories withoutexport default metadefeat that fallback: there is nodefaultkey to match on, so a chunk-split namespace from MDX never resolves.This adds a second fallback that iterates the namespace's exported values, looking them up via the existing
exportToStorymap. If every recognized export points to the same CSF file, that file wins. If they span multiple CSF files, the lookup is rejected so we don't silently pick a wrong file. AnisStoryguard skips this loop for individual CSF4 Story objects so<Canvas of={Stories.Primary} />keeps resolving to a story.referenceCSFFilealso guards itsdefaultregistration — for CSF4 modules withoutdefault, the previous code was registeringundefinedin the map.Manual testing
The fix can be verified against the reporter's repro (https://github.com/yuheiy/storybook-mdx-meta-without-default-export-bug):
npm installnpm run build-storybooknpx http-server storybook-statichttp://localhost:8080/iframe.html?viewMode=docs&id=button--docsBefore/after screenshots from this reproduction are attached below.
What I changed
code/core/src/preview-api/modules/preview-web/docs-context/DocsContext.tsreferenceCSFFile: guard thedefaultexport registration so CSF4 modules don't pollute the map withundefinedresolveModuleExport: add a story-export fallback (withisStoryguard, and reject ambiguous multi-CSF matches)code/core/src/preview-api/modules/preview-web/docs-context/DocsContext.test.tsreferenceMeta: new test for CSF4 modules without default + different namespace identityreferenceMeta: new test that rejects namespaces whose story exports span multiple CSF filesresolveOf: new test for CSF4 namespace resolutionresolveOf: new regression test that a CSF4 Story object still resolves to its storycode/core/src/preview-api/modules/preview-web/docs-context/test-utils.tscsfFileParts: optionalincludeDefaultExportto simulate CSF4 modulesChecklist
DocsContext.test.ts(37 total pass)yarn nx check core)Related
Picks up the approach from the previous attempt #34375, which was closed because the author did not provide before/after verification. The technique is the same (story-export fallback); this diff is focused (no unrelated reformatting) and ships with the requested verification.
Summary by CodeRabbit
Tests
Chores