Import multiple files with a glob









up vote
0
down vote

favorite












I am building a modular react app. But I am running into an issue where I can't dynamically import the routes of my app. Imagine this:



app
├── app.js
└── modules
├── module_1
│   └── routes.js
├── module_2
│   └── routes.js
└── module_3
└── routes.js



Imagine all the routes.js files containing something like this:



//modules/module_*/route.js
import React, Fragment from 'react';
import Route from 'react-router-dom';

export default () =>
<Route path="/module/path" component=ModuleComponent>
;


I would then in my app.js to be able to do this:



const Routes = import('modules/**/routes.js');

const App = () => (
<BrowserRouter>
<Routes />
</BrowserRouter>
);
export default App;


Right now im running a separate node script that creates an index file for al the routes.js files but this is getting tedious. And i am hoping there might be a better way to do










share|improve this question

























    up vote
    0
    down vote

    favorite












    I am building a modular react app. But I am running into an issue where I can't dynamically import the routes of my app. Imagine this:



    app
    ├── app.js
    └── modules
    ├── module_1
    │   └── routes.js
    ├── module_2
    │   └── routes.js
    └── module_3
    └── routes.js



    Imagine all the routes.js files containing something like this:



    //modules/module_*/route.js
    import React, Fragment from 'react';
    import Route from 'react-router-dom';

    export default () =>
    <Route path="/module/path" component=ModuleComponent>
    ;


    I would then in my app.js to be able to do this:



    const Routes = import('modules/**/routes.js');

    const App = () => (
    <BrowserRouter>
    <Routes />
    </BrowserRouter>
    );
    export default App;


    Right now im running a separate node script that creates an index file for al the routes.js files but this is getting tedious. And i am hoping there might be a better way to do










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am building a modular react app. But I am running into an issue where I can't dynamically import the routes of my app. Imagine this:



      app
      ├── app.js
      └── modules
      ├── module_1
      │   └── routes.js
      ├── module_2
      │   └── routes.js
      └── module_3
      └── routes.js



      Imagine all the routes.js files containing something like this:



      //modules/module_*/route.js
      import React, Fragment from 'react';
      import Route from 'react-router-dom';

      export default () =>
      <Route path="/module/path" component=ModuleComponent>
      ;


      I would then in my app.js to be able to do this:



      const Routes = import('modules/**/routes.js');

      const App = () => (
      <BrowserRouter>
      <Routes />
      </BrowserRouter>
      );
      export default App;


      Right now im running a separate node script that creates an index file for al the routes.js files but this is getting tedious. And i am hoping there might be a better way to do










      share|improve this question













      I am building a modular react app. But I am running into an issue where I can't dynamically import the routes of my app. Imagine this:



      app
      ├── app.js
      └── modules
      ├── module_1
      │   └── routes.js
      ├── module_2
      │   └── routes.js
      └── module_3
      └── routes.js



      Imagine all the routes.js files containing something like this:



      //modules/module_*/route.js
      import React, Fragment from 'react';
      import Route from 'react-router-dom';

      export default () =>
      <Route path="/module/path" component=ModuleComponent>
      ;


      I would then in my app.js to be able to do this:



      const Routes = import('modules/**/routes.js');

      const App = () => (
      <BrowserRouter>
      <Routes />
      </BrowserRouter>
      );
      export default App;


      Right now im running a separate node script that creates an index file for al the routes.js files but this is getting tedious. And i am hoping there might be a better way to do







      javascript node.js reactjs ecmascript-6 react-router






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 21:17









      user3112038

      1615




      1615






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          Create an index.js in each folder you want to export things from and in the structure you want.



          You could for instance export * from './routes' in each of your modules and then in turn export these in your modules folder like



          import route1 from './module_1';
          import route2 from './module_2';
          import route3 from './module_3';
          export const routes = [route1, route2, route3];


          Then in your app.js you import routes from './modules and apply these similiar to:



          routes.map((route, i) => <Routes key="i"/>)





          share|improve this answer




















          • The problem it's a modular application. I will have to update the Router everytime a module gets added or deleted. The example you have given is already how I do it now, but I run a filewatcher instead to do the indexing for me. I was hoping for a more elegant solution tho.
            – user3112038
            Nov 8 at 22:05

















          up vote
          0
          down vote



          accepted










          The npm package import-glob does exactly what I'm trying to achieve.



          --edit---



          The package basically does what @E. Sundin described in his post. You basically add the package as a pre-loader in your JavaScript files which allows you to do



          import modules from "./foo/**/*.js";


          And the package expands it into:



          import * as module0 from "./foo/1.js";
          import * as module1 from "./foo/bar/2.js";
          import * as module2 from "./foo/bar/3.js";

          modules = [module0, module1, module2]





          share|improve this answer


















          • 1




            While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
            – Maccath
            Nov 9 at 12:53






          • 1




            Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review
            – souldeux
            Nov 9 at 13:58










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216259%2fimport-multiple-files-with-a-glob%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Create an index.js in each folder you want to export things from and in the structure you want.



          You could for instance export * from './routes' in each of your modules and then in turn export these in your modules folder like



          import route1 from './module_1';
          import route2 from './module_2';
          import route3 from './module_3';
          export const routes = [route1, route2, route3];


          Then in your app.js you import routes from './modules and apply these similiar to:



          routes.map((route, i) => <Routes key="i"/>)





          share|improve this answer




















          • The problem it's a modular application. I will have to update the Router everytime a module gets added or deleted. The example you have given is already how I do it now, but I run a filewatcher instead to do the indexing for me. I was hoping for a more elegant solution tho.
            – user3112038
            Nov 8 at 22:05














          up vote
          0
          down vote













          Create an index.js in each folder you want to export things from and in the structure you want.



          You could for instance export * from './routes' in each of your modules and then in turn export these in your modules folder like



          import route1 from './module_1';
          import route2 from './module_2';
          import route3 from './module_3';
          export const routes = [route1, route2, route3];


          Then in your app.js you import routes from './modules and apply these similiar to:



          routes.map((route, i) => <Routes key="i"/>)





          share|improve this answer




















          • The problem it's a modular application. I will have to update the Router everytime a module gets added or deleted. The example you have given is already how I do it now, but I run a filewatcher instead to do the indexing for me. I was hoping for a more elegant solution tho.
            – user3112038
            Nov 8 at 22:05












          up vote
          0
          down vote










          up vote
          0
          down vote









          Create an index.js in each folder you want to export things from and in the structure you want.



          You could for instance export * from './routes' in each of your modules and then in turn export these in your modules folder like



          import route1 from './module_1';
          import route2 from './module_2';
          import route3 from './module_3';
          export const routes = [route1, route2, route3];


          Then in your app.js you import routes from './modules and apply these similiar to:



          routes.map((route, i) => <Routes key="i"/>)





          share|improve this answer












          Create an index.js in each folder you want to export things from and in the structure you want.



          You could for instance export * from './routes' in each of your modules and then in turn export these in your modules folder like



          import route1 from './module_1';
          import route2 from './module_2';
          import route3 from './module_3';
          export const routes = [route1, route2, route3];


          Then in your app.js you import routes from './modules and apply these similiar to:



          routes.map((route, i) => <Routes key="i"/>)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 21:27









          E. Sundin

          2,757924




          2,757924











          • The problem it's a modular application. I will have to update the Router everytime a module gets added or deleted. The example you have given is already how I do it now, but I run a filewatcher instead to do the indexing for me. I was hoping for a more elegant solution tho.
            – user3112038
            Nov 8 at 22:05
















          • The problem it's a modular application. I will have to update the Router everytime a module gets added or deleted. The example you have given is already how I do it now, but I run a filewatcher instead to do the indexing for me. I was hoping for a more elegant solution tho.
            – user3112038
            Nov 8 at 22:05















          The problem it's a modular application. I will have to update the Router everytime a module gets added or deleted. The example you have given is already how I do it now, but I run a filewatcher instead to do the indexing for me. I was hoping for a more elegant solution tho.
          – user3112038
          Nov 8 at 22:05




          The problem it's a modular application. I will have to update the Router everytime a module gets added or deleted. The example you have given is already how I do it now, but I run a filewatcher instead to do the indexing for me. I was hoping for a more elegant solution tho.
          – user3112038
          Nov 8 at 22:05












          up vote
          0
          down vote



          accepted










          The npm package import-glob does exactly what I'm trying to achieve.



          --edit---



          The package basically does what @E. Sundin described in his post. You basically add the package as a pre-loader in your JavaScript files which allows you to do



          import modules from "./foo/**/*.js";


          And the package expands it into:



          import * as module0 from "./foo/1.js";
          import * as module1 from "./foo/bar/2.js";
          import * as module2 from "./foo/bar/3.js";

          modules = [module0, module1, module2]





          share|improve this answer


















          • 1




            While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
            – Maccath
            Nov 9 at 12:53






          • 1




            Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review
            – souldeux
            Nov 9 at 13:58














          up vote
          0
          down vote



          accepted










          The npm package import-glob does exactly what I'm trying to achieve.



          --edit---



          The package basically does what @E. Sundin described in his post. You basically add the package as a pre-loader in your JavaScript files which allows you to do



          import modules from "./foo/**/*.js";


          And the package expands it into:



          import * as module0 from "./foo/1.js";
          import * as module1 from "./foo/bar/2.js";
          import * as module2 from "./foo/bar/3.js";

          modules = [module0, module1, module2]





          share|improve this answer


















          • 1




            While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
            – Maccath
            Nov 9 at 12:53






          • 1




            Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review
            – souldeux
            Nov 9 at 13:58












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          The npm package import-glob does exactly what I'm trying to achieve.



          --edit---



          The package basically does what @E. Sundin described in his post. You basically add the package as a pre-loader in your JavaScript files which allows you to do



          import modules from "./foo/**/*.js";


          And the package expands it into:



          import * as module0 from "./foo/1.js";
          import * as module1 from "./foo/bar/2.js";
          import * as module2 from "./foo/bar/3.js";

          modules = [module0, module1, module2]





          share|improve this answer














          The npm package import-glob does exactly what I'm trying to achieve.



          --edit---



          The package basically does what @E. Sundin described in his post. You basically add the package as a pre-loader in your JavaScript files which allows you to do



          import modules from "./foo/**/*.js";


          And the package expands it into:



          import * as module0 from "./foo/1.js";
          import * as module1 from "./foo/bar/2.js";
          import * as module2 from "./foo/bar/3.js";

          modules = [module0, module1, module2]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 22:30

























          answered Nov 8 at 22:07









          user3112038

          1615




          1615







          • 1




            While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
            – Maccath
            Nov 9 at 12:53






          • 1




            Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review
            – souldeux
            Nov 9 at 13:58












          • 1




            While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
            – Maccath
            Nov 9 at 12:53






          • 1




            Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review
            – souldeux
            Nov 9 at 13:58







          1




          1




          While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
          – Maccath
          Nov 9 at 12:53




          While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
          – Maccath
          Nov 9 at 12:53




          1




          1




          Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review
          – souldeux
          Nov 9 at 13:58




          Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review
          – souldeux
          Nov 9 at 13:58

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216259%2fimport-multiple-files-with-a-glob%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Darth Vader #20

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Ondo