Single import from modules without default export

Marius Gundersen gundersen at gmail.com
Wed Aug 6 02:06:42 PDT 2014


There are currently three (basic) different ways to imports:

```js
//method A: import the default export as anything the user wants
import defaultWithANewName from "moduleThatExportsUsingDefault";

//method B: import named exports
import {namedExport} from "moduleThatExportsNamedBindings";

//method C: import named exports with rename
import {namedExport as aNewName} from "moduleThatExportsNamedBindings";
```

The first import (A, without the curly braces) has several advantages:
 * It is two characters shorter than B and several characters shorter than C
 * The name of the imported binding is decided by the user of the module,
not the author
 * The user of the module does not need to know the name of the exported
binding, set by the author of the module.

Unfortunately it requires that the author of the module uses the `default`
keyword when exporting the binding. This keyword is meant to represent a
module with a single export, but this is not restricted. You can therefore
use `default` along with many named exports, or you can have a single named
export without using the `default` keyword. This means that the use of the
`default` keyword is not actually correlated with whether or not the module
is a single or multiple export module.

It would be great if we could have single export modules without using the
`default` keyword to mark the exported binding as being special. One way to
do this is for method A (import without curly braces) to import the first
exported binding from the module, no matter how many named bindings the
module has.

This has several advantages:
 * No special syntax or keyword needed for single export modules
 * Users of the module can import the first (only?) binding without needing
to know what it is called
 * Single export modules can be extended with multiple named bindings
without breaking the import of the module in existing code
 * Legacy code (using AMD or CommonJS) can export a binding without needing
to rename the exported object. IOW, they can be easily transpiled.

This assumes the module object (which is available using `module
theModuleObject from "moduleThatExportsSeveralBindings"`) is iterable, so
the first entry can be accessed.

I therefore want to propose that the default keyword is removed, that only
named bindings are allowed when exporting in a module, and that method A
imports the first named binding.

Marius Gundersen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140806/b053d7d9/attachment-0001.html>


More information about the es-discuss mailing list