Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 module: re-export as object

I have moduleA which exports some functions:

// moduleA.js
export function f1() {...}
export function f2() {...}

Is there any way to re-export all exports of moduleA in moduleB and make it looks like one object:

// moduleB.js
export * as a from 'moduleA';  // pseudo code, doesn't work

So that I can use it in this way?

// main.js
import {a} from 'moduleB';
a.f1();
a.f2();
like image 695
aleung Avatar asked Dec 09 '15 08:12

aleung


People also ask

How do I reexport JavaScript?

To re-export values from another file in JavaScript, make sure to export the name exports as export {myFunction, myConstant} from './another-file. js and the default export as export {default} from './another-file. js' . The values can be imported from the file that re-exported them.

Can you export an object JavaScript?

The export statement is used when creating JavaScript modules to export objects, functions, variables from the module so they can be used by other programs with the help of the import statements. There are two types of exports. One is Named Exports and other is Default Exports.

Is module exports ES6?

With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

Can you export an IIFE?

IIFE is hiddenly executed by a built-in wrapper functionality of Node. JS. When you need this exported one elsewhere, access it with “require” and you will get the same object you exported once.


1 Answers

The syntax is not supported yet but there is a proposal for it.

You can use it now with Babel.js or simply do:

import * as a from '...';
export {a};
like image 55
Felix Kling Avatar answered Sep 27 '22 21:09

Felix Kling