1

Is it possible to recreate the following with ES6 module syntax?

var foo = {};
module.exports = foo;

ES6 has support for adding the declarative keyword to the expression, like so:

export var foo = 'bar';

However, when run through 6to5, this generates:

var foo = exports.foo = 'bar';

Is it possible to use this syntax in conjunction with the default keyword, in order to generate the top code snippet?

1 Answer 1

4

You must export foo entity with default keyword:

var foo = {};
export default foo;

It will generate exactly what do you want.

PS: You can export only one default variable per module and can import it without curly brackets:

import foo from 'some_module';

If you're exporting multiple variables:

export var foo = 10;
export var boo = 'something';

Then you must import them using curly brackets:

import { foo, boo } from 'some_module';

In that case 6to5 will generate a little more complicated result than your example.

More about ES6 modules read here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.