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

Naming variable before export default breaks code display #11702

Closed
aleph-naught2tog opened this issue Jul 27, 2020 · 3 comments
Closed

Naming variable before export default breaks code display #11702

aleph-naught2tog opened this issue Jul 27, 2020 · 3 comments

Comments

@aleph-naught2tog
Copy link

aleph-naught2tog commented Jul 27, 2020

Describe the bug
If you name the top-level story configuration in a stories file -- aka, what is usually done as export default { ... } but instead you do const config = { ... } ; export default config; no code will be available for display in the docs view. It will show "No code available".

To Reproduce
Reproduction: https://github.com/aleph-naught2tog/addon-docs-code-issue

Expected behavior
I expect to be able to assign what I am about to export to a variable thatVariable, then export default thatVariable and have the code still appear in the "Show Code" portion.

Screenshots
n/a

Code snippets
Here is a reproduction issue: https://github.com/aleph-naught2tog/addon-docs-code-issue

This code works and the Preview in the MDX page shows the code source just fine.

import * as React from 'react';

import { Button } from '../Button';

import mdx from '../button.mdx';

export default {
  title: 'Button',
  component: Button,
  parameters: {
    docs: {
      page: mdx,
    },
  },
};

// story below

In contrast, this means that the Preview in the MDX file cannot show the code (instead says "No Code Available").

import * as React from 'react';

import { Button } from '../Button';

import mdx from '../button.mdx';

const boop = {
  title: 'Button',
  component: Button,
  parameters: {
    docs: {
      page: mdx,
    },
  },
};

export default boop;

// story below

System:
Environment Info:

System:
OS: macOS 10.15.5
CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Binaries:
Node: 12.4.0 - ~/.nvm/versions/node/v12.4.0/bin/node
Yarn: 1.21.1 - /usr/local/bin/yarn
npm: 6.9.0 - ~/.nvm/versions/node/v12.4.0/bin/npm
Browsers:
Chrome: 84.0.4147.89
Safari: 13.1.1
npmPackages:
@storybook/addon-actions: ^5.3.19 => 5.3.19
@storybook/addon-docs: ^5.3.19 => 5.3.19
@storybook/addon-links: ^5.3.19 => 5.3.19
@storybook/addons: ^5.3.19 => 5.3.19
@storybook/preset-create-react-app: ^3.1.4 => 3.1.4
@storybook/react: ^5.3.19 => 5.3.19

Additional context
I verified that this is still an issue on v6.0.0-rc.15. I first noticed this issue in my current work code, but it's fully reproducible in the small repo above.

@rikkit
Copy link

rikkit commented Jul 30, 2020

I think I have a similar issue #11738 - I'm speculating that the code preview isn't actually running the module in the JS sense, but doing some other parsing to get the info it needs

@shilman
Copy link
Member

shilman commented Jul 31, 2020

That's right @rikkit. We need the default export to be statically analyzable, so export default { ... } not export default someVar

@shilman shilman closed this as completed Jul 31, 2020
This was referenced Nov 17, 2020
@zoephilipps
Copy link

zoephilipps commented Aug 10, 2022

👋 Hello there! I've written a blog post that references this issue as an explanation for an adjacent and closely related issue, which is that automatic story source only works with inline, named exports for CSF stories. You can read the applicable excerpt here: Statically analyzable 🤔

GitHub discussion: #18915

✌️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants