Index: Support { csfData as default }
CSF exports
#18588
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue:
What I did
Parse csfMetadata from
{ someConst as default }
exports.This ensures that if CSF data is exported with the
{ data as default }
syntax rather than as
export default data
it is still recognized byStorybook.
This improves interop with automated code-generation performed by the
ReScript compiler.
ReScript background
The kind people in Discord asked me to provide a bit of background on ReScript. ReScript is "Fast, Simple, Fully Typed JavaScript from the Future" and provides "a robustly typed language that compiles to efficient and human-readable JavaScript. It comes with a lightning fast compiler toolchain that scales to any codebase size."
Contrary to TypeScript, which tries to type all the intricacies of JavaScript, ReScript takes a sound typesystem (borrowed from OCaml) and compiles to valid, human-readable JavaScript.
With respect to this PR it's important to note that the proposed change improves interoperability with JavaScript in general and doesn't attempt to support some kind of non-JavaScript dialect.
export default csfData
vsexport { csfData as default }
Before submitting this PR I dove into the differences between
export default
andexport { X as default }
. Jake Archibald, developer advocate for Google Chrome, did an excellent deep dive. I'll highlight (what I believe to be) the most important bits for this PR below.In most cases (except destructuring of a dynamic import) what is imported is a live binding: changing the value in the exporting file changes the value in the importing file.
Because
export default
syntax supports exporting a value directly (e.g.export default "Foo"
) which is not supported in other exports (i.e. you can'texport { "Foo" as X }
) some special behavior is needed.This means the interpreter sees
export default <expression>
as exporting the result of some expression. To make this work the result of the expression is assigned to a hidden variable which is actually exported.Because
export { thing as default }
follows the "normal exporting behavior" and is not in the specialexport default
form, it exportsthing
as a live-binding.What this means for Storybook
I can generate three examples to illustrate what happens.
As can be seen from Storybook's perspective (
import.mjs
) the two different forms are indistinguishable.How to test
If your answer is yes to any of these, please make sure to include it in your PR.