How do you dynamically import/ export components in an index file?
How can you dynamically import and export all modules in a folder in an index.js file? I'm working with ReactJS and wanting to import/export when the app is built (not during runtime)
before
// index.js
import ComponentA from './ComponentA';
import ComponentB from './ComponentB';
import ComponentC from './ComponentC';
export { ComponentA, ComponentB, ComponentC }
after - but doesn't work
// index.js
const fs = require('fs');
let components = {};
fs.readdirSync(__dirname)
.filter((f) => f !== 'index.js')
.forEach((file) => {
if (file.toLowerCase().endsWith('.js')) {
filePath = './'+file;
fileName = file.split('.')[0];
// Not sure how to write this part properly
components[fileName] = import(filePath);
}
});
export components; // SyntaxError: Unexpected token 'export'
from Recent Questions - Stack Overflow https://ift.tt/2Tgk2LT
https://ift.tt/eA8V8J
Comments
Post a Comment