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

allow to import all stories files to storybook without explicitly requiring one file at a time #125

Closed
gavriguy opened this issue Apr 20, 2016 · 10 comments

Comments

@gavriguy
Copy link

it would be nice to add to the storybook config an option to require all story files that matches a specific file name pattern like *.storie.js - in a similar way that test runners are loading all test files in a projects

@arunoda
Copy link
Member

arunoda commented Apr 20, 2016

I think that's possible to do with webpack. So simply need to use it in the config.js
But I'm not exactly sure about the syntax.

@gavriguy
Copy link
Author

Hi, I followed your advice:

Assuming all your stories follow the name _story.js

e.g

components
|- Component1
   |- index.jsx
   |- _story.jsx
|- Component2
   |- index.jsx
   |- _story.jsx

then this is the way to require all project's stories

import { configure } from '@kadira/storybook';

function requireAll(requireContext) {
  return requireContext.keys().map(requireContext);
}

function loadStories() {
  requireAll(require.context("..", true, /_story\.jsx?$/));
}

configure(loadStories, module);

Maybe it will be good idea to add this to the docs. Let me know if you want me to open a pull request for it.

@arunoda
Copy link
Member

arunoda commented Apr 20, 2016

@gavriguy
Copy link
Author

I see its already documented there :)

import { configure } from '@kadira/storybook';

const req = require.context('./', true, /Story\.js$/)

function loadStories() {
  req.keys().forEach(req)
}

configure(loadStories, module);

@arunoda
Copy link
Member

arunoda commented Apr 20, 2016

oops.

@JohnAlbin
Copy link
Contributor

Updated URL of docs if you are landing here via Google search: https://getstorybook.io/docs/react-storybook/basics/slow-start-guide#create-the-config-file

ndelangen pushed a commit that referenced this issue Apr 5, 2017
Load config from project dir (fixes #119)
@philcockfield
Copy link
Contributor

This seems to have been pulled out of the documentation now. I've been using this approach for a long time, and it still currently works. Is this an approach that is still intended to be used?

// Load stories.
const req = require.context("../lib", true, /.stories.js$/);
configure(() => {
  req.keys().forEach(filename => req(filename));
}, module);

@shilman
Copy link
Member

shilman commented May 21, 2017

@philcockfield it's in the docs:

https://storybooks.js.org/docs/react-storybook/basics/writing-stories/#loading-stories-dynamically

maybe we should also add it to the "slow start guide"? any suggestions welcome!

@daverau-optimizely
Copy link

Some folks searching might find this useful for sorting all stories ignoring folder structure/hierarchy, using the options addon, and this in your config:

setOptions({
  sortStoriesByKind: true
});

@ivansnek
Copy link

ivansnek commented Oct 7, 2018

For those RN devs having this issue, according the docs the solution is: https://github.com/elderfo/react-native-storybook-loader I tried and it works

Docs Reference: https://storybook.js.org/basics/writing-stories/#loading-stories-dynamically

adamchenwei added a commit to adamchenwei/vuejs-storybook-storyshot-jest-webpack-boilerplate that referenced this issue Oct 25, 2018
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

7 participants