I have a Vue SPA app. Now I want to extend this app with app modules so I thought to make a folder src/modules
that contain the modules folders. I want to put at the root of each folder some configuration js files (store.js, route.js, and so on).
The question is: in the main files (src/store/index.js
, src/router/index.js
, etc.), how can I loop for every folder in src/modules
and check if some configuration file exists to add its content at the main config? Of course this operation should be executed at build time, modules install is not intended to be dynamically.
Thanks a lot
Asked
Active
Viewed 353 times
0

Neo710
- 95
- 10
-
Hmm, maybe using a build tool like Webpack or a shell script of some kind? – user115014 Oct 27 '22 at 09:20
-
@user115014 I don't want to use a shell script, I prefer to use javascript inside main files. Could you answer the question with the way to achive the solution with webpack? Thank you. – Neo710 Oct 27 '22 at 09:27
-
1Check how to access the filesystem from a stand point of a Node.js app (using `fs` mainly or `readFileSync` etc). Frontend cannot really scan local folders, just use static paths hardcoded as far as I know. – kissu Oct 27 '22 at 10:41
1 Answers
1
You can use require.context
const configurations = require.context('./modules', true, /config\.js$/i);
configurations.keys().forEach(filename =>
{
console.log(filename);
// file contents will be inside configurations[filename]
});

IVO GELOV
- 13,496
- 1
- 17
- 26