Skip to content

Instantly share code, notes, and snippets.

@Jaid
Last active April 4, 2020 22:42
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 Jaid/e4d1900905a5aa038f26bd87092b5de4 to your computer and use it in GitHub Desktop.
Save Jaid/e4d1900905a5aa038f26bd87092b5de4 to your computer and use it in GitHub Desktop.
Examples for dynamic requires with Webpack require.context
const entries = {}
const requireContext = require.context("./plugins/", true, /^\.\/\w+\/index.js$/)
for (const relativePath of requireContext.keys()) {
const name = relativePath.match(/[/\\](?<name>.+?)[/\\]index\.js$/).groups.name
entries[name] = requireContext(relativePath).default
}
const entries = {}
const requireContext = require.context("./entries/", false)
for (const relativePath of requireContext.keys())
const name = relativePath.match(/\.\/(?<name>\w+)/).groups.name
entries[name] = requireContext(relativePath).default
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment