Created
April 16, 2018 15:16
-
-
Save AlanJenkinsVS/24535188e3d3f31e195e2254f5e6262d to your computer and use it in GitHub Desktop.
2. Component Registration - Vue JS Optimisations
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
/* | |
* Loads components from this directory with the name format of 'base-{name}.vue'. | |
* Components are registered globally. | |
* Note - don't do this for every component as this will bloat your bundles | |
*/ | |
import Vue from 'vue'; | |
import upperFirst from 'lodash/upperFirst'; | |
import camelCase from 'lodash/camelCase'; | |
// Require a base component context | |
const requireComponent = require.context( | |
'.', false, /base-[\w-]+\.vue$/ | |
) | |
requireComponent.keys().forEach(fileName => { | |
// Get Component config | |
const componentConfig = requireComponent(fileName); | |
// Get PascalCase name of component | |
const componentName = upperFirst( | |
camelCase(fileName.replace(/^\.\//, '').replace(/\.\w+$/, '')) | |
) | |
// Register component globally | |
Vue.component(componentName, componentConfig.default || componentConfig) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment