Module syntax

David Herman dherman at mozilla.com
Wed Jun 5 12:37:11 PDT 2013


On Jun 5, 2013, at 11:51 AM, Kevin Smith <zenparsing at gmail.com> wrote:

> It occurs to me that this is valid under the current grammar:
> 
>     import { default as foo } from "foo";
>     export { foo as default };
> 
> We've discussed using a well-known symbol for the default export, but this simple desugaring might be another option:
> 
>     import foo as "foo"; 
>     // => import { default as foo } from "foo";
> 
>     export default = expr; 
>     // => let __genident__ = expr; export { __genident__ as default };
> 
> This would provide easy access to the default export on a module instance object without having to obtain a reference to a symbol.
> 
>     module Foo from "foo";
>     F.default();
> 
> Just throwing an idea out...

Yes, this is actually the direction I've been going in my thinking, based on the critique that export default is the only export form that isn't a binding form, especially when combined with a named function literal (`export default function f() { ... }`). I also like the conceptual simplicity of the default export simply being a public export named "default". Moreover, Yehuda has urged me to consider

    export x = 17;

as sugar for

    export let x = 17;

When you put this all together, you can actually just see `export default = ...` as sugar for `export let default = ...`. We can also allow the keyword `default` to be used for the other exported declaration forms like `export function default(...) { ... }` and `export class default { ... }`. (We'll finesse the .name property of such a declared function to be taken from the module name, a la Brandon's accepted proposal for .name in ES6.) To recap, the following all become valid ways to specify the default export:

    export let default = 17;
    export default = 17;
    export default = function f() { ... };
    export default = class { ... };
    export function default(...) { ... }
    export class default { ... }

And you can now import both the default and non-default exports of a module in a single line:

    import { default as $, ajax } from "jquery";

Dave



More information about the es-discuss mailing list