babel-plugin-require-context-hook
babel-plugin-require-context-hook copied to clipboard
Export a babel macro?
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)
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.