Currently, I am working on a Vue js application where I am tasked with exporting all the files and components from a specific directory. These files/components need to be imported into a separate file named paths.js
.
If I use the require
function to import all the components in index.js
, how can I correctly export them so that they can be accessed in path.js
as
import {Component1, Component2} from index.js
?
Please note: I do not want to manually include each component in index.js using the import
keyword, which is why I am utilizing require
.
Index.js
import upperFirst from "lodash/upperFirst";
import camelCase from "lodash/camelCase";
const requireComponent = require.context(".", true, /\.vue$/); // fetches Component1.vue and Component2.vue
const components = {};
requireComponent.keys().forEach(fileName => {
const componentConfig = requireComponent(fileName);
const componentName = upperFirst(
camelCase(fileName.replace(/^\.\//, "").replace(/\.\w+$/, ""))
);
components[componentName] = requireComponent(fileName).default;
});
export default components;
paths.js
import {Component1, Component2} from 'index.js';'
console.log(Component1); // undefined
I have attempted to export components using export {...components}
, but that resulted in an error.