92

Is there a syntax using ES6 or ES7 or babel which will allow me to easily bundle together many groups of sub files?

E.g., given:

./action_creators/index.js
./action_creators/foo_actions.js
./action_creators/bar_actions.js

Have index.js import foo and bar actions, then re-export them, so I can

import {FooAction, BarAction} from './action_creators/index.js'

I don't want to have to remember / change references if I were to change which file I've organized the objects themselves into.

Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
Jordan Feldstein
  • 10,130
  • 10
  • 47
  • 78

2 Answers2

145

Yes, ES6 supports directly exporting imported modules:

export { name1, name2, …, nameN } from …;

export {FooAction, BarAction} from './action_creators/index.js'

You can also re-export all exports of the imported module using the * syntax:

export * from …;

export * from './action_creators/index.js';

More info on MDN.

TimoStaudinger
  • 38,697
  • 15
  • 86
  • 91
  • 50
    This [answer](https://stackoverflow.com/questions/41139740/how-to-re-export-another-modules-default-export?noredirect=1&lq=1) worked for me `export { default as foo } from './foo';` instead. – clodal Aug 03 '17 at 10:29
  • 8
    This doesn't work `export * as something from './something'`. Any ideas? – Qwerty Sep 03 '18 at 09:40
  • 14
    @Qwerty you cannot currently export as named namespaces like that. See https://github.com/tc39/proposal-export-ns-from for the proposal for it. One thing you can do in the meantime is something like: ``` import * as Something from './something'; export { Something } ``` – Jasmine Hegman Sep 15 '18 at 17:25
  • 2
    What happens if two subsequent lines of `export * from ...` export a named function with the same name? I mean, if `export * from 'a';` and `export * from 'b'`, both `a` and `b` define a function with the same name and export it, e.g.: `export function fn() { ... }`... – tonix Dec 01 '19 at 13:22
  • 6
    I came here looking for why I can't use `export * as something from ...`.... Hahahhh – Qwerty May 20 '20 at 17:44
  • @Qwerty Same . . . – David Callanan Aug 09 '20 at 08:24
  • Guys you can also do `export * as default from './some-module';` – smac89 Mar 31 '21 at 07:06
63

Default export as Default:

export {default} from './something';

Default export as Named:

export {default as foo} from './something';

Named export as Default:

export {foo as default} from './something';

Named export as Named:

export {foo} from './something';

Named export as Renamed:

export {foo as bar} from './something';
Gerson Diniz
  • 848
  • 7
  • 5