Search code examples
javascriptamdecmascript-6babeljs

ES6 Default Module Syntax with 6to5


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?


Solution

  • 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