Skip to content

Instantly share code, notes, and snippets.

@zalmoxisus
Created August 2, 2016 14:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zalmoxisus/54925eed1805c095f0d1ebf5935fd5b1 to your computer and use it in GitHub Desktop.
Save zalmoxisus/54925eed1805c095f0d1ebf5935fd5b1 to your computer and use it in GitHub Desktop.
Require modules from all files in subdirectories as a nested object
function requireAll(requireContext) {
return requireContext.keys().reduce((previous, current) => {
const name = current.substring(current.lastIndexOf('/') + 1, current.lastIndexOf('.'));
previous[name] = requireContext(current);
return previous;
}, {});
}
const actionCreators = requireAll(require.context('../actions', true, /^\.\/.*\.js$/));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment