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

Question: Loading stories from individual component folders? #1171

Closed
reggi opened this issue May 31, 2017 · 8 comments
Closed

Question: Loading stories from individual component folders? #1171

reggi opened this issue May 31, 2017 · 8 comments

Comments

@reggi
Copy link

reggi commented May 31, 2017

I'm interested in editing my config.js to abide by a different storybook structure.

  • /components
    • /SearchBox
      • /index.js
      • /story.js

Where each storybook story for a given component lives in the same folder with the component itself.

import { configure } from '@kadira/storybook'

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

function loadStories () {
  req.keys().forEach((filename) => req(filename))
}

configure(loadStories, module)

I think this would be a useful config to document for others as well.

@shilman
Copy link
Member

shilman commented May 31, 2017

@reggi like this?

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

Or did you have something else in mind?

@reggi
Copy link
Author

reggi commented Jun 1, 2017

Thanks!

@reggi reggi closed this as completed Jun 1, 2017
@alistair-hmh
Copy link

alistair-hmh commented Jan 22, 2019

What if our components are in multiple node module folders? Eg:

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

const req = require.context("@my-lib/src/", true, /.stories.js$/);
const req2 = require.context("@your-lib/src/", true, /.stories.js$/);

// How can I require stories from req2?
configure(() => {
	req.keys().forEach(filename => req(filename));
}, module);

@zachintosh
Copy link

What if our components are in multiples node module folders? Eg:

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

const req = require.context("@my-lib/src/", true, /.stories.js$/);
const req2 = require.context("@your-lib/src/", true, /.stories.js$/);

// How can I require stories from req2?
configure(() => {
	req.keys().forEach(filename => req(filename));
}, module);

I would also like to know the right way to do this.

@alistair-hmh
Copy link

alistair-hmh commented May 11, 2019

@zlw11063 - I found this kind of thing to work ok...

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

const libs = ['@my-lib/src', '@your-lib/src'];

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

@wbern
Copy link

wbern commented May 14, 2019

@alistair-hmh don't you get webpack compile errors when using that?

@markolofsen
Copy link

My variant:

const req1 = require.context('../stories', true, /\.stories\.js$/);
const req2 = require.context('../widgets', true, /\.story\.js$/);
const libs = [req1, req2];

configure(() => libs.forEach(req => req.keys().forEach(f => req(f))), module);

@elewin
Copy link

elewin commented Nov 13, 2019

Just pass them in as an array:

configure([
  require.context('../src/components', true, /\.stories\.js$/),
  require.context('../lib', true, /\.stories\.js$/)
], module);

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

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

8 participants