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

Export a babel macro? #7

Open
tmeasday opened this issue Nov 29, 2018 · 1 comment
Open

Export a babel macro? #7

tmeasday opened this issue Nov 29, 2018 · 1 comment

Comments

@tmeasday
Copy link

tmeasday commented Nov 29, 2018

Hi @smrq thanks again for this plugin!

For storybook users using setups like create-react-app, using a babel plugin may not be an option; however a babel macro is. (This may only apply to CRA but it's a lot of folks I guess).

Would you consider adding a babel macro? I've done a POC here: https://github.com/tmeasday/require-context.macro/blob/master/require-context.macro.js

The basic idea is, you use it like:

import requireContext from './require-context.macro';

const req = requireContext('../stories', true, /.stories.js$/);

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

loadStories();

When babel is run via storybook (i.e. NODE_ENV !== 'test'), it will output:

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

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

loadStories();

When babel is run via jest (i.e. `NODE_ENV === 'test'), it will output:

const req = __requireContext(__dirname, '../stories', true, /.stories.js$/);

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

loadStories();

Does this make sense to you? I can submit a PR? (I'm not really sure what I am doing with babel though)

@kylemh
Copy link

kylemh commented Dec 17, 2018

I ended up publishing a macro here.

https://github.com/storybooks/require-context.macro

I saw that there's been a PR open for a few weeks and figured you were busy.

Let me know if you'll accept a PR that essentially brings in the above library! I'd rather have the macro exported here, since it's basically all your work.

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

No branches or pull requests

2 participants