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

addDecorator doesn't work on config.js #7058

Closed
allisonc2lee opened this issue Jun 12, 2019 · 28 comments
Closed

addDecorator doesn't work on config.js #7058

allisonc2lee opened this issue Jun 12, 2019 · 28 comments

Comments

@allisonc2lee
Copy link

allisonc2lee commented Jun 12, 2019

Describe the bug
I am trying to implement some global style. And I think config.js is the good place to put my code. I copied the addDecorator from the official storybook doc to test. But it shows the error:

in ./.storybook/config.js
Module build failed (from ./node_modules/react-scripts/node_modules/babel-loader/lib/index.js):
SyntaxError: /Users/ccl/dev/storybook-sandbox/.storybook/config.js: Unexpected token (3:35)

 1 | import { configure, addDecorator } from '@storybook/react';
 2 | 
> 3 | const CenterDecorator = storyFn => <div style={styles}>{storyFn()}</div>;
   |                                    ^
 4 | addDecorator(CenterDecorator);

I am using the create-react-app and @storybook/react": "^5.1.3
It is a problem with Create-react-app?

The addDecorator works fine on individual components but it does not work on config.js

@ndelangen
Copy link
Member

Do you have some custom webpack config? I suppose not, since you're using CRA, but I figure I'd ask anyway.

If I try that in one of our examples it works just fine, so I'm puzzled why it's not working for you.

@shilman
Copy link
Member

shilman commented Jun 13, 2019

You can fix it by adding preset-env and preset-react to your babel config. I'm not sure why you need to do this. 😭

@allisonc2lee
Copy link
Author

Do you have some custom webpack config? I suppose not, since you're using CRA, but I figure I'd ask anyway.

If I try that in one of our examples it works just fine, so I'm puzzled why it's not working for you.

I think the problem is CRA. addDecorator works fine after I set up my react project with webpack.

@Yankovsky
Copy link
Contributor

@shilman @chingchinglcc I had similar problem. Storybook has @babel/core and babel-loader in its peer dependencies and relies on you to install them. Check this link https://storybook.js.org/docs/guides/guide-react/
So, probably, babel-loader is not listed in your dependencies and it could be nested inside different modules in your node_modules directory. Because of that storybook can't use it and can't process jsx inside your config file.
Let me know if this solution works for you.

@ZebraFlesh
Copy link

You can fix it by adding preset-env and preset-react to your babel config. I'm not sure why you need to do this

This fixed it for me. I also am unclear on why we need to do this. Everything was working just fine with storybook 5.0, but as soon as I upgraded to the 5.1 releases it stopped working. Smells like someone made a breaking change.

@allisonc2lee
Copy link
Author

Yes. It fixed for me as well.

@shilman
Copy link
Member

shilman commented Jun 24, 2019

Smells like someone made a breaking change.

@ZebraFlesh These are the two explicitly babel-related changes in 5.1. I don't see anything suspicious here, but if anybody wants to dig in, this might be a good place to start: #6634 #6878

These workarounds are not required for every project, so it would be useful to understand what's in common with all the projects here. Are they all CRA projects? What version? @chingchinglcc @ZebraFlesh @Yankovsky

@ZebraFlesh
Copy link

Mine is hand rolled, not CRA.

@ZebraFlesh
Copy link

Looking over those PRs and combining it with the behavior I’m seeing, it looks SB 5.1 is loading my root babel.config.js file. Previously this only had a simple @babel/preset-env preset; due to a lack of preset-react, JSX wouldn’t parse. I do not have a custom babel config file in the .storybook config folder.

This is hard, because I really wanted the fix from the first PR for a different project. 😢

@ZebraFlesh
Copy link

More detailed steps to reproduce now that I'm not on my phone:

  1. Do not create any babel configuration in the .storybook folder.
  2. Create a babel.config.js file at the root of your repo:
module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                targets: {
                    node: 'current'
                }
            }
        ]
    ]
};
  1. Create a custom webpack config for storybook in .storybook/webpack.config.js:
const path = require('path');

module.exports = ({ config }) => {
    config.devtool = 'inline-source-map';
    config.module.rules.push({
        test: /\.scss$/,
        loaders: ['style-loader', 'css-loader', 'sass-loader'],
        include: path.resolve(__dirname, '../')
    });
    config.module.rules.push({
        test: /\.stories\.jsx?$/,
        loaders: [require.resolve('@storybook/addon-storysource/loader')],
        enforce: 'pre'
    });

    return config;
};
  1. In a single .stories.js file, add the following decorator:
storiesOf('My Component', module)
    .addDecorator(getStory => (
        <React.Fragment>
            {getStory()}
        </React.Fragment>
    ))
    .add('some story', () => ( ...
  1. Use storybook 5.0.11 and everything is fine.
  2. Upgrade to storybook 5.1.1 and you get an Unexpected token error on the decorator from step 4.

I'm not sure if step 3 is necessary, but I've included it for completeness.

@stale stale bot added the inactive label Jul 16, 2019
@ZebraFlesh
Copy link

Definitely still an issue

@stale stale bot removed the inactive label Jul 16, 2019
@storybookjs storybookjs deleted a comment from stale bot Jul 17, 2019
@shilman
Copy link
Member

shilman commented Jul 17, 2019

Good news, I think @mrmckeb is going to fix this!

@shilman shilman added the cra Prioritize create-react-app compatibility label Jul 17, 2019
@shilman
Copy link
Member

shilman commented Jul 26, 2019

OK here's what's happening: #6634 was an unintentional breaking change. If you used a babel.config.js in your project root prior to 5.1, Storybook would ignore it. Now it's picking it up.

The change was a weird, inconsistent one. The code should probably either read:

  const babelConfig =
    loadFromPath(path.resolve(configDir, '.babelrc')) ||
    loadFromPath(path.resolve(configDir, 'babel.config.js')) ||
    loadFromPath(path.resolve(projectRoot, '.babelrc')) ||
    loadFromPath(path.resolve(projectRoot, 'babel.config.js'));

OR

  const babelConfig =
    loadFromPath(path.resolve(configDir, '.babelrc')) ||
    loadFromPath(path.resolve(configDir, 'babel.config.js'));

Now we're in a funny situation where if we fix it the first way, we'll break a lot more people.

The second one is also a breaking change since we're removing support for project-level config.

My vote is the second fix. And potentially adding the first fix as part of 6.0.

Thoughts @ndelangen @ZebraFlesh @igor-dv ?

@igor-dv
Copy link
Member

igor-dv commented Jul 26, 2019

I think if it was a break change we need to fix it back and wait for the v6.

@ndelangen
Copy link
Member

ndelangen commented Jul 26, 2019

Reading the discussion on the PR by @LukeAskew, it was never supposed to load from the project root.

@shilman
Copy link
Member

shilman commented Jul 26, 2019

Yo-ho-ho!! I just released https://github.com/storybookjs/storybook/releases/tag/v5.2.0-beta.10 containing PR #7573 that references this issue. Upgrade today to try it out!

You can find this prerelease on the @next NPM tag.

Closing this issue. Please re-open if you think there's still more to do.

@shilman shilman closed this as completed Jul 26, 2019
@shilman
Copy link
Member

shilman commented Jul 31, 2019

Shiver me timbers!! I just released https://github.com/storybookjs/storybook/releases/tag/v5.1.10 containing PR #7573 that references this issue. Upgrade today to try it out!

@shilman
Copy link
Member

shilman commented Aug 2, 2019

Somehow project root babel.config.js is still being loaded. Repro: https://github.com/dburles/sb-babel-bug

Reopening this 😭

@shilman shilman reopened this Aug 2, 2019
@justinlazaro-iselect
Copy link

@ZebraFlesh @chingchinglcc can you give an example fix for this? thanks

@shilman
Copy link
Member

shilman commented Sep 3, 2019

@justinlazaro-iselect Does creating .storybook/babel.config.js with the following content work?

module.exports = {
  presets: [
    "@babel/preset-env",
    "@babel/preset-react"
  ]
};

@geraufN26
Copy link

Fixed for me

@shilman shilman modified the milestones: 5.1.x, 5.2.x Sep 23, 2019
@shilman shilman modified the milestones: 5.2.x, 5.3.x Jan 11, 2020
@ravikp7
Copy link

ravikp7 commented May 18, 2020

Hey @shilman This still doesn't work from me. I'm using v5.3.18 and it's still picking the babel.config.js from the root directory. Specifying custom .storybook/babel.config.js with the above config also doesn't work. But if I remove the root babel.config.js it just works. Is a there a version in which this works, so that I can use it? Thanks!

@shilman
Copy link
Member

shilman commented May 19, 2020

@ravikp7 have you tried 6.0-beta?

@ravikp7
Copy link

ravikp7 commented May 19, 2020

@shilman I just tried v6.0.0-beta.8, it is also picking up the root babel.config.js

Let me know if I can provide more debugging info or can contribute in any way to help fix this :)

@ndelangen
Copy link
Member

ndelangen commented May 19, 2020

@ravikp7 do you have a main.js file for storybook configuration?

Add a babel property there like so:

module.exports = {
  babel: async (babelConfig) => {
    // do anything with the babel config for preview
    return babelConfig;
  },
};

That should allow you to change the babel config exactly to your liking.

I assume you have a monorepo and there's many babel configs in your project?
Or what else is the reason you do not want storybook to pick up the babel.config.js in the root of your project?

@ravikp7
Copy link

ravikp7 commented May 19, 2020

@ndelangen Thanks for this!

I don't have a monorepo, it's a react project setup with react-boilerplate. Currently, I don't have an issue with the root babel.config.js being picked up in storybook as I changed the config which was conflicting with storybook.

I'm not sure if this is the expected behavior with storybook that it picks root babel.config.js

@stale
Copy link

stale bot commented Dec 25, 2020

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Dec 25, 2020
@ndelangen
Copy link
Member

config.js is no longer supported in storybook 7.0 beta

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

10 participants