Get data from file inside every folder using raw-loader

386 Views Asked by At

I have an example file structure:

-data
   -cars
   -users

Inside each of these folders (cars and users) I have a config.js file in which I export an object with data, e.g .:

export default {
     title: 'aaa',
     color: 'bbb',
}

Question: How can I get to each of these files (config.js) without importing them. I have a project written in Vue. I mean, for example, to download data by raw-loader from the webpack, but in it I have to declare the path and I mean that the path should look like this: ../data/(every folder inside 'data' folder)/config.js

const versioninfo = require(`raw-loader!${../data/(every folder inside 'data' folder)/config.js}`).default
console.log(versioninfo)
1

There are 1 best solutions below

0
On

I found another solution, I used require.context:

const getConfig = require.context(
    '../data',
    true,
    /\/[\w-]+\/config\.js$/,
)
getConfig.keys().forEach(fileName => {
    const file = require(`../data${fileName.substring(1)}`).default
    console.log(file)
})