JavaScript - import/export & destructuring

2.9k views Asked by At

Is there a way to combine these two export statements?

I understand that modules are not object literals, but it seems strange that I cannot actually import and then export actions here. From what I've tried so far, it appears that you have to export directly from an import.

And if no solutions are currently available, are there any future proposals on the horizon that anyone knows about?

// index.js

import reducer from './reducer';
import selectors from './selectors';

export * from './actions';
export { reducer as default, selectors };

Update

Since an explicit import/export doesn't appear to be possible, I decided to settle for consistency, making my selectors object a regular export as opposed to a default export, and then directly exporting from all three imports.

export * from './actions';
export * from './selectors';
export { reducer as default } from './reducer';

I'd still be interested in a solution for the original question, though.

1

There are 1 answers

2
Sebastian Sebald On BEST ANSWER

What about:

export { reducer as default } from './reducer';
export * from './selectors';
export * from './actions';

Maybe not 100% what you want, but it's still one less line of code.


But you actually could do the destructuring with CJS. Something like this should work:

exports = {
  default: require('./reducer').reducer,
  selectors: require('./selectors'),
  ...require('./actions')
};