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
javascript node.js reactjs ecmascript-6 react-router
add a comment |
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
javascript node.js reactjs ecmascript-6 react-router
add a comment |
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
javascript node.js reactjs ecmascript-6 react-router
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
javascript node.js reactjs ecmascript-6 react-router
asked Nov 8 at 21:17
user3112038
1615
1615
add a comment |
add a comment |
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"/>)
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
add a comment |
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]
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
add a comment |
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"/>)
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
add a comment |
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"/>)
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
add a comment |
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"/>)
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"/>)
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
add a comment |
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
add a comment |
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]
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
add a comment |
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]
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
add a comment |
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]
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]
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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