georgeperry
georgeperry

Reputation: 204

Automatically import modules with the same file suffix

I am currently setting up my Redux store and importing many different reducer files. This is beginning to look messy and wanted to know if there was a way to import all modules with the same file suffix. So currently...

import reducerOne from '../fileOne/one.reducer.js;
import reducerTwo from '../fileTwo/two.reducer.js;
import reducerThree from '../pathThree/fileThree/three.reducer.js;
import reducerFour from '../four.reducer.js;
import reducerFive from './five.reducer.js;
import reducerSix from '../longPathSix/pathSix/fileSix/six.reducer.js;
import reducerSeven from '../pathSeven/seven.reducer.js;

Is there a way that I can import all 'reducer.js' files instead of manually import each module separately when each of the file paths is different?

Upvotes: 0

Views: 214

Answers (1)

Shilly
Shilly

Reputation: 8589

As written in the duplicate question:

If you create an extra file reducers.js, with this definition:

import reducerOne from '../fileOne/one.reducer.js;
import reducerTwo from '../fileTwo/two.reducer.js;
import reducerThree from '../pathThree/fileThree/three.reducer.js;
import reducerFour from '../four.reducer.js;
import reducerFive from './five.reducer.js;
import reducerSix from '../longPathSix/pathSix/fileSix/six.reducer.js;
import reducerSeven from '../pathSeven/seven.reducer.js;
export {
  reducerOne,
  reducerTwo,
  reducerThree,
  reducerFour,
  reducerFive,
  reducerSix,
  reducerSeven
};

Then you can use this in your main file:

import { reducerOne, reducerTwo, reducerThree, reducerFour, reducerFive, reducerSix, reducerSeven } from '../reducers.js';

You basically 'bundle' all your reducers into one file with only one path. And since it's very few syntax, automating it to create such a file is trivial.

Upvotes: 1

Related Questions