3

i want to write below import in one single line. is it possible?

 `import SellerAdaptor from '../adaptors/sellers';
  import UserAdaptor from '../adaptors/user';
  import ShopEarnAdaptor from '../adaptors/shop_earn';
  import ProductAdaptor from '../adaptors/product';
  import JobAdaptor from '../adaptors/job';
  import OrderAdaptor from '../adaptors/order';
  import NotificationAdaptor from '../adaptors/notification';
  import CategoryAdaptor from '../adaptors/category';
  import AdminAdaptor from '../adaptors/adminAdaptor';`
Deepak Tyagi
  • 75
  • 1
  • 11
  • 2
    Why'd you do that? What's wrong with multiline imports? – yunzen Mar 06 '19 at 11:05
  • 2
    Remove the line breaks, and it would be one single line.... – Usagi Miyamoto Mar 06 '19 at 11:05
  • 2
    Do the back ticks (`) belong to your code? – yunzen Mar 06 '19 at 11:05
  • 1
    no you can't but if you need to you can create a factory like module for all imports check this https://stackoverflow.com/questions/34648195/is-it-possible-to-do-multiple-class-imports-with-es6-babel – AZ_ Mar 06 '19 at 11:07
  • since all of those modules live under `adaptors`, you might as well create an `index.js` file and put all the exports in there. Given that, all the consumers will `import { ... } from '../adaptors'` – Hitmands Mar 06 '19 at 11:08

3 Answers3

6

No.

Destructuring would let you import many values from one module.

You can't import multiple modules at once.


A typical pattern here would be to have an ../adaptors/index which imported all the modules and then exported them:

import SellerAdaptor from './sellers';
import UserAdaptor from './user';
// etc
export { SellerAdaptor, UserAdaptor, etc };

Then you would be able to:

import { SellerAdaptor, UserAdaptor, etc } from "../adaptors/index";
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

To be able to write import { A, B, C } from 'myPackage', it's upon myPackage and how it has been written.

If they are different files, you can't do it. If you have one file adaptors that export the different modules, you can write your imports with the destructuring.

HRK44
  • 2,382
  • 2
  • 13
  • 30
0

You can do like as follows:

import SellerAdaptor from '../adaptors/sellers';
import UserAdaptor from '../adaptors/user';
import ShopEarnAdaptor from '../adaptors/shop_earn';
import ProductAdaptor from '../adaptors/product';
import JobAdaptor from '../adaptors/job';
import OrderAdaptor from '../adaptors/order';
import NotificationAdaptor from '../adaptors/notification';
import CategoryAdaptor from '../adaptors/category';
import AdminAdaptor from '../adaptors/adminAdaptor';


export {
    SellerAdaptor,
    UserAdaptor,
    ShopEarnAdaptor,
    ProductAdaptor,
    JobAdaptor,
    OrderAdaptor,
    NotificationAdaptor,
    CategoryAdaptor,
    AdminAdaptor
}

And Then You can Import like as follows

import { SellerAdaptor, UserAdaptor} from './<filename>'
Sourabh Somani
  • 2,138
  • 1
  • 13
  • 27