Storybook: Improve docs & assist with automatic story loading

Created on 4 Apr 2017  路  11Comments  路  Source: storybookjs/storybook

config.js requires you to require() all your stories, It's very common to see users use webpack to autorequire all stories using a pattern:

const regex = /.stories.js$/;
const context = require.context('../src/components', true, regex)
context.keys().map(context)

However this is undocumented. We should add this to the docs and possibly add a demo-project using this to refer to.

discussion

All 11 comments

not easy to find in the docs, but it is in there: https://getstorybook.io/docs/react-storybook/basics/writing-stories#loading-stories-dynamically

The code in the docs doesn't mention this but it also support nested folders too.

something like stories/buttons/cancelButton.stories.jsx would also work. makes organization a lot easier.

yep, i keep my stories in nested folders, right next to the component. the details in the docs support that approach well

@travi You say it was not easy to find, do you have any suggestions how to make it easier to find?

@ndelangen I feel like this should be a default that *.stories.js(x) be automatically included when you define a stories folder with an option to opt out or define custom filename regex in the config file.

yeah something to think about.

@ndelangen good question. docs organization is never simple. the main thought i guess i have it that i would have expected these details under the "slow start guide" where it describes the config file since its more related to config than the stories themselves

i would appreciate for checking stor(ies|y). we use this pattern in our modules:

reducer.js
component.js
story.js

but at the same time, i don't believe storybook should do this by default. this seems more like an implementation detail.

I could see us auto-loading all /.story.js$/ in the future.

I could see us auto-loading all /.story.js$/ in the future.

I like this pattern because it is consistent with Jest.

I will close this and create a new issue dedicated to:

I could see us auto-loading all /.story.js$/ in the future.

done: https://github.com/storybooks/storybook/issues/1013

Was this page helpful?
0 / 5 - 0 ratings