104

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
  • 795,719
  • 175
  • 1,089
  • 1,143
Jordan Warbelow-Feldstein
  • 10,510
  • 12
  • 48
  • 79

3 Answers3

160

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
  • 41,396
  • 16
  • 88
  • 94
  • 51
    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
  • 15
    @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
  • 3
    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
  • @tonix If there are two wildcard exports statements that implicitly re-export the same name, neither one is re-exported. – Paweł Mioduszewski Nov 15 '22 at 12:21
73

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
  • 1,073
  • 8
  • 9
0

Export and import twice

import {DEV as dev} from '../dev'
export {DEV} from '../dev'
export const PROD = !dev
dogdog
  • 133
  • 7