Skip to content

Instantly share code, notes, and snippets.

@lencioni
Created May 18, 2017 20:17
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 lencioni/7aee7966f30de10b356a7abba37e590a to your computer and use it in GitHub Desktop.
Save lencioni/7aee7966f30de10b356a7abba37e590a to your computer and use it in GitHub Desktop.
Slurping up all examples and components with webpack
// 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