Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 module syntax: is it possible to `export * as Name from ...`?

See question title. I found a great reference for the forms of export available, but I have not seen what I'm looking for.

Is it possible to do something like the following?

// file: constants.js
export const SomeConstant1 = 'yay';
export const SomeConstant2 = 'yayayaya';

// file: index.js
export * as Constants from './constants.js';

I.e. this would provide a named export Constants inside of index.js containing all of the named exports from constants.js.


This answer seems to indicate it's not possible in TypeScript; is the same true for pure JavaScript?

(This example is a bit contrived; in reality I'm trying to have a prop-types.js module that uses named exports for internal use within the React package, but also exports the prop type definitions under PropTypes for external consumption. I tried to simplify for the sake of the question.)

like image 891
vergenzt Avatar asked Jun 19 '17 22:06

vergenzt


People also ask

What is export default and named export in ES6?

Exports without a default tag are Named exports. Exports with the default tag are Default exports. Using one over the other can have effects on your code readability, file structure, and component organization. Named and Default exports are not React-centric ideas. They are es6 features.

What is ES6 module syntax?

An ES6 module is a file containing JS code. There's no special module keyword; a module mostly reads just like a script. There are two differences. ES6 modules are automatically strict-mode code, even if you don't write "use strict"; in them. You can use import and export in modules.

What can you export with module exports?

By module. exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method.


3 Answers

No, it's not allowed in JS either, however there is a proposal to add it. For now, just use the two-step process with importing into a local variable and exporting that:

// file: constants.js
export const SomeConstant1 = 'yay';
export const SomeConstant2 = 'yayayaya';

// file: index.js
import * as Constants from './constants.js';
export {Constants};
like image 141
Bergi Avatar answered Oct 09 '22 18:10

Bergi


Today in 2019, it is now possible.

export * as name1 from …;
like image 33
NPP Avatar answered Oct 09 '22 17:10

NPP


The proposal for this spec has merged to ecma262. If you're looking for this functionality in an environment that is running a previous JS, there's a babel plugin for it! After configuring the plugin (or if you're using ecma262 or later), you are able to run the JS in your question:

// file: constants.js
export const SomeConstant1 = 'yay';
export const SomeConstant2 = 'yayayaya';

// file: index.js
export * as Constants from './constants.js';

// file: component.js
import { Constants } from './index.js';

const newVar = Constants.SomeConstant1; // 'yay'
like image 4
josephemswiler Avatar answered Oct 09 '22 17:10

josephemswiler