Skip to content

Instantly share code, notes, and snippets.

@pmunin
Last active March 21, 2018 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmunin/ae8d24d68f239528adf73177265fa838 to your computer and use it in GitHub Desktop.
Save pmunin/ae8d24d68f239528adf73177265fa838 to your computer and use it in GitHub Desktop.
Javascript import all modules of 1st level subfolders (Webpack)
//gist url: https://gist.github.com/pmunin/ae8d24d68f239528adf73177265fa838
var context = require.context('./', true, /(\.\/)[^/]+(\/)$/);
//regex to get all 1st level subfolders paths (e.g.: "./subFolder/", but not "./subFolder/subFolder/")
const subModules = {}
context.keys().forEach((filename)=>{ //e.g.: ./subFolder/
const subModule = context(filename); //same as: import * as subModule from './subFolder/'
subModules[filename]=subModule;
});
export default subModules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment