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

Add babelrc for storybook #217

Merged
merged 3 commits into from
Jan 14, 2018
Merged

Add babelrc for storybook #217

merged 3 commits into from
Jan 14, 2018

Conversation

joshwcomeau
Copy link
Owner

When running React Storybook locally, I was getting the following error:

ERROR in ./.storybook/config.js
Module build failed: Error: Couldn't find preset "./.babelrc.js" relative to directory "/Users/joshu/work/flip-move/.storybook"

This is easy enough to understand; our .babelrc file now looks like this:

{
   "presets": ["./.babelrc.js"]
}

It appears that for some reason, Storybook config is trying to load this relative to the storybook dir, and failing to find it there.

I tried creating a new .babelrc file inside .storybook, and simply pointing it to ../.babelrc.js, but then it fails with a similar error, trying to open it from a subdirectory:

ERROR in ./stories/helpers/FlipMoveListItem.js
Module build failed: Error: Couldn't find preset "../.babelrc.js" relative to directory "/Users/joshu/work/flip-move/stories/helpers"

I tried making the path an absolute path with /.babelrc.js, no luck.

So yeah, my final solution is just to resurrect the former .babelrc, just for storybook. This is obviously kind of a gross solution!

Because it's a dev-only concern and I wanna get the major version shipped, I may just merge this and deal with it in a subsequent PR, but @Andarist or @Hypnosphi or anyone else, if you have any clues as to how to get Storybook to read .babelrc from the same relative root every time, I'd love to kill this and have both files point to the same .babelrc.js.

@Hypnosphi
Copy link
Collaborator

Related storybook issue: storybookjs/storybook#2610

@Hypnosphi
Copy link
Collaborator

Hypnosphi commented Jan 14, 2018

There an issue for supporting .babelrc.js as well: storybookjs/storybook#2582

@Hypnosphi
Copy link
Collaborator

BTW you can try to leave an empty object in .storybook/.babelrc, this way the default config should apply. This should work unless we use flow or decorators in stories

@Andarist
Copy link
Collaborator

That's weird, in general it's babel which loads this and this works fine, I've used this pattern with combination with numerous tools. I guess storybook is trying to be clever and re-implements babel config loading on its own (and fails while doing so).

I guess this is no biggie and I'd just use this PR's solution - if it works there is no point in searching for a better solution right now. This should get fixed in storybook anyway.

babel@7 will come with 'native' .babelrc.js support, but this "hack" of proxying to it from .babelrc should just work - it's should be possible to load any plugin/preset like that from relative paths.

@Hypnosphi
Copy link
Collaborator

storybook is trying to be clever and re-implements babel config loading on its own (and fails while doing so).

Unfortunately that's true

@Hypnosphi
Copy link
Collaborator

Hypnosphi commented Jan 14, 2018

I look at current storybook sources, and seems like it can work if we use extends instead of presets:

{
   "extends": "./.babelrc.js"
}

@joshwcomeau
Copy link
Owner Author

I look at current storybook sources, and seems like it can work if we use extends instead of presets

Woohoo! I had to extend the .babelrc rather than the .babelrc.js, but it works!

(also I tried just keeping it an empty object, but then it can't parse JSX since then it loads no babel presets, rather than just using Storybook defaults).

Thanks y'all, you're both awesome.

@joshwcomeau joshwcomeau merged commit e2722ce into master Jan 14, 2018
@joshwcomeau joshwcomeau deleted the fix-storybook-babel branch January 14, 2018 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants