Search code examples
javascriptreact-nativeecmascript-6flowtypecommonjs

ES6 modules: How to automatically re-export all types in the current directory from index.js?


I have lots of code like this scattered around index.js files throughout my React Native project:

import Restaurant from './Restaurant';
import Store from './Store';
import Vineyard from './Vineyard';
import Wine from './Wine';

export {
    Restaurant,
    Store,
    Vineyard,
    Wine
};

It's very repetitive and tedious to write out. Is there a way I can automatically re-export all of the other files in the current working directory from index.js? (note I'm also using Flow in my project so any solution should preserve the type information it can infer.)

Thanks.


Solution

  • export * from './Restaurant';
    export * from './Store';
    

    By using the above syntax, you can access all exported properties from each component and export them directly.

    This is a common pattern when you grouping all Actions in each individual Action file inside index.js and exporting them directly. You can have a look at the github repo