Currently I am loading all of my Vue components with require.context
, this searches my components
directory with a regex for .vue
files. This works fine but I would like to load async components as well with dynamic imports.
Currently when I use require.context
all files get loaded so even If I want to use a dynamic import my file is already loaded and nothing happens.
I need a way to exclude certain files from my require.context
call. I cannot dynamically create a regex because this does not work with require.context
.
// How I currently load my Vue components.
const components = require.context('@/components', true, /[A-Z]\w+\.vue$/);
components.keys().forEach((filePath) => {
const component = components(filePath);
const componentName = path.basename(filePath, '.vue');
// Dynamically register the component.
Vue.component(componentName, component);
});
// My component that I would like to load dynamically.
Vue.component('search-dropdown', () => import('./search/SearchDropdown'));
It seems the only way to do this is either manually declare all my components, which is a big hassle.
Or to create a static regex that skips files that have Async
in their name. Which forces me to adopt a certain naming convention for components that are async. Also not ideal.
Would there be a better way to go about doing this?