Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to export the result of "import * as" in ES2015?

In ES2015, it's possible to import an entire module as an object whose properties are the module's exports:

import * as name from 'module';

I find this to be extraordinarily useful for namespacing and use it all the time.

It's also possible to re-export other modules' exports:

export { name } from 'module'; // selectively
export * from 'other-module'; // indiscriminately

Now I'm trying to write a library with namespacing in this style. The intuitive way of collecting everything in the top-level module would be like this:

export * as name from 'module';

But that doesn't seem to work; Babel and Rollup both reject it.

I could import the module as an object, create a clone by iterating over its keys, and export that, but then it would just be a plain old dynamic object, so I would lose the great advantages Rollup provides.

So, is there really no way to do this with the declarative module syntax? It seems to me like there's no excuse for that.

like image 546
Permutator Avatar asked Nov 26 '15 00:11

Permutator


1 Answers

No, this was simply missed in ES6. There is a stage 1 proposal to add these, though, and rollup will consider implementing it.

Until then, you will need to use two declarations and a local binding, altough there's no need to clone the object:

import * as name from 'module';
export { name };
like image 109
Bergi Avatar answered Oct 13 '22 18:10

Bergi