Last active
April 4, 2020 22:42
-
-
Save Jaid/e4d1900905a5aa038f26bd87092b5de4 to your computer and use it in GitHub Desktop.
Examples for dynamic requires with Webpack require.context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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