Created
May 18, 2017 20:17
-
-
Save lencioni/7aee7966f30de10b356a7abba37e590a to your computer and use it in GitHub Desktop.
Slurping up all examples and components with webpack
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
// Keep track of all DLS components: | |
const DLSComponents = {}; | |
// Keep track of all DLS examples: | |
const DLSExamples = {}; | |
function requireComponent(srcFile, requireFn) { | |
// Extract the component name from the file name: | |
let componentName = path.basename(srcFile, '.jsx'); | |
// Handle ComponentName/index.jsx: | |
if (componentName.startsWith('index')) { | |
componentName = path.basename(path.dirname(srcFile)); | |
} | |
return { [componentName]: requireFn(srcFile).default }; | |
} | |
function requireComponents(requireContext) { | |
requireContext.keys().forEach((srcFile) => { | |
Object.assign(DLSComponents, requireComponent(srcFile, requireContext)); | |
}); | |
} | |
function requireExample(exampleFile, requireFn) { | |
// Extract the component name from the file name: | |
const componentName = path.basename(exampleFile, '_example.jsx'); | |
return { [componentName]: requireFn(exampleFile).default }; | |
} | |
function requireExamples(requireContext) { | |
requireContext.keys().forEach((exampleFile) => { | |
Object.assign(DLSExamples, requireExample(exampleFile, requireContext)); | |
}); | |
} | |
export function browserDLSExamples() { | |
requireComponents(require.context('../src', true, /\.jsx$/)); | |
requireExamples(require.context('.', true, /_example\.jsx$/)); | |
return { DLSComponents, DLSExamples }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment